plugin, provider, gateway, upstream: a test whose check fails while its fake vendor holds a reply ends with a FAIL, not a package hung until -timeout (#1808)

TestNPMInfoDeadline's fake npm held slow-one's reply on <-release, closed only
on the happy path. A check firing first under load (on the issue's machine,
the quick ones missing at npmAll = 300ms) left the handler held, and
httptest.Server.Close, run in the test's cleanup, waited for it forever:
the whole internal/plugin package hung to -timeout and lost every result.

- TestNPMInfoDeadline: a t.Cleanup registered after fakeNPM (so it runs
  before the fake's Close) lets the handler go once (sync.OnceFunc). Its
  timing assumptions, all listed: the 2s wall-clock bound on Info is gone
  (slow-one is held and npmEach is an hour, so Info coming back at all is
  npmAll at work; a minute only turns a wait into a FAIL); npmAll is 5s,
  the quick ones' time to answer on a loaded machine, not 300ms; the slow
  one's kept answer is polled for 30s, not 3s.
- The same pattern in its siblings, each fake handler blocked on a channel
  only the happy path closed, now let go by a cleanup/defer that runs
  before the server's Close: TestRestartFinishesTheReply (plugin),
  TestPastedAddressAndCallbackFinishOnce, TestPartnersFetchedLastIsHeld
  (provider), TestBusy (before gw.Close too, whose request waits on it),
  TestLaneReleasedOnFailure (gateway), TestReadOnceInFresh (upstream; its
  100ms bound on Of goes the same way as the 2s one: the page is held, so
  Of coming back is its not waiting, 10s being short of the read's 15s).
- Audited, already right: grok_accounts, quota_refresh_order,
  quota_history_waiter, subscription_usage, sub2api_usage, qoder, routing,
  github_test, session_route, request_encoding, key_limits, vertex (quit),
  plugin_usage (r.Context).

Run, in a sandbox HOME with pinned caches:
- Old TestNPMInfoDeadline with its check forced (took > 0): "httptest.Server
  blocked in Close", panic at -timeout 30s. With a 500ms delay injected into
  the fake's answers (a loaded machine): FAIL at the quick-ones check, then
  the same hang. New test: with the delay injected, PASS (5.55s); with the
  check forced, FAIL in 5.5s and the package's other tests run; with
  npmAll an hour (the deadline broken), "Info waited for the slow package"
  and the package ends.
- Each sibling with t.Fatal injected while its handler is held: old code,
  all five httptest siblings hang to -timeout 90s ("blocked in Close"),
  upstream's stalls ~56s; new code, each ends at once with FAIL.
- go test -race -count=5 of every touched test: pass.
- gofmt -l (nothing), go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: clean, before and after rebasing onto
  a3f2097ed. go test -tags nogui ./... (darwin): all 49 packages ok before
  the rebase; after it, all ok but internal/proc's
  TestFindToolKeepsWrapperWhenMiseDoesntSay/hangs ("mise wasn't asked",
  a 300ms miseWait under load; passes 20 of 20 alone), filed as #1838.
Not run: the Windows box, Linux tests.
This commit is contained in:
yetone
2026-10-12 23:24:22 +08:00
parent f340aecabc
commit 3a4df527f5
7 changed files with 68 additions and 18 deletions
+7 -1
View File
@@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
@@ -33,6 +34,11 @@ func TestBusy(t *testing.T) {
s := New()
gw := httptest.NewServer(s.Handler())
defer gw.Close()
// deferred after both Closes, so it runs first: each waits for the held
// stream (the gateway's request on it), which a failed check would
// leave held (#1808)
let := sync.OnceFunc(func() { close(release) })
defer let()
if b := s.Busy(); b.Any() || !b.Last.IsZero() {
t.Fatalf("before any request: %+v", b)
}
@@ -53,7 +59,7 @@ func TestBusy(t *testing.T) {
if b := s.Busy(); b.Requests != 1 || b.Tools != 0 || !b.Any() {
t.Fatalf("streaming: %+v, want one request", b)
}
close(release)
let()
<-done
deadline := time.Now().Add(2 * time.Second)
for s.Busy().Requests != 0 && time.Now().Before(deadline) {
+5 -1
View File
@@ -226,6 +226,10 @@ func TestLaneReleasedOnFailure(t *testing.T) {
io.WriteString(w, "data: {\"id\":\"x\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")
}))
t.Cleanup(up.Close)
// runs before up.Close, which waits for a's held request: a check that
// fails before the gate opens below doesn't leave it held (#1808)
let := sync.OnceFunc(func() { close(gate) })
t.Cleanup(let)
one := 1
if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Key: "k", Models: []string{"m"}, Chat: up.URL + "/v1", MaxConcurrency: &one}); err != nil {
t.Fatal(err)
@@ -236,7 +240,7 @@ func TestLaneReleasedOnFailure(t *testing.T) {
within(t, "a out", func() bool { return lane().Busy == 1 })
g.send(context.Background(), "/v1/chat/completions", "slow/m", "b")
within(t, "b queued", func() bool { return lane().Waiting == 1 })
close(gate)
let()
got := map[string]laneReply{}
for range 2 {
r := g.reply()
+20 -7
View File
@@ -82,11 +82,24 @@ func TestNPMInfoDeadline(t *testing.T) {
}
npmAnswer(w, r)
})
npmAll = 300 * time.Millisecond
start := time.Now()
got := Info(context.Background(), []string{"fast-one", "slow-one", "fast-two"})
if took := time.Since(start); took > 2*time.Second {
t.Fatalf("took %v: the slow package held it", took)
// registered after fakeNPM, so it runs before the fake's Close, which
// waits for every handler: a check below that fails doesn't leave the
// slow one held and the package hung until -timeout (#1808)
let := sync.OnceFunc(func() { close(release) })
t.Cleanup(let)
// The slow one is held until the test lets it go and each package is
// waited on for an hour, so Info coming back at all is npmAll at work,
// with no wall-clock bound a loaded machine can pass; the minute only
// turns a wait into a FAIL. npmAll is the quick ones' time to answer,
// room enough on a loaded machine.
npmEach, npmAll = time.Hour, 5*time.Second
answered := make(chan map[string]NPM, 1)
go func() { answered <- Info(context.Background(), []string{"fast-one", "slow-one", "fast-two"}) }()
var got map[string]NPM
select {
case got = <-answered:
case <-time.After(time.Minute):
t.Fatal("Info waited for the slow package")
}
if got["fast-one"].Version != "1.2.3" || got["fast-two"].Version != "1.2.3" {
t.Fatalf("the quick ones: %+v", got)
@@ -94,8 +107,8 @@ func TestNPMInfoDeadline(t *testing.T) {
if _, ok := got["slow-one"]; ok {
t.Fatalf("the slow one before it answered: %+v", got["slow-one"])
}
close(release)
deadline := time.Now().Add(3 * time.Second)
let()
deadline := time.Now().Add(30 * time.Second)
for InfoCached([]string{"slow-one"})["slow-one"].Version == "" {
if time.Now().After(deadline) {
t.Fatal("the slow one's answer wasn't kept")
+6 -1
View File
@@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
@@ -128,6 +129,10 @@ func TestRestartFinishesTheReply(t *testing.T) {
io.WriteString(w, "data: last\n\n")
}))
defer srv.Close()
// deferred after srv.Close, so it runs first: Close waits for the
// slow reply's handler, which a failed check would leave held (#1808)
let := sync.OnceFunc(func() { close(release) })
defer let()
t.Setenv("FAKE_BASE", srv.URL+"/v1")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
@@ -160,7 +165,7 @@ func TestRestartFinishesTheReply(t *testing.T) {
if generation.Load() == before || !strings.Contains(string(b), "last") {
t.Fatalf("after the restart: a new host %v, %q", generation.Load() != before, b)
}
close(release)
let()
b, err = io.ReadAll(res.Body)
if err != nil || !strings.Contains(string(b), "data: last") {
t.Fatalf("the reply streaming through the restart: %q, %v", b, err)
+6 -1
View File
@@ -6,6 +6,7 @@ import (
"net/http/httptest"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -152,6 +153,10 @@ func TestPartnersFetchedLastIsHeld(t *testing.T) {
_, _ = rw.Write([]byte(b))
}))
t.Cleanup(srv.Close)
// runs before srv.Close, which waits for the held fetch: a check that
// fails before the release below doesn't leave it held (#1808)
let := sync.OnceFunc(func() { close(release) })
t.Cleanup(let)
t.Setenv("MAGPIE_PARTNERS", srv.URL)
Partners()
@@ -162,7 +167,7 @@ func TestPartnersFetchedLastIsHeld(t *testing.T) {
time.Sleep(5 * time.Millisecond)
}
body.Store(`{"partners":[]}`)
go func() { time.Sleep(50 * time.Millisecond); close(release) }()
go func() { time.Sleep(50 * time.Millisecond); let() }()
fetchPartnersNow()
<-served
// the earlier fetch has answered; give it the time to store its list
+7 -2
View File
@@ -9,6 +9,7 @@ import (
"net/url"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -128,6 +129,10 @@ func TestPastedAddressAndCallbackFinishOnce(t *testing.T) {
claudeHome(t)
hold := make(chan struct{})
trades := fakeCodexTokens(t, hold)
// registered after the fake, so it runs before the fake's Close, which
// waits for the held trade: a failed check doesn't leave it held (#1808)
let := sync.OnceFunc(func() { close(hold) })
t.Cleanup(let)
st, err := StartSignIn("codex")
if err != nil {
@@ -147,7 +152,7 @@ func TestPastedAddressAndCallbackFinishOnce(t *testing.T) {
browser := http.Client{Timeout: 3 * time.Second}
resp, err := browser.Get(strings.Replace(good, "localhost", "127.0.0.1", 1))
if err != nil {
close(hold)
let()
t.Fatalf("the browser's callback traded the code again: %v", err)
}
page, _ := io.ReadAll(resp.Body)
@@ -155,7 +160,7 @@ func TestPastedAddressAndCallbackFinishOnce(t *testing.T) {
if !strings.Contains(string(page), "already finishing") {
t.Fatalf("second callback's page: %s", page)
}
close(hold)
let()
if err := <-pasted; err != nil {
t.Fatal(err)
}
+17 -5
View File
@@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"
"slices"
"sync"
"sync/atomic"
"testing"
"time"
@@ -185,6 +186,10 @@ func TestReadOnceInFresh(t *testing.T) {
w.Write([]byte(claudePage("major_outage", "operational", "operational", "")))
}))
defer srv.Close()
// deferred after srv.Close, so it runs first: Close waits for the held
// read, which a failed check would leave held (#1808)
let := sync.OnceFunc(func() { close(slow) })
defer let()
for i := range Vendors {
if Vendors[i].ID == "anthropic" {
Vendors[i].Summary = srv.URL
@@ -192,15 +197,22 @@ func TestReadOnceInFresh(t *testing.T) {
}
defer func(n func() time.Time) { now = n }(now)
now = func() time.Time { return time.Now().Add(Fresh) }
start := time.Now()
s := Of("anthropic")
if took := time.Since(start); took > 100*time.Millisecond {
t.Fatalf("Of waited %v for the page", took)
// the page is held until let, so Of coming back at all is its not
// waiting for it: no wall-clock bound a loaded machine can pass (#1808).
// The deadline only turns a wait into a FAIL, and is short of the
// read's own 15s, after which a wait would come back too.
looked := make(chan []Status, 1)
go func() { looked <- Of("anthropic") }()
var s []Status
select {
case s = <-looked:
case <-time.After(10 * time.Second):
t.Fatal("Of waited for the page")
}
if s[0].Level != "ok" {
t.Fatalf("level %q while the page is read again, want the last one", s[0].Level)
}
close(slow)
let()
if s := one(t, "anthropic"); s.Level != "major" {
t.Fatalf("level %q once read again, want major", s.Level)
}