gateway tests: TestClaudeRunEndsAfterStructuredOutput waits for the bridge to let its runs go, not only their processes

CI's macOS race shard failed it on main at c8c3ce9a9 ("2 runs kept, want
only the one waiting on read"). A run leaves s.subscription.runs in
removeRun, after its claude process is reaped, so the test's count read
straight after leftover() saw the processes gone and the runs still held.
The count is now polled for up to 3s, as the processes are.

Run, in a worktree on origin/main 62378c5f8, sandbox HOME:
- Cause: a 300ms sleep injected before removeRun turns the old test red
  3 of 3 ("9 runs kept"); with this change it passes 3 of 3 with the
  sleep still in.
- Guarantee: removeRun skipped by hand, the new test is red ("9 runs
  kept, want only the one waiting on read").
- go test -tags nogui -race -count=10 -run
  'TestClaudeRunEndsAfterStructuredOutput$|TestClaudeParkedRunsAreBounded|TestClaudeIdleRunKept'
  ./internal/gateway: ok. gofmt -l clean; go vet -tags nogui
  ./internal/gateway clean.
Test-only; the bridge's own code is unchanged.
This commit is contained in:
yetone
2026-10-14 15:37:56 +08:00
parent d4a9cb5cb0
commit c4c3cafa27
@@ -147,14 +147,28 @@ func TestClaudeRunEndsAfterStructuredOutput(t *testing.T) {
if !alive(read) {
t.Fatalf("the run waiting on a read result was let go")
}
s.subscription.mu.Lock()
runs := len(s.subscription.runs)
s.subscription.mu.Unlock()
if runs != 1 {
// A run is taken off the bridge after its process is reaped, so the
// count is polled too: under -race it lags the processes' end.
if runs := keptRuns(s, 1, 3*time.Second); runs != 1 {
t.Fatalf("%d runs kept, want only the one waiting on read", runs)
}
}
// keptRuns is how many runs the bridge holds once it holds want, or after
// within, polled.
func keptRuns(s *Server, want int, within time.Duration) int {
deadline := time.Now().Add(within)
for {
s.subscription.mu.Lock()
n := len(s.subscription.runs)
s.subscription.mu.Unlock()
if n == want || time.Now().After(deadline) {
return n
}
time.Sleep(20 * time.Millisecond)
}
}
// Runs whose callers stopped with their tool calls unanswered are let go
// past parkedMost, the longest waiting first; the one that just asked is
// kept.