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.
This commit is contained in:
Spike Curtis
2025-09-16 21:54:50 +04:00
committed by GitHub
parent 4fc0093388
commit 655a36c392
+6 -1
View File
@@ -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) {