Skip to content

fix(rate-limit): make retryAfter-accuracy tests robust to window-boundary race#2

Merged
jaredLunde merged 1 commit into
mainfrom
fix/rate-limit-window-boundary-race
May 19, 2026
Merged

fix(rate-limit): make retryAfter-accuracy tests robust to window-boundary race#2
jaredLunde merged 1 commit into
mainfrom
fix/rate-limit-window-boundary-race

Conversation

@jaredLunde
Copy link
Copy Markdown
Contributor

Summary

Fixes the `retryAfter accuracy [http/resp]` test failures that have been flaking on main. The fixed-window and sliding-window algorithms bucket by `Math.floor(now / window)` — two awaited `limit()` calls can straddle a bucket boundary on a slow CI runner, so the test's "second request is denied" precondition is racy.

Replace the immediate-denial assertion with a small bounded loop (`untilDenied`, max 6 calls) that drives the limiter until it actually denies, then runs the retryAfter+sleep verification that the test actually exists to check. Genuinely broken limiters still fail loudly via the 6-call cap.

This is a test bug, not an implementation bug — the limiter is behaving correctly when it allows a request in a new window.

Test plan

  • All 100 rate-limit tests pass locally including the 3 retryAfter-accuracy cases
  • CI passes

🤖 Generated with Claude Code

…dary race

The fixed-window and sliding-window algorithms bucket time by
Math.floor(now / window). Two awaited limit() calls back-to-back can
straddle a bucket boundary on a slow CI runner, so the precondition
"second request is denied" is racy — when the boundary slips between
calls, both requests fall into different buckets and both are allowed,
the test then fails on `expect(allowed).toBe(false)` before ever
exercising the retryAfter sleep.

Replace the immediate-denial precondition with a small bounded loop that
drives the limiter until it actually denies, then verify retryAfter+sleep
unblocks. 6-call cap so a genuinely broken limiter still fails loudly.

Tested the new helper locally: all 100 rate-limit tests pass, including
the three retryAfter-accuracy cases.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jaredLunde jaredLunde merged commit 43a8dea into main May 19, 2026
5 checks passed
@jaredLunde jaredLunde deleted the fix/rate-limit-window-boundary-race branch May 19, 2026 20:02
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.

1 participant