Skip to content

fix: Move timeout ctx closer to use in tests, increase timeout #3109

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 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cli/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ func TestList(t *testing.T) {
t.Parallel()
t.Run("Single", func(t *testing.T) {
t.Parallel()
ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFunc()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
Expand All @@ -30,6 +28,9 @@ func TestList(t *testing.T) {
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())

ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFunc()
done := make(chan any)
go func() {
errC := cmd.ExecuteContext(ctx)
Expand Down
5 changes: 3 additions & 2 deletions coderd/provisionerjobs_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ func TestProvisionerJobLogs_Unit(t *testing.T) {
t.Parallel()

t.Run("QueryPubSubDupes", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
// mDB := mocks.NewStore(t)
fDB := databasefake.New()
Expand Down Expand Up @@ -65,6 +63,9 @@ func TestProvisionerJobLogs_Unit(t *testing.T) {
{ID: uuid.New(), JobID: jobID, Stage: "Stage3"},
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Copy link
Member

Choose a reason for hiding this comment

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

Why defer here over t.Cleanup?

Copy link
Member Author

Choose a reason for hiding this comment

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

I feel tests should prefer to read like idiomatic Go code, t.Cleanup is useful for helper functions but has no benefit over defer.

It could also lead to logic bugs where there are defers and t.Cleanup mixed, because the order of execution would differ.

defer close1()
t.Cleanup(close2)
defer close3()

This would execute in the following order: close3, close1, close2 likely leading to a logic bug.


// wow there are a lot of DB rows we touch...
_, err = fDB.InsertAPIKey(ctx, database.InsertAPIKeyParams{
ID: keyID,
Expand Down
6 changes: 3 additions & 3 deletions site/site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestCaching(t *testing.T) {
defer srv.Close()

// Create a context
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
Copy link
Member

Choose a reason for hiding this comment

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

(Idea, out-of-scope): should we have a "standard" set of timeouts we use for all unit tests?
e.g. "short" -> 1 second, "medium" -> 5 seconds, "long", 10 seconds?

Copy link
Member Author

Choose a reason for hiding this comment

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

I really like that idea! It's all over the place now and doesn't allow us to easily adjust for slow/fast platforms.

defer cancelFunc()

testCases := []struct {
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestServingFiles(t *testing.T) {
defer srv.Close()

// Create a context
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFunc()

testCases := []struct {
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestServingBin(t *testing.T) {
defer srv.Close()

// Create a context
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFunc()

for _, tr := range tt.reqs {
Expand Down