From ef7d357e19816f1305817a3476570a1c294a8d6b Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 22 Jul 2022 17:42:09 +0300 Subject: [PATCH] fix: Move timeout ctx closer to use in tests, increase timeout (#3109) Some contexts were moved closer to use so that test setup doesn't affect timeout. And timeout was increased for some others to avoid flakyness due to slow test runners. --- cli/list_test.go | 5 +++-- coderd/provisionerjobs_internal_test.go | 5 +++-- site/site_test.go | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/cli/list_test.go b/cli/list_test.go index 597e4451a9..8343f1fead 100644 --- a/cli/list_test.go +++ b/cli/list_test.go @@ -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) @@ -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) diff --git a/coderd/provisionerjobs_internal_test.go b/coderd/provisionerjobs_internal_test.go index 2a99148872..b09cd3d623 100644 --- a/coderd/provisionerjobs_internal_test.go +++ b/coderd/provisionerjobs_internal_test.go @@ -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() @@ -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() + // wow there are a lot of DB rows we touch... _, err = fDB.InsertAPIKey(ctx, database.InsertAPIKeyParams{ ID: keyID, diff --git a/site/site_test.go b/site/site_test.go index 3c5ffb7068..27b6edcbbf 100644 --- a/site/site_test.go +++ b/site/site_test.go @@ -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) defer cancelFunc() testCases := []struct { @@ -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 { @@ -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 {