From 6855218884153e18882ee04b2c719bd707162ebe Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 14 Oct 2026 20:23:35 +0800 Subject: [PATCH] gateway tests: TestCanceledRequestRestsNobody cancels once a is asked, not 50ms after the request began (#2175) The sibling of #2175 (fixed for TestAgentGoneInTheRetryPause in b8070cc6c): this test canceled 50ms after the request began and let a answer 50ms later, assuming a was being asked by then. On a busy machine the cancel could land before a was asked, or a's 200 race the cancel, and the test no longer covers an agent leaving mid-request. The scripted fake now closes `hanging` as its first request starts to wait; the test cancels then, and a answers only once the gateway is done with the request (or after 10s, so nothing outlives the test). Run (rebased on origin/main 5ccc05afe): - broken by hand: a 150ms delay before ServeHTTP (the request reaching a late) still passes with the new wait. Before the rebase, with my own version of the TestAgentGoneInTheRetryPause wait: a 300ms delay in its fake upstream turned the old test red with the issue's shape (Millis 110/131, Again 0, Fail:canceled). - go test -race -run 'TestCanceledRequestRestsNobody$|TestAgentGoneInTheRetryPause$' -count=20: ok (360s, before rebase); -count=5 after rebase: ok - go test ./internal/gateway/ -count=1: ok (1419s, load average ~395, before rebase; the rebase changed only agent_gone_pause_test.go, taken as origin/main has it) - gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean Looked at and left alone: the 100-200ms sleeps before cancel in the claude_subscription rewind/takeback/park tests and in TestVertexMintWaitCancelled come after the state they need is already seen (the fake holds its reply forever), so a late cancel changes nothing. --- internal/gateway/retry_test.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/internal/gateway/retry_test.go b/internal/gateway/retry_test.go index 6377b8e82..efd6d5289 100644 --- a/internal/gateway/retry_test.go +++ b/internal/gateway/retry_test.go @@ -20,6 +20,7 @@ type scripted struct { replies []reply n int hang chan struct{} // when set, the first request waits on it + hanging chan struct{} // when set, closed as the first request begins to wait on hang limited http.Header // headers each 429 goes with } @@ -34,6 +35,9 @@ func (s *scripted) ServeHTTP(w http.ResponseWriter, r *http.Request) { i := min(s.n, len(s.replies)-1) s.n++ if s.hang != nil && i == 0 { + if s.hanging != nil { + close(s.hanging) + } <-s.hang } x := s.replies[i] @@ -249,17 +253,33 @@ func TestRateLimitIsWaitedOut(t *testing.T) { // rests for it and nobody else is asked. func TestCanceledRequestRestsNobody(t *testing.T) { fresh(t) - a := &scripted{replies: []reply{{200, "", chatOK}}, hang: make(chan struct{})} + a := &scripted{replies: []reply{{200, "", chatOK}}, hang: make(chan struct{}), hanging: make(chan struct{})} b := &scripted{replies: []reply{{200, "", chatOK}}} scriptedOn(t, "a", provider.Chat, a) scriptedOn(t, "b", provider.Chat, b) provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}) s := New() ctx, cancel := context.WithCancel(context.Background()) - go func() { time.Sleep(50 * time.Millisecond); cancel(); time.Sleep(50 * time.Millisecond); close(a.hang) }() + served := make(chan struct{}) + // the agent goes away while a is asked (not a moment after the request + // began: on a busy machine a may not be asked yet, #2175), and a never + // answers until the gateway is done with the request + go func() { + select { + case <-a.hanging: + cancel() + case <-served: + } + select { + case <-served: + case <-time.After(10 * time.Second): + } + close(a.hang) + }() rec := httptest.NewRecorder() s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)).WithContext(ctx)) + close(served) r := s.trace.routes[len(s.trace.routes)-1] if b.n != 0 || len(r.Tries) != 1 || r.Tries[0].Fail != failCanceled || r.Tries[0].Rest != nil || s.resting("a") { t.Fatalf("b tried %d, tries %+v", b.n, r.Tries)