Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: executor_test: reduce test execution time #1876

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

Merged
merged 5 commits into from
May 30, 2022

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented May 27, 2022

This PR removes 5-second wait in autobuild.executor unit tests (now approx. 1.6s on my machine):

  • Adds a write-only channel to Executor and plumbs through to unit tests
  • Modifies runOnce to return an executor.RunStats struct and write to statsCh if not nil

Closes #1859

@johnstcn johnstcn self-assigned this May 27, 2022
@johnstcn johnstcn changed the title Cj/gh 1859/autobuild test times chore: executor_test: reduce test execution time May 27, 2022
@johnstcn johnstcn force-pushed the cj/gh-1859/autobuild-test-times branch from f118679 to b0260d7 Compare May 30, 2022 09:39
@johnstcn johnstcn marked this pull request as ready for review May 30, 2022 11:43
@johnstcn johnstcn requested a review from a team May 30, 2022 11:44
SSHKeygenAlgorithm gitsshkey.Algorithm
APIRateLimit int
AutobuildTicker <-chan time.Time
AutobuildStatsChannel chan<- executor.RunStats
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could call this AutobuildStats. The fact that it's a channel doesn't seem very important to the caller in this context.

Comment on lines 45 to 51
// WithStatsChannel will cause Executor to push a RunStats to ch after
// every tick.
func (e *Executor) WithStatsChannel(ch chan<- RunStats) *Executor {
e.statsCh = ch
return e
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we always do this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eg. maybe we could remove this and just make it the default behavior.

Copy link
Member Author

@johnstcn johnstcn May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any major use-case for consuming these outside of the unit tests, hence the toggle.
Thoughts? Just pulling those values from the channel and dumping them to an underscore seems like a bit of a waste IMO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I think it's fine for now!

Comment on lines 27 to 32
// RunStats contains information about one run of Executor.
type RunStats struct {
Transitions map[uuid.UUID]database.WorkspaceTransition
Elapsed time.Duration
Error error
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably call this Stats. Since executor.Executor is the primary export of this package, it seems reasonable executor.Stats would attach to it.

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one question but otherwise LGTM!

@@ -92,6 +93,11 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, *coderd.API)
options.AutobuildTicker = ticker
t.Cleanup(func() { close(ticker) })
}
if options.AutobuildStatsChannel != nil {
t.Cleanup(func() {
close(options.AutobuildStatsChannel)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this could result in a send on nil channel at some point? Or is the Run() function guaranteed to have exited/not tick further at this point?

Copy link
Member Author

@johnstcn johnstcn May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a unit test, the ticks are sent manually. So, the only way a send could occur after the channel is closed is if you did something like the below without pulling from the stats channel.

func TestSomething(t *testing.T) {
  // setup goes here blah blah
  t.Cleanup(func() { 
    tickCh <- time.Now()
  })
  // stats := <- statsCh
}

Additionally, calls to t.Cleanup are executed in LIFO order, so I'd suspect that you might run into goleak complaining instead.

@johnstcn johnstcn added this to the Community MVP milestone May 30, 2022
@johnstcn johnstcn added api Area: HTTP API chore 🔧 labels May 30, 2022
@johnstcn johnstcn merged commit e02ef6f into main May 30, 2022
@johnstcn johnstcn deleted the cj/gh-1859/autobuild-test-times branch May 30, 2022 19:23
@misskniss misskniss mentioned this pull request Jun 1, 2022
20 tasks
kylecarbs pushed a commit that referenced this pull request Jun 10, 2022
Removes 5-second wait in autobuild.executor unit tests:

- Adds a write-only channel to Executor and plumbs through to unit tests
- Modifies runOnce to return an executor.RunStats struct and write to statsCh if not nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Area: HTTP API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

chore: improve autobuild.executor unit tests
4 participants