From 0d76d16ec6733130319b9dc4ccc8d7bea1fcaf47 Mon Sep 17 00:00:00 2001 From: ggbdpq Date: Thu, 29 Oct 2026 23:36:22 +0800 Subject: [PATCH] test(gateway): let only the gateway's deadline end a stall's fake answer What changed: the fake relays in TestStalledAnswerSaysUrlOrB64 and TestVendorThatStopsMidAnswerIsSaid waited on r.Context().Done() with a time.After(5 * time.Second) fallback of their own. Under load the fake's 5s could end its answer before the gateway's 500ms stall deadline (drawUnit = 100ms) was acted on, so the gateway read a cut body and said 502 "isn't an images API's: unexpected end of JSON input" instead of the stall's 504 (#4245). The fakes now wait on the context alone: the deadline's cancel is what ends them, so the order can't flip. No product behavior changes; both tests still assert the same 504 wording. Run: go test ./internal/gateway -run 'TestStalledAnswerSaysUrlOrB64|TestVendorThatStopsMidAnswerIsSaid' -count=5 - ok. go test -tags nogui -timeout 45m ./internal/gateway/... - ok (500s). go vet ./... and GOOS=windows go vet ./... - ok; GOOS=linux go vet ./... fails in wails (undefined: pointer) at the base commit too. CGO_ENABLED=0 GOOS=linux|windows|darwin go build -tags nogui . - ok. Generated-by: GLM-5.3-Flash (ZCode) --- internal/gateway/draw_fetch_test.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/internal/gateway/draw_fetch_test.go b/internal/gateway/draw_fetch_test.go index 5d300dfad..48e99d82f 100644 --- a/internal/gateway/draw_fetch_test.go +++ b/internal/gateway/draw_fetch_test.go @@ -155,10 +155,9 @@ func TestVendorThatStopsMidAnswerIsSaid(t *testing.T) { s, _ := relayed(t, func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, `{"created":1,"data":[`) w.(http.Flusher).Flush() - select { - case <-r.Context().Done(): - case <-time.After(5 * time.Second): - } + // only the gateway's deadline may end the answer: a timer of the + // fake's could end it first under load and be read as a 502 (#4245) + <-r.Context().Done() }) code, _, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"relay/gpt-image-2.5-sunburst","prompt":"a 4K city"}`) if code != 504 || !strings.Contains(raw, "Relay began answering after 0s (200 OK) but hadn't sent all of its answer in 300ms: what came doesn't yet say whether the image was coming as a url or as b64_json; 21 B had come") { @@ -183,10 +182,8 @@ func TestStalledAnswerSaysUrlOrB64(t *testing.T) { s, _ := relayed(t, func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, c.sent) w.(http.Flusher).Flush() - select { - case <-r.Context().Done(): - case <-time.After(5 * time.Second): - } + // only the gateway's deadline may end the answer (#4245) + <-r.Context().Done() }) code, _, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"relay/gpt-image-2.5-sunburst","prompt":"a 2K city","size":"2048x1152"}`) if code != 504 || !strings.Contains(raw, c.want) {