From 2d7009e50d0371d9d8641fb0e79ed9c21282eae6 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 3 Mar 2026 10:19:00 -0500 Subject: [PATCH] test: reduce unnecessary sleep durations in tests (#22552) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Removes `time.Sleep` calls in two test files by replacing them with deterministic or event-driven alternatives. ### Changes **`coderd/provisionerjobs_test.go`** (34.5s → 0.25s) Replaced `time.Sleep(1500ms)` with a direct SQL `UPDATE` to bump `created_at` by 2 seconds. The sleep existed purely to ensure different timestamps for sort-order testing. The fix is deterministic and cannot flake. Uses `NewDBWithSQLDB` (the test already required real Postgres via `WithDumpOnFailure`). **`coderd/database/pubsub/pubsub_test.go`** (2.05s → 1.3s) Replaced `time.Sleep(1s)` with a `testutil.Eventually` retry loop that publishes and checks for subscriber receipt. This is the idiomatic pattern in the codebase. The old sleep waited for pq.Listener to re-issue LISTEN after reconnect; the new code polls until it actually works. --- coderd/database/pubsub/pubsub_test.go | 36 +++++++++++++++++++-------- coderd/provisionerjobs_test.go | 12 ++++++--- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/coderd/database/pubsub/pubsub_test.go b/coderd/database/pubsub/pubsub_test.go index 1ee2801bd4..066b9ce59a 100644 --- a/coderd/database/pubsub/pubsub_test.go +++ b/coderd/database/pubsub/pubsub_test.go @@ -151,7 +151,10 @@ func TestPGPubsubDriver(t *testing.T) { gotChan := make(chan struct{}, 1) defer close(gotChan) subCancel, err := subber.Subscribe("test", func(_ context.Context, _ []byte) { - gotChan <- struct{}{} + select { + case gotChan <- struct{}{}: + default: + } }) require.NoError(t, err) defer subCancel() @@ -174,14 +177,27 @@ func TestPGPubsubDriver(t *testing.T) { // wait for the reconnect _ = testutil.TryReceive(ctx, t, subDriver.Connections) - // we need to sleep because the raw connection notification - // is sent before the pq.Listener can reestablish it's listeners - time.Sleep(1 * time.Second) - // ensure our old subscription still fires - err = pubber.Publish("test", []byte("hello-again")) - require.NoError(t, err) - - // wait for the message on the old subscription - _ = testutil.TryReceive(ctx, t, gotChan) + // The raw connection notification is sent before the + // pq.Listener re-issues LISTEN on the new connection. + // Rather than sleeping a fixed duration, retry publishing + // until the subscriber receives a message, which proves + // that the LISTEN has been re-established. + testutil.Eventually(ctx, t, func(_ context.Context) bool { + // Drain any stale signals before publishing. + select { + case <-gotChan: + default: + } + err := pubber.Publish("test", []byte("hello-again")) + if err != nil { + return false + } + select { + case <-gotChan: + return true + case <-time.After(testutil.IntervalFast): + return false + } + }, testutil.IntervalMedium, "subscriber did not receive message after reconnect") } diff --git a/coderd/provisionerjobs_test.go b/coderd/provisionerjobs_test.go index 91096e3b64..6584b6e241 100644 --- a/coderd/provisionerjobs_test.go +++ b/coderd/provisionerjobs_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "strconv" "testing" - "time" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -28,7 +27,7 @@ func TestProvisionerJobs(t *testing.T) { t.Parallel() t.Run("ProvisionerJobs", func(t *testing.T) { - db, ps := dbtestutil.NewDB(t, dbtestutil.WithDumpOnFailure()) + db, ps, sqlDB := dbtestutil.NewDBWithSQLDB(t, dbtestutil.WithDumpOnFailure()) client := coderdtest.New(t, &coderdtest.Options{ IncludeProvisionerDaemon: true, Database: db, @@ -42,10 +41,17 @@ func TestProvisionerJobs(t *testing.T) { coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID) - time.Sleep(1500 * time.Millisecond) // Ensure the workspace build job has a different timestamp for sorting. workspace := coderdtest.CreateWorkspace(t, client, template.ID) coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID) + // Ensure the workspace build job has a different timestamp from + // the template version job for sorting, without sleeping. + _, err := sqlDB.ExecContext(context.Background(), + "UPDATE provisioner_jobs SET created_at = created_at + INTERVAL '2 seconds' WHERE id = $1", + workspace.LatestBuild.Job.ID, + ) + require.NoError(t, err) + // Create a pending job. w := dbgen.Workspace(t, db, database.WorkspaceTable{ OrganizationID: owner.OrganizationID,