From b8070cc6c840acf4a6f473d00e97319f80d1b642 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 14 Oct 2026 19:46:29 +0800 Subject: [PATCH] tests: TestAgentGoneInTheRetryPause cancels once the last try is in its pause, not 100ms after the upstream counted it CI on b75e21be3 (ubuntu, run 38549121391) failed group_replan: the last try ended 499 "canceled" after 112ms with Again 0, so the agent had left while the try was still in flight, not in the pause the test is about. The test counted the upstream's request and then slept a fixed 100ms before cancelling; on a loaded runner the gateway hadn't read the 503 by then. It now polls the trace until the last route has its tries and the last one carries Again (written just before the pause starts), then cancels. Run (worktree on origin/main 331c27703): - Injected a 150ms sleep in the fake upstream between counting and answering: the old test fails both subtests with CI's exact shape (Millis ~110, Again 0); the new one passes, -count=3. - Break check: with try.Gone = true taken out of both pause paths in gateway.go, the new test fails both subtests. - go test -race -run TestAgentGoneInTheRetryPause -count=8: ok (116s, load average ~300). - gofmt -l clean, go vet -tags nogui ./internal/gateway ok. Not run: the whole gateway package (only this test file changed). --- internal/gateway/agent_gone_pause_test.go | 25 ++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/gateway/agent_gone_pause_test.go b/internal/gateway/agent_gone_pause_test.go index 662b01778..c873becaa 100644 --- a/internal/gateway/agent_gone_pause_test.go +++ b/internal/gateway/agent_gone_pause_test.go @@ -83,7 +83,12 @@ func TestAgentGoneInTheRetryPause(t *testing.T) { if up.n.Load() != want { t.Fatalf("upstream asked %d times before the pause, want %d", up.n.Load(), want) } - time.Sleep(100 * time.Millisecond) + // and its answer has been read: the last try says how long the + // pause is (a fixed sleep here cancelled the last try still in + // flight on a loaded runner) + if !inPause(s, int(want), 10*time.Second) { + t.Fatal("the last try never started its pause") + } cancel() // the agent's window is closed <-ended // the route ends at once, not when the pause would have @@ -120,3 +125,21 @@ func TestAgentGoneInTheRetryPause(t *testing.T) { }) } } + +// inPause tells whether the last route's tries number n and the last of +// them waits to be asked again, polled for up to within. +func inPause(s *Server, n int, within time.Duration) bool { + for deadline := time.Now().Add(within); time.Now().Before(deadline); time.Sleep(5 * time.Millisecond) { + s.trace.mu.Lock() + ok := false + if k := len(s.trace.routes); k > 0 { + tries := s.trace.routes[k-1].Tries + ok = len(tries) == n && tries[n-1].Again > 0 + } + s.trace.mu.Unlock() + if ok { + return true + } + } + return false +}