fix(rate-limit): make retryAfter-accuracy tests robust to window-boundary race#2
Merged
Merged
Conversation
…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>
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
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
🤖 Generated with Claude Code