Files
magpie/internal/provider/reads_out_test.go
T
yetone b684308c61 provider: an allowance or usage read a test leaves running ends with it (#3174)
TestClaudeAccountServesModelsListedSinceItsFetch went red in full
internal/provider runs under load (macOS CI at 5849b47ca, run
42448627522; mote; WSL): find(All(), "claude") came back empty right
after claudeSignIn.

Cause: the test before it in file order, TestClaudeAllSignedOutStaysListed,
ends with InUseLogin("claude"), whose loginRoom asks Allowances. Allowances
starts its read in a goroutine and waits for it only firstWait (3s); under
load the read runs on past the test. It reads the logins (Logins ->
liveLogin -> claudeCredential) with whatever HOME is set by then: the next
test's, after claudeHome forgot the credential cache and before
claudeSignIn wrote the sign-in. It caches "no credential" in the one
global slot for 10s, and the next test's All() is served that miss, so
there is no Claude row. skygunner's -race run on #3174 named the same
path (isolate's write against an Allowances read from that test).
SubscriptionUsage (with a stale copy to serve) and KeyAllowance leave
their reads running the same way; an instrumented full run saw all three
store into the credential cache from goroutines no test waited for.

Fix: those three reads start through goRead, which counts them
(readsOut). isolate, which every account test goes through, waits for
them before it resets anything and again as the test's last cleanup,
and fails the test if one still runs a minute later (a read gives up
after 30s). TestReadLeftOutEndsWithItsTest leaves an Allowances read
out past firstWait and checks it ended with its subtest.

Not taken: the patches offered on #3174 (ggbdpq, skygunner) forget the
credential cache after claudeSignIn's write. That hides this one victim's
symptom while the read keeps running into the next test's home and its
other caches (usedCache, logins), so the cause is left.

Run on mote (darwin arm64, go1.26.8):
- pinned repro (throwaway test, deleted): an Allowances read left out
  past a 10ms firstWait reading claudeCredential in a loop, then the
  victim; on 691a10446 the victim was red 3 of 5 with the issue's
  output (claude_models_test.go:52 ... Account:<nil>} false).
- with the fix: the repro, TestReadLeftOutEndsWithItsTest and the victim
  -count=10 -race ok.
- broken by hand (goRead as a bare go f()): TestReadLeftOutEndsWithItsTest
  red 5 of 5 ("ran on after its test"), the victim red 4 of 5.
- go test ./internal/provider -count=3 -shuffle=on (seed
  1794116678624149000): one red, TestCodexSubagentModelsKept's
  allocation ratio (filed as #5435, output added there); all else ok.
- go vet ./internal/provider on darwin, GOOS=linux -tags nogui and
  GOOS=windows: clean. gofmt -l clean.
- full suite: `lockf /Users/yetone/suite.lock go test -timeout 45m ./...` on mote at this tree (base 691a10446): every package ok, no reds (internal/provider 201s, internal/gateway 978s, internal/agent 379s). A first attempt was cut off when mote rebooted.
- after rebasing onto origin/main a5c97cd83 (which changed internal/provider): `go test ./internal/provider -count=2 -shuffle=on` on mote, seed 1794163393889037000: only TestCodexSubagentModelsKept red, the allocation count already filed as #5435.

Not tried: the loaded CI schedule itself (the repro stands in for it);
Windows and Linux test runs of the package.

Fixes #3174
2026-11-09 02:47:26 +08:00

49 lines
1.6 KiB
Go

package provider
import (
"context"
"sync/atomic"
"testing"
"time"
)
// An allowance read Allowances leaves running past firstWait ends with the
// test that started it: left to run on, it read Claude Code's sign-in in the
// next test's home before that test wrote it, cached that there was none,
// and TestClaudeAccountServesModelsListedSinceItsFetch found no Claude
// account (#3174).
func TestReadLeftOutEndsWithItsTest(t *testing.T) {
var ended atomic.Bool
// put back by the outer test: LoginUsageVia waits for the stand-in it
// replaces to return, which would wait the read out for the subtest
t.Cleanup(func() { LoginUsageVia(nil) })
t.Run("leaves a read out", func(t *testing.T) {
claudeHome(t)
usedCache.Lock()
usedCache.m, usedCache.at, usedCache.loading = map[string]map[string]Allowance{}, map[string]time.Time{}, map[string]chan struct{}{}
usedCache.Unlock()
old := firstWait
firstWait = time.Millisecond
t.Cleanup(func() { firstWait = old })
release := make(chan struct{})
LoginUsageVia(func(context.Context, string) map[string]SubscriptionQuota {
<-release
// what the read does once let go takes time a test that
// doesn't wait for it is long gone in
time.Sleep(200 * time.Millisecond)
claudeCredential()
ended.Store(true)
return nil
})
// run before isolate's: the read is let go as the test ends
t.Cleanup(func() { close(release) })
Allowances("claude")
if ended.Load() {
t.Fatal("the read ended before the test did: nothing was left out")
}
})
if !ended.Load() {
t.Fatal("the allowance read Allowances left out ran on after its test")
}
}