`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.
40 lines
958 B
Go
40 lines
958 B
Go
package chatd
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWaitUntilIdleForTestTimeout(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("RunnerOpen", func(t *testing.T) {
|
|
t.Parallel()
|
|
server := &Server{chatWorker: &chatWorker{manager: &runnerManager{
|
|
runners: map[runnerKey]*runnerRecord{{}: nil},
|
|
}}}
|
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Unix(1, 0))
|
|
defer cancel()
|
|
require.ErrorIs(t, WaitUntilIdleForTest(ctx, server), context.DeadlineExceeded)
|
|
})
|
|
|
|
t.Run("InflightWork", func(t *testing.T) {
|
|
t.Parallel()
|
|
server := &Server{}
|
|
release := make(chan struct{})
|
|
t.Cleanup(func() {
|
|
close(release)
|
|
server.drainInflight()
|
|
})
|
|
require.NoError(t, server.goInflight(func() { <-release }))
|
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Unix(1, 0))
|
|
defer cancel()
|
|
require.ErrorIs(t, WaitUntilIdleForTest(ctx, server), context.DeadlineExceeded)
|
|
})
|
|
}
|