Cause: TestMaxRPMWaitsForRoomInTheMinute, TooLongIs429WithRetryAfter and
CountsRetriesAndCounts ran a 1.5s window on the wall clock and checked
what the limiter decided against how fast the machine got there. A second
request that came 0.5s into the window was told Retry-After 1, not the
pinned 2; one that came after the window found room (200, no header); a
count that came after it reached the vendor (4, not 3); and "didn't wait"
was a 0.75s bar on a round trip. All four shapes are in the issue, at
loads of 15 to 205.
Fix: rpm.go reads its clock and timer through rpmNow and rpmTimer
(time.Now and time.NewTimer; nothing else changes for users). The tests
hold both (holdMinute): the clock moves only when the test moves it, a
first request's dial lag moves it by `late` at the vendor, which notes
its times on the same clock, and a wait for room is a state the test sees
(minuteClock.waiting) instead of a time it measures.
- Retry-After is still exactly "2": the clock is held where the first
went, so the whole 1.5s window is left.
- "went at once" is now "never started a wait for room" (atOnce), which
is stronger than the 0.75s bar; one that waits is seen and fails at once.
- The third/last request's wait is checked to end exactly the window and
slack after the first, and the vendor still has to see it a whole window
after the first (the slack vs. dial guarantee of ce1ecaa4, now exact).
- AgentGoneWhileWaiting holds the clock too, so its 3s window can't pass
under load, and checks no wait is left behind instead of sleeping 100ms.
- Waiting for a request on a busy machine is bounded at 2 minutes only so
a broken test ends; nothing is checked against a duration. A failing
test lets the held waits go before the gateway's Close, so it can't hang.
Run (macOS, load average ~180 to 290):
- Red first, on the old tests, with a sleep injected before the second
request (RPM_INJECT, removed before commit): at 600ms WaitsForRoom
"the second waited 834ms" and TooLong "Retry-After "1" ... want 2"; at
1600ms all three: "second waited 1.79s", "second = 200 Retry-After """,
"the vendor saw 4, want the count estimated".
- Green with the same injection at 1600ms and at 5s on the new tests.
- go test -p 2 -race -count=20 -run 'TestMaxRPM' ./internal/gateway/: ok.
- Broken by hand, each red: Retry-After floored instead of ceiled
("Retry-After "1""); the slack dropped from rpmSpan (WaitsForRoom and
CountsRetries: "waits to go 1.5s ... want 1.75s"); a count with no room
waiting (gateway.go's free filter removed: "a count with no room waited
for room"); a too-long wait not refused ("the second ... waited for room
in the minute"); giveBack removed (AgentGone: "its place given back").
- gofmt -l clean; go vet ./internal/gateway/ for darwin, linux, windows.
- go test -p 2 -timeout 60m ./internal/gateway/: ok (1002s), on this tree but
for AgentGone still sleeping 100ms after an advance (since replaced by
the waiting() check). The rerun on the final tree failed to build: the
disk was full (117Mi free). The final tree passed the -race -count=20
run above.
Not tried: CI's machines, Windows.
Fixes#1791