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,