diff --git a/internal/gateway/busy_test.go b/internal/gateway/busy_test.go index ed780154d..e143bb391 100644 --- a/internal/gateway/busy_test.go +++ b/internal/gateway/busy_test.go @@ -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) { diff --git a/internal/gateway/concurrency_account_test.go b/internal/gateway/concurrency_account_test.go index 492b62de6..4c5d0f48b 100644 --- a/internal/gateway/concurrency_account_test.go +++ b/internal/gateway/concurrency_account_test.go @@ -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() diff --git a/internal/plugin/market_npm_test.go b/internal/plugin/market_npm_test.go index 556b7094b..bece7045c 100644 --- a/internal/plugin/market_npm_test.go +++ b/internal/plugin/market_npm_test.go @@ -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") diff --git a/internal/plugin/updates_test.go b/internal/plugin/updates_test.go index 5d099570d..814459e70 100644 --- a/internal/plugin/updates_test.go +++ b/internal/plugin/updates_test.go @@ -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) diff --git a/internal/provider/partners_test.go b/internal/provider/partners_test.go index b5ae5bc55..bd190cdaa 100644 --- a/internal/provider/partners_test.go +++ b/internal/provider/partners_test.go @@ -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 diff --git a/internal/provider/signin_paste_test.go b/internal/provider/signin_paste_test.go index ea99ac852..4ae3c3768 100644 --- a/internal/provider/signin_paste_test.go +++ b/internal/provider/signin_paste_test.go @@ -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) } diff --git a/internal/upstream/upstream_test.go b/internal/upstream/upstream_test.go index 66f12210d..0750959c5 100644 --- a/internal/upstream/upstream_test.go +++ b/internal/upstream/upstream_test.go @@ -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) }