gateway: TestRequestBodyProgressRenewsIdleDeadline leaves a loaded runner ten gaps of room in its idle window (#2567)

The 408 on macOS CI (run 38888578439, 19e9fc47f) was the test's timing,
not the gateway's renewal. idleBody moves the read deadline forward on
every wire byte, but the test gave it a 200ms idle window and 50ms gaps
between writes. The client and the gateway share one process with the
whole package, so on a loaded runner one gap ran past 200ms: a real
200ms idle that the gateway was right to refuse. A real upload uses the
60s default window and is not at risk.

The window is now 1s with sixteen writes 100ms apart. The test also
fails if the whole upload finishes inside one idle window, since then
nothing was renewed. A failure prints the longest gap it saw.

Run:
- Old test with one 250ms client stall injected after the second write:
  408 on identity, gzip and zstd.
- New test as written: green (1.5-1.7s per encoding). With the same
  250ms stall injected: green.
- New test with idleBody's SetReadDeadline renewal disabled: red, 408 on
  all three encodings (longest gap ~104ms, window 1s).
- gofmt -l clean; go vet -tags nogui for darwin, linux and windows: clean.
- Full go test -timeout 45m ./... through suite-lock (base ef6d5ca40),
  run at load 227-239: 64 packages ok. Not green:
  - internal/agent timed out at 45m (#2544);
  - TestIntentClassifierFails' slow step took 2.49s (#1829);
  - internal/gateway timed out at 45m in TestGroupFailsOverASchemaTooDeep
    (#2108).
  This change touches only a gateway test file, so the agent reds can't
  come from it.
- internal/gateway run alone afterwards, at load 301-333: ok (1480s).
This commit is contained in:
yetone
2026-10-16 22:07:41 +08:00
parent a4fa7bbd8c
commit a751f5282e
+22 -6
View File
@@ -21,7 +21,14 @@ func TestRequestBodyProgressRenewsIdleDeadline(t *testing.T) {
payload := bytes.Repeat([]byte("body"), 128)
wire := encodedBody(t, payload, encoding)
s := New()
s.requestLimits.readTimeout = 200 * time.Millisecond
// The idle window is ten times the gap between writes: the
// client and the gateway share one process with the whole
// package's tests, and a loaded runner stalled a 50ms gap past
// a 200ms window, a real idle the gateway rightly refused
// (#2567).
idle := time.Second
gap := idle / 10
s.requestLimits.readTimeout = idle
gw := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, ok := s.readRequestBody(w, r, provider.Responses, 0)
if !ok {
@@ -38,23 +45,32 @@ func TestRequestBodyProgressRenewsIdleDeadline(t *testing.T) {
t.Fatal(err)
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(3 * time.Second))
conn.SetDeadline(time.Now().Add(10 * time.Second))
fmt.Fprintf(conn, "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: %d\r\nContent-Encoding: %s\r\n\r\n", len(wire), encoding)
// Six progressing reads take longer than one idle interval.
step := (len(wire) + 5) / 6
// Sixteen progressing writes take longer than one idle window,
// so only renewing on progress lets the upload finish.
const writes = 16
step := (len(wire) + writes - 1) / writes
start := time.Now()
last, longest := start, time.Duration(0)
for offset := 0; offset < len(wire); offset += step {
time.Sleep(50 * time.Millisecond)
time.Sleep(gap)
longest = max(longest, time.Since(last))
last = time.Now()
if _, err := conn.Write(wire[offset:min(offset+step, len(wire))]); err != nil {
break
}
}
if took := time.Since(start); took <= idle {
t.Fatalf("upload took %v, inside one idle window of %v: nothing was renewed", took, idle)
}
res, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
t.Fatalf("progressing upload: %d", res.StatusCode)
t.Fatalf("progressing upload: %d (longest gap between writes %v, idle window %v)", res.StatusCode, longest, idle)
}
body, err := io.ReadAll(res.Body)
if err != nil || string(body) != "ok" {