test: speed up agent container websocket close test (#25559)

`TestWatchAgentContainers/CoderdWebSocketCanHandleClientClosing` spent
about 15 seconds waiting for the real websocket heartbeat ticker to
detect that the client closed.

Add a clock-aware `HeartbeatClose` wrapper and pass `api.Clock` through
the containers watch handler so the test can drive the heartbeat
deterministically with `quartz.Mock`. The test still verifies the same
client-close teardown path, but it advances the heartbeat tick instead
of waiting for wall-clock time.

Refs #25557


Discovered as part of the work on CODAGT-381.
This commit is contained in:
Ethan
2026-05-22 20:10:25 +10:00
committed by GitHub
parent ca1f6b19a2
commit 705421bc5d
3 changed files with 19 additions and 2 deletions
+6
View File
@@ -21,6 +21,12 @@ func HeartbeatClose(ctx context.Context, logger slog.Logger, exit func(), conn *
heartbeatCloseWith(ctx, logger, exit, conn, quartz.NewReal(), HeartbeatInterval)
}
// HeartbeatCloseWithClock is like HeartbeatClose, but uses the provided
// clock so tests can drive heartbeat ticks deterministically.
func HeartbeatCloseWithClock(ctx context.Context, logger slog.Logger, exit func(), conn *websocket.Conn, clk quartz.Clock) {
heartbeatCloseWith(ctx, logger, exit, conn, clk, HeartbeatInterval)
}
func heartbeatCloseWith(ctx context.Context, logger slog.Logger, exit func(), conn *websocket.Conn, clk quartz.Clock, interval time.Duration) {
ticker := clk.NewTicker(interval, "HeartbeatClose")
defer ticker.Stop()