Repository navigation
test: make rate limiter tests deterministic with an injectable clock - #65
Merged
Merged
Conversation
Give RateLimiter an unexported now func (defaulting to time.Now) and drive refill tests from a fake clock instead of real sleeps, so CI sleep overshoot can no longer refill extra tokens. Wait tests that still use real time assert only bounds that scheduling delays cannot violate. Closes #35 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #35
TestTryAcquire/refills_tokens_correctlyslept 55ms and asserted exactly one token refilled. When the runner oversleeps by about 45ms, a second token refills and the assertion fails.RateLimitergets an unexportednow func() time.Time, defaulting totime.Now, whichtryAcquirereads. The public API and production behaviour are unchanged.fakeClockand anewFakeClockLimiterhelper. Refill tests advance the clock instead of sleeping, so the token counts are exact.refills_tokens_correctlynow also checks the boundary: no token at 49ms, one at 50ms.ratelimit_test.gofor the same problem:allows burst up to limitused to assert each call took <10ms.refills tokens over timeused to sleep 60ms and assert <10ms.handles concurrent requests safelyused to assert>= 10. With the clock frozen it now asserts exactly 10.waits when limit exceededused to assert a real-time lower bound, which a stall before the thirdWaitcould break. It now checks thatWaitblocks while the clock is frozen and returns afterAdvance.Wait's realtime.Afterpolling:respects context cancellation: bound widened from <100ms to <5s. Refill is 1h, so returning at all proves cancellation.maintains rate limit under load: asserts only a lower bound, which scheduling delays can't break.Waitthat should return right away use a 5s guard context, so a regression fails the test instead of hanging it.Deflake evidence (macOS,
-race -count=200,-run 'TestTryAcquire|TestRateLimiter|TestNewRateLimiter')-cpu 1+ 2×ncpu busy loops-cpu 1, 8×ncpu busy loops, 4 concurrent test processesratelimit_test.go:197(the line in the issue)Local CPU load did not reproduce the CI overshoot. Injecting the overshoot directly shows the failure from the issue, and the refill tests no longer sleep at all. The rewritten tests also cut the package's run time for these tests from about 1.5s to about 1s per iteration.
Checklist
./scripts/ci.shpasses locally (orgo test -race ./...at minimum). I rango build,go vet,go test -race ./...and gosec v2.28.0, all clean.feat:,fix:,docs:,chore:,ci:,test:)cu docs markdown --dir docs/site/commands): n/a🤖 Generated with Claude Code