`chatd.WaitUntilIdleForTest` waited up to 10 seconds for the chat worker to go idle, dropped the timeout error, and then drained tracked work, so a test whose worker never went idle passed after a silent 10-second stall. The hook now takes the caller's `ctx` and returns an error when `ctx` ends before the worker is idle or the tracked work finishes. The 26 callers that expect success wrap it in `require.NoError`. The poll loop moves from `chatWorker.WaitIdle`, whose only caller was the hook, into `testhooks.go`. It polls on a real 10 ms ticker instead of the server clock, which a mock clock never advances. ## Why it is safe - The idle check is unchanged: the hook reads the runner manager under the worker's lock and returns at once when the worker or manager is nil. The drain still calls `drainInflight`, now in a goroutine so that `ctx` bounds it; that goroutine exits once `Close` cancels the tracked work. - `TestWaitUntilIdleForTestTimeout` pins both error returns without a database: it passes an expired `ctx` while one runner is open (`RunnerOpen`) or one tracked task is in flight (`InflightWork`) and requires `context.DeadlineExceeded`. Changing both returns to `return nil` fails both subtests with `Expected error with "context deadline exceeded" in chain but got nil`; changing one fails only its subtest. - Red-green for the idle wait: with `runnerManager.idle` forced to return `false`, `TestCreateChatImmediatelyProcessesNewChat` passes on `main` after 10.2 s and fails here at its 25 s context with `wait for the chat worker to go idle: context deadline exceeded`. - Red-green for the drain: on this PR's first head, whose drain ignored `ctx`, a probe holding one tracked task open stayed blocked 2 s past a 100 ms context, then returned `nil`. - Hidden timeouts: on `main`, with the hook instrumented to report errors and idle waits over 1 s, all 119 hook calls in the packages below returned in under 1 s without an error. ## Review focus Callers now pass their own test context instead of getting a fresh 10 s one. Every direct caller, and every caller of `coderdtest.WaitForChatSettled` and `waitForChatProcessed`, passes `testutil.WaitLong` or longer, so the budget grows, but the rest of the test shares it. The PR adds 100 lines and removes 54: the drain goroutine is 9 of them and the new test 39. Without the goroutine, a stalled title or summary call holds the hook past its deadline. ## Validation - `go test ./coderd/x/chatd/ -count=3`: pass. - `go test ./coderd/ -count=3 -run '<every test in exp_chats_test.go, exp_chats_workspace_file_test.go, and exp_chats_hooks_test.go>'`: pass. - `go test ./codersdk/toolsdk/ -count=3 -run '^(TestChatTools|TestCreateChatOrganizationDefaultModel)$'`: pass. - `go test -race ./coderd/x/chatd/ -run '^TestWaitUntilIdleForTestTimeout$' -count=50`: pass. - Mutants and the probe use `go test -overlay`; a copy with a syntax error fails to build, so the overlay applies. - `make pre-commit` passed. > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description.
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package chatd
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
// WaitUntilIdleForTest waits for background chat work tracked by the server to
|
|
// finish without shutting the server down. Tests use this to assert final
|
|
// database state only after asynchronous chat processing has completed.
|
|
// Close waits for the same tracked work, but also stops the server. It returns
|
|
// an error if ctx ends first.
|
|
func WaitUntilIdleForTest(ctx context.Context, server *Server) error {
|
|
idle := func() bool {
|
|
if server.chatWorker == nil {
|
|
return true
|
|
}
|
|
server.chatWorker.mu.Lock()
|
|
manager := server.chatWorker.manager
|
|
server.chatWorker.mu.Unlock()
|
|
return manager == nil || manager.idle()
|
|
}
|
|
// A real ticker rather than the server clock, because tests often run the
|
|
// server on a mock clock that never fires on its own.
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
for !idle() {
|
|
select {
|
|
case <-ticker.C:
|
|
case <-ctx.Done():
|
|
return xerrors.Errorf("wait for the chat worker to go idle: %w", ctx.Err())
|
|
}
|
|
}
|
|
drained := make(chan struct{})
|
|
go func() {
|
|
server.drainInflight()
|
|
close(drained)
|
|
}()
|
|
select {
|
|
case <-drained:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return xerrors.Errorf("wait for tracked chat work to finish: %w", ctx.Err())
|
|
}
|
|
}
|