test: add testutil.WaitBuffer and replace time.Sleep in tests (#22922)

WaitBuffer is a thread-safe io.Writer that supports blocking until
accumulated output matches a substring or custom predicate. It
replaces ad-hoc safeBuffer/syncWriter types and time.Sleep-based
poll loops in tests with signal-driven waits.

- WaitFor/WaitForNth/WaitForCond for blocking on output
- Replace custom buffer types in cli/sync_test.go and
  provisionersdk/agent_test.go
- Convert time.Sleep poll loops to require.Eventually/require.Never
  in cli/ssh_test.go, coderd/activitybump_test.go,
  coderd/workspaceagentsrpc_test.go, workspaceproxy_test.go, and
  scaletest tests
This commit is contained in:
Mathias Fredriksson
2026-03-12 18:07:52 +02:00
committed by GitHub
parent a6697b1b29
commit 57af7abf1f
11 changed files with 449 additions and 161 deletions
+4 -4
View File
@@ -116,10 +116,10 @@ func TestWorkspaceActivityBump(t *testing.T) {
// is required. The Activity Bump behavior is also coupled with
// Last Used, so it would be obvious to the user if we
// are falsely recognizing activity.
time.Sleep(testutil.IntervalMedium)
workspace, err = client.Workspace(ctx, workspace.ID)
require.NoError(t, err)
require.Equal(t, workspace.LatestBuild.Deadline.Time, firstDeadline)
require.Never(t, func() bool {
workspace, err = client.Workspace(ctx, workspace.ID)
return err == nil && !workspace.LatestBuild.Deadline.Time.Equal(firstDeadline)
}, testutil.IntervalMedium, testutil.IntervalFast, "deadline should not change")
return
}
+9 -10
View File
@@ -233,17 +233,16 @@ func TestWorkspaceAgentRPCRole(t *testing.T) {
// Close the connection and give the server time to process.
_ = conn.Close()
time.Sleep(100 * time.Millisecond)
// Verify that connectivity timestamps were never set.
agent, err := db.GetWorkspaceAgentByID(dbauthz.AsSystemRestricted(ctx), r.Agents[0].ID)
require.NoError(t, err)
assert.False(t, agent.FirstConnectedAt.Valid,
"first_connected_at should NOT be set for non-agent role")
assert.False(t, agent.LastConnectedAt.Valid,
"last_connected_at should NOT be set for non-agent role")
assert.False(t, agent.DisconnectedAt.Valid,
"disconnected_at should NOT be set for non-agent role")
// Verify that connectivity timestamps were never set
// (first_connected_at, last_connected_at, disconnected_at).
require.Never(t, func() bool {
agent, err := db.GetWorkspaceAgentByID(dbauthz.AsSystemRestricted(ctx), r.Agents[0].ID)
if err != nil {
return false
}
return agent.FirstConnectedAt.Valid || agent.LastConnectedAt.Valid || agent.DisconnectedAt.Valid
}, testutil.IntervalMedium, testutil.IntervalFast, "connectivity timestamps should NOT be set for non-agent role")
})
// NOTE: Backward compatibility (empty role) is implicitly tested by