-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Channel Refactoring #91
Conversation
server/server.go
Outdated
@@ -277,7 +279,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { | |||
} | |||
vcsClient := vcs.NewClientProxy(githubClient, gitlabClient, bitbucketCloudClient, bitbucketServerClient, azuredevopsClient) | |||
commitStatusUpdater := &events.DefaultCommitStatusUpdater{Client: vcsClient, StatusName: userConfig.VCSStatusName} | |||
terraformOutputChan := make(chan *models.TerraformOutputLine) | |||
terraformOutputChan := make(chan *models.ProjectCmdOutputLine) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
terraformOutputChan := make(chan *models.ProjectCmdOutputLine) | |
projectCmdOutput := make(chan *models.ProjectCmdOutputLine) |
terraformOutputChan chan<- *models.ProjectCmdOutputLine | ||
ProjectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
terraformOutputChan chan<- *models.ProjectCmdOutputLine | |
ProjectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler | |
projectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler |
replace every struct that uses terraformOutputChan with ProjectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler
@@ -105,7 +107,7 @@ func NewClientWithDefaultVersion( | |||
tfDownloader Downloader, | |||
usePluginCache bool, | |||
fetchAsync bool, | |||
terraformOutputChan chan<- *models.TerraformOutputLine, | |||
terraformOutputChan chan<- *models.ProjectCmdOutputLine, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update this to projectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
terraformOutputChan chan<- *models.ProjectCmdOutputLine, | |
projectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler, |
@@ -173,6 +175,7 @@ func NewClientWithDefaultVersion( | |||
versions: versions, | |||
usePluginCache: usePluginCache, | |||
terraformOutputChan: terraformOutputChan, | |||
ProjectCmdOutputHandler: handlers.DefaultProjectCommandOutputHandler{}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ProjectCmdOutputHandler: handlers.DefaultProjectCommandOutputHandler{}, | |
ProjectCmdOutputHandler: projectCmdOutputHandler, |
So all structs that have |
server/server.go
Outdated
@@ -508,7 +511,9 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { | |||
WorkingDir: workingDir, | |||
Webhooks: webhooksManager, | |||
WorkingDirLocker: workingDirLocker, | |||
TerraformOutputChan: terraformOutputChan, | |||
ProjectCmdOutputHandler: handlers.DefaultProjectCommandOutputHandler{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you defining a new handlers.DefaultProjectCommandOutputHandler
? You already created it pass in projectCmdOutputHandler
server/server.go
Outdated
@@ -302,7 +307,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { | |||
userConfig.TFDownloadURL, | |||
&terraform.DefaultDownloader{}, | |||
true, | |||
terraformOutputChan) | |||
*projectCmdOutputHandler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*projectCmdOutputHandler) | |
projectCmdOutputHandler) |
chanLock sync.RWMutex | ||
WebsocketHandler WebsocketHandler | ||
WebsocketHandler handlers.WebsocketHandler | ||
ProjectCommandOutputHandler handlers.DefaultProjectCommandOutputHandler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ProjectCommandOutputHandler handlers.DefaultProjectCommandOutputHandler | |
ProjectCommandOutputHandler handlers.ProjectCommandOutputHandler |
@@ -666,7 +667,10 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl | |||
e2eStatusUpdater := &events.DefaultCommitStatusUpdater{Client: e2eVCSClient} | |||
e2eGithubGetter := mocks.NewMockGithubPullGetter() | |||
e2eGitlabGetter := mocks.NewMockGitlabMergeRequestGetter() | |||
tempchan := make(chan *models.TerraformOutputLine) | |||
tempchan := make(chan *models.ProjectCmdOutputLine) | |||
projectCmdOutputHandler := handlers.DefaultProjectCommandOutputHandler{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
projectCmdOutputHandler := handlers.DefaultProjectCommandOutputHandler{ | |
projectCmdOutputHandler := &handlers.DefaultProjectCommandOutputHandler{ |
@@ -666,7 +667,10 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl | |||
e2eStatusUpdater := &events.DefaultCommitStatusUpdater{Client: e2eVCSClient} | |||
e2eGithubGetter := mocks.NewMockGithubPullGetter() | |||
e2eGitlabGetter := mocks.NewMockGitlabMergeRequestGetter() | |||
tempchan := make(chan *models.TerraformOutputLine) | |||
tempchan := make(chan *models.ProjectCmdOutputLine) | |||
projectCmdOutputHandler := &handlers.DefaultProjectCommandOutputHandler{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For tests you should generate mocks and use them instead of directly creating handlers.DefaultProjectCommandOutputHandler
object.
Same way we are mocking WebsocketHandler
err := callback(msg) | ||
if err != nil { | ||
p.logger.Err(err.Error()) | ||
} | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err := callback(msg) | |
if err != nil { | |
p.logger.Err(err.Error()) | |
} | |
return nil | |
if err := callback(msg); err != nil { | |
return err | |
} |
func NewProjectCommandOutputHandler() ProjectCommandOutputHandler { | ||
return &DefaultProjectCommandOutputHandler{ | ||
ProjectCmdOutput: make(chan *models.ProjectCmdOutputLine), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func NewProjectCommandOutputHandler() ProjectCommandOutputHandler { | |
return &DefaultProjectCommandOutputHandler{ | |
ProjectCmdOutput: make(chan *models.ProjectCmdOutputLine), | |
} | |
} | |
func NewProjectCommandOutputHandler(projectCmdOutput chan *models.ProjectCmdOutputLine, logger logging.SimpleLogging) ProjectCommandOutputHandler { | |
return &DefaultProjectCommandOutputHandler{ | |
ProjectCmdOutput: projectCmdOutput, | |
logger: logger, | |
controllerBuffer: map[string]map[chan string]bool{}, | |
} | |
} |
Use this function in the server.go
@@ -666,7 +667,8 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl | |||
e2eStatusUpdater := &events.DefaultCommitStatusUpdater{Client: e2eVCSClient} | |||
e2eGithubGetter := mocks.NewMockGithubPullGetter() | |||
e2eGitlabGetter := mocks.NewMockGitlabMergeRequestGetter() | |||
tempchan := make(chan *models.TerraformOutputLine) | |||
tempchan := make(chan *models.ProjectCmdOutputLine) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this
projectOutputHandler.Send(ctx, "Test Terraform Output") | ||
projectOutputHandler.Handle() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need these two lines.
@@ -119,11 +121,12 @@ type DefaultProjectCommandRunner struct { | |||
VersionStepRunner StepRunner | |||
RunStepRunner CustomStepRunner | |||
EnvStepRunner EnvStepRunner | |||
PullApprovedChecker runtime.PullApprovedChecker |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PullApprovedChecker runtime.PullApprovedChecker |
ApplyStepRunner: mockApply, | ||
RunStepRunner: mockRun, | ||
EnvStepRunner: &realEnv, | ||
PullApprovedChecker: nil, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PullApprovedChecker: nil, |
server/server.go
Outdated
@@ -508,10 +511,11 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { | |||
EnvStepRunner: &runtime.EnvStepRunner{ | |||
RunStepRunner: runStepRunner, | |||
}, | |||
PullApprovedChecker: vcsClient, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PullApprovedChecker: vcsClient, |
WorkingDir: workingDir, | ||
Webhooks: &mockWebhookSender{}, | ||
WorkingDirLocker: locker, | ||
PullApprovedChecker: e2eVCSClient, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PullApprovedChecker: e2eVCSClient, |
func (p *DefaultProjectCommandOutputHandler) writeLogLine(pull string, line string) { | ||
p.controllerBufferLock.Lock() | ||
if p.projectOutputBuffers == nil { | ||
p.projectOutputBuffers = map[string][]string{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can move this into NewProjectCommandOutputHandler
//Remove channel, so client no longer receives Terraform output | ||
func (p *DefaultProjectCommandOutputHandler) removeChan(pull string, ch chan string) { | ||
p.controllerBufferLock.Lock() | ||
delete(p.controllerBuffers[pull], ch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also close the channel here.
delete(p.controllerBuffers[pull], ch) | |
delete(p.controllerBuffers[pull], ch) | |
close(ch) |
if p.controllerBuffers == nil { | ||
p.controllerBuffers = map[string]map[chan string]bool{} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if p.controllerBuffers == nil { | |
p.controllerBuffers = map[string]map[chan string]bool{} | |
} |
// this logBuffers | ||
projectOutputBuffers map[string][]string | ||
// this is wsChans | ||
controllerBuffers map[string]map[chan string]bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename this into receiverBuffers
controllerBuffers map[string]map[chan string]bool | |
receiverBuffers map[string]map[chan string]bool |
return &DefaultProjectCommandOutputHandler{ | ||
ProjectCmdOutput: projectCmdOutput, | ||
logger: logger, | ||
controllerBuffers: map[string]map[chan string]bool{}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
controllerBuffers: map[string]map[chan string]bool{}, | |
controllerBuffers: map[string]map[chan string]bool{}, | |
projectOutputBuffers: map[string][]string{}, |
if p.projectOutputBuffers == nil { | ||
p.projectOutputBuffers = map[string][]string{} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if p.projectOutputBuffers == nil { | |
p.projectOutputBuffers = map[string][]string{} | |
} |
ProjectInfo: ctx.PullInfo(), | ||
Line: msg, | ||
ProjectInfo: ctx.PullInfo(), | ||
ClearBuffBefore: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't want to set this to true everytime you send a message. You want to clear the buffer before starting terraform process on a project. You can create a Clear(ctx models.ProjectCommandContext, msg string)
function that will set the flag to true
go func() { | ||
projectOutputHandler.Receive(ctx.PullInfo(), func(msg string) error { | ||
expectMsg = msg | ||
wg.Done() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can move this outside of receive callback and put as a last thing in the go routine. You are sending 3 messages to the queue. So it is calling wg.Done()
3 times. But wait group is only expecting 1 call.
go func() { | ||
projectOutputHandler.Send(ctx, "Test Terraform Output") | ||
}() | ||
|
||
go func() { | ||
projectOutputHandler.Clear(ctx, "") | ||
}() | ||
|
||
go func() { | ||
projectOutputHandler.Send(ctx, "Test Terraform Output") | ||
}() | ||
|
||
expectMsg := "" | ||
wg.Add(1) | ||
go func() { | ||
projectOutputHandler.Receive(ctx.PullInfo(), func(msg string) error { | ||
expectMsg = msg | ||
return nil | ||
}) | ||
wg.Done() | ||
}() | ||
wg.Wait() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should fix the test
go func() { | |
projectOutputHandler.Send(ctx, "Test Terraform Output") | |
}() | |
go func() { | |
projectOutputHandler.Clear(ctx, "") | |
}() | |
go func() { | |
projectOutputHandler.Send(ctx, "Test Terraform Output") | |
}() | |
expectMsg := "" | |
wg.Add(1) | |
go func() { | |
projectOutputHandler.Receive(ctx.PullInfo(), func(msg string) error { | |
expectMsg = msg | |
return nil | |
}) | |
wg.Done() | |
}() | |
wg.Wait() | |
wg.Add(1) | |
go func() { | |
projectOutputHandler.Send(ctx, "Test Terraform Output") | |
}() | |
wg.Add(1) | |
go func() { | |
projectOutputHandler.Clear(ctx, "") | |
}() | |
wg.Add(1) | |
go func() { | |
projectOutputHandler.Send(ctx, "Test Terraform Output") | |
}() | |
expectMsg := "" | |
go func() { | |
projectOutputHandler.Receive(ctx.PullInfo(), func(msg string) error { | |
expectMsg = msg | |
wg.Done() | |
return nil | |
}) | |
}() | |
wg.Wait() |
* Began channel refactoring * Fixing refactoring logic * Replacing terraformOutputChan * Making suggested changes * Changing attribute declarations * Fixed channel logic * Formatting * Changing tests to utilize mocks * Adding go files * Adding suggested changes * Suggested changes before merging * Reformatting * Deleting unnecesary lines * Buffer cleanup logic * Buffer cleanup & Testing * Trying to make test pass * Suggested changes * Fixing test * Fixing failing test * Error checking * Comment improvements * Adding buffer clearing for apply workflow * Removing clearing for apply workflow
* Began channel refactoring * Fixing refactoring logic * Replacing terraformOutputChan * Making suggested changes * Changing attribute declarations * Fixed channel logic * Formatting * Changing tests to utilize mocks * Adding go files * Adding suggested changes * Suggested changes before merging * Reformatting * Deleting unnecesary lines * Buffer cleanup logic * Buffer cleanup & Testing * Trying to make test pass * Suggested changes * Fixing test * Fixing failing test * Error checking * Comment improvements * Adding buffer clearing for apply workflow * Removing clearing for apply workflow
No description provided.