Skip to content

test: make rate limiter tests deterministic with an injectable clock - #65

Merged
timimsms merged 1 commit into
mainfrom
test/deterministic-ratelimit
Nov 18, 2026
Merged

timimsms merged 1 commit into
mainfrom
test/deterministic-ratelimit

Conversation

@timimsms

Copy link
Copy Markdown
Owner

Summary

Closes #35

TestTryAcquire/refills_tokens_correctly slept 55ms and asserted exactly one token refilled. When the runner oversleeps by about 45ms, a second token refills and the assertion fails.

  • RateLimiter gets an unexported now func() time.Time, defaulting to time.Now, which tryAcquire reads. The public API and production behaviour are unchanged.
  • The tests add a small fakeClock and a newFakeClockLimiter helper. Refill tests advance the clock instead of sleeping, so the token counts are exact. refills_tokens_correctly now also checks the boundary: no token at 49ms, one at 50ms.
  • I checked every other test in ratelimit_test.go for the same problem:
    • Moved to the fake clock:
      • allows burst up to limit used to assert each call took <10ms.
      • refills tokens over time used to sleep 60ms and assert <10ms.
      • handles concurrent requests safely used to assert >= 10. With the clock frozen it now asserts exactly 10.
      • waits when limit exceeded used to assert a real-time lower bound, which a stall before the third Wait could break. It now checks that Wait blocks while the clock is frozen and returns after Advance.
    • Still on real time, because they test Wait's real time.After polling:
      • 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.
  • Calls to Wait that 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')

Run main this branch
No load 200/200 200/200
-cpu 1 + 2×ncpu busy loops 200/200 200/200
-cpu 1, 8×ncpu busy loops, 4 concurrent test processes 200/200 200/200
45ms overshoot injected (55ms sleeps patched to 100ms) 0/20, fails at ratelimit_test.go:197 (the line in the issue) n/a: the refill tests no longer sleep

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.sh passes locally (or go test -race ./... at minimum). I ran go build, go vet, go test -race ./... and gosec v2.28.0, all clean.
  • Commit messages use conventional prefixes (feat:, fix:, docs:, chore:, ci:, test:)
  • CLI docs regenerated if command help text changed (cu docs markdown --dir docs/site/commands): n/a
  • Docs updated if user-facing behavior changed: n/a, test-only plus an unexported field

🤖 Generated with Claude Code

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>
@timimsms
timimsms merged commit 7cb833d into main Nov 18, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: deflake TestTryAcquire/refills_tokens_correctly (timing-sensitive)

1 participant