From 7b151cf5491cc99e71c47ca5a67a6b198ecae0dc Mon Sep 17 00:00:00 2001 From: nrenvo Date: Sun, 11 Oct 2026 20:46:13 +0800 Subject: [PATCH] provider tests: the stale copy's prompt return is asserted by a held refresh, not a 40ms bound (#1586) TestSubscriptionUsageServesStale said "a stale copy comes back at once while it refreshes" by timing the call: a 50ms ctx and a 40ms bound on time.Since(start). On a loaded machine the bound is what fails -- the report has "waited 311.332917ms for a stale copy" -- so the test said nothing either way about the guarantee it guards. Which step of the call took those 311ms I did not establish; the assertion no longer needs to know, because nothing is timed. The refresh is now held at the vendor instead: CopilotUserURL points at an httptest server whose handler blocks until released, so the refresh cannot finish. The copy is asserted to come back while SubscriptionUsageReading still reports a refresh in flight, which is the guarantee itself rather than a proxy for it, and <-reached proves a refresh really went out, so the test cannot pass by never starting one. The test also resets c.asked, the one field of the cache it primes that it had left alone. It is not set when this test runs -- AskUsage is the only setter and quota_asked_test.go's own restart clears it -- so this is not the reported failure's cause; it completes what the test already does to c.data, c.at and c.pending, so the test does not depend on another file's cleanup. What was run: gofmt -l internal/provider/subscription_usage_test.go (no output) go test -tags nogui -count=1 -run TestSubscriptionUsageServesStale ./internal/provider go test -race -count=20 -run TestSubscriptionUsageServesStale ./internal/provider go test -tags nogui -count=1 ./internal/provider (whole package) go vet ./... ; GOOS=linux go vet -tags nogui ./... ; GOOS=windows go vet ./... go build ; GOOS=linux go build -tags nogui ; GOOS=windows go build go test -tags nogui ./... (53 of 54 packages ok; internal/gateway reds only in the parallel run, which this change cannot reach -- it does not compile this file -- and which is filed already as #1543/#1544) Without the change: with SubscriptionUsage's "if !have && pending != nil" broken to "if have && pending != nil" -- a copy that has data waiting for the pending refresh -- the new test fails with subscription_usage_test.go:97: got [], want only claude: a copy that waited for the held refresh comes back with nothing --- FAIL: TestSubscriptionUsageServesStale (12.51s) Reviewed and landed from nrenvo/magpie fix/1586-stale-copy-test (b5bda612), posted on #1586 because PRs are collaborators-only. Run at the cherry-pick on origin/main 398f8469b, temp HOME, caches pinned, load average ~120-135: go test -tags nogui -race -count=50 -run 'TestSubscriptionUsageServesStale$' ./internal/provider ok go test -tags nogui -race -count=20 -cpu 1,2 -run TestSubscriptionUsage ./internal/provider ok go test -tags nogui -count=1 ./internal/provider ok 149.7s go vet, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/provider; gofmt -l clean Broken by hand in SubscriptionUsage, two ways, each red: "if !have && pending != nil" -> "if pending != nil" (a stale read waits for the refresh, up to ctx): FAIL at :96, got [] (11.6s) an unbounded <-pending for every read: FAIL at :96, got the Copilot card read after the held refresh gave up (21s) --- internal/provider/subscription_usage_test.go | 47 +++++++++++++++----- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/internal/provider/subscription_usage_test.go b/internal/provider/subscription_usage_test.go index b2841d9d3..10e2ba8b3 100644 --- a/internal/provider/subscription_usage_test.go +++ b/internal/provider/subscription_usage_test.go @@ -6,6 +6,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "sync" "testing" "time" ) @@ -44,9 +45,29 @@ func TestSubscriptionUsageServesStale(t *testing.T) { if err := Delete("codex"); err != nil { t.Fatal(err) } + // The refresh is held at the vendor, so a copy that waited for it could + // not come back: what the stale copy returns, it returns while the + // refresh is still in flight (#1586). Held rather than timed, because a + // loaded machine makes a wall-clock bound say nothing either way. + held := make(chan struct{}) + reached := make(chan struct{}) + var releaseOnce, reachedOnce sync.Once + release := func() { releaseOnce.Do(func() { close(held) }) } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + reachedOnce.Do(func() { close(reached) }) + <-held + w.Write([]byte(`{"copilot_plan":"individual","token_based_billing":true,"quota_snapshots":` + + `{"premium_interactions":{"has_quota":true,"unlimited":false,"entitlement":"300","percent_remaining":50}}}`)) + })) + oldUser := CopilotUserURL + CopilotUserURL = srv.URL + t.Cleanup(func() { CopilotUserURL = oldUser }) + c := &subscriptionUsageCache c.Lock() - c.at, c.pending = time.Now().Add(-time.Hour), nil + // all four fields the call reads: asked is part of the state this test + // primes, not something to inherit from whatever ran before it + c.at, c.asked, c.pending = time.Now().Add(-time.Hour), false, nil c.data = []SubscriptionQuota{{Provider: "claude", Name: "Claude Code"}, {Provider: "codex", Name: "Codex"}} c.Unlock() t.Cleanup(func() { @@ -62,21 +83,24 @@ func TestSubscriptionUsageServesStale(t *testing.T) { <-p } }) + // before the wait above, which the held vendor would otherwise keep + // blocked, and before the process is left with a server nobody closes + t.Cleanup(func() { release(); srv.Close() }) - ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) + // The deadline is only so a copy that did wait fails as a FAIL rather + // than hanging the run: nothing here is measured against it. + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - start := time.Now() got := SubscriptionUsage(ctx) - if time.Since(start) > 40*time.Millisecond { - t.Fatalf("waited %v for a stale copy", time.Since(start)) - } if len(got) != 1 || got[0].Provider != "claude" { - t.Fatalf("got %+v, want only claude", got) + t.Fatalf("got %+v, want only claude: a copy that waited for the held refresh comes back with nothing", got) } - c.Lock() - refreshing := c.pending != nil || time.Since(c.at) < time.Minute - c.Unlock() - if !refreshing { + if !SubscriptionUsageReading() { + t.Fatal("the copy came back with no refresh in flight") + } + select { + case <-reached: + case <-time.After(15 * time.Second): t.Fatal("a stale copy did not start a refresh") } // what showed the stale copy hears when the new one lands @@ -88,6 +112,7 @@ func TestSubscriptionUsageServesStale(t *testing.T) { } }) t.Cleanup(func() { OnSubscriptionUsage(nil) }) + release() c.Lock() p := c.pending c.at = time.Time{}