From 655a36c3921d12d197b1b7eb9ba767d0516b0005 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Tue, 16 Sep 2025 21:54:50 +0400 Subject: [PATCH] test: fix TestAgentConnectionMonitor_PingTimeout race with mock assertions (#19836) Fixes https://github.com/coder/internal/issues/970 The test doesn't wait for `monitor()` to complete, and the mock database call that we assert takes place in a `defer` within `monitor()`. This allows the mock assertions to race with the defer and flake the test. Solution is to explicitly wait for `monitor()` to complete before the end of the test, so that mock assertions (which happen in a `t.Cleanup()`) don't race. --- coderd/workspaceagentsrpc_internal_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/coderd/workspaceagentsrpc_internal_test.go b/coderd/workspaceagentsrpc_internal_test.go index f2a2c7c87f..d44a666979 100644 --- a/coderd/workspaceagentsrpc_internal_test.go +++ b/coderd/workspaceagentsrpc_internal_test.go @@ -150,9 +150,14 @@ func TestAgentConnectionMonitor_PingTimeout(t *testing.T) { AnyTimes(). Return(database.WorkspaceBuild{ID: build.ID}, nil) - go uut.monitor(ctx) + done := make(chan struct{}) + go func() { + uut.monitor(ctx) + close(done) + }() fConn.requireEventuallyClosed(t, websocket.StatusGoingAway, "ping timeout") fUpdater.requireEventuallySomeUpdates(t, build.WorkspaceID) + _ = testutil.TryReceive(ctx, t, done) // ensure monitor() exits before mDB assertions are checked. } func TestAgentConnectionMonitor_BuildOutdated(t *testing.T) {