Repository navigation
test: stop the GET rate-limit test flaking on a minute boundary - #289
Merged
Merged
Conversation
The limiter buckets requests as `window:<ip>:<method>:<floor(Date.now()/60000)`. The test fired 241 GETs in a tight loop and asserted the 241st returned 429, but if the wall clock ticked into the next minute partway through, the counter moved to a fresh bucket and the cap was never crossed — the loop finished on 200 and the test failed depending only on when it happened to run. Pin Date.now to a fixed instant inside one minute so the run cannot straddle the boundary, and add a companion test that moves the clock deliberately to prove the limit does reset on a new minute.
Deploying openmouse with
|
| Latest commit: |
9f60e82
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://04c2a08d.openmouse.pages.dev |
| Branch Preview URL: | https://fix-ratelimit-test-minute-bo.openmouse.pages.dev |
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.
What
The GET rate-limit test fired 241 requests in a tight loop and asserted the 241st returned 429. It depends on the wall clock.
functions/_middleware.jsbuckets requests as:If the clock ticks into the next minute partway through the loop, the counter moves to a fresh bucket, the cap is never crossed, and the loop finishes on 200 — the test then fails depending only on when it happened to run. That is why it passed on retry both times it was seen.
Fix
Date.nowto a fixed instant inside a single minute so the run cannot straddle the boundary. Behaviour is unchanged (cap is 240 GETs/min, the 241st crosses it); only the clock is frozen, and it is restored in afinally.the GET rate limit resets on a new minute, which moves the clock deliberately and proves the reset — turning the race the old test tripped over into an asserted behaviour.Verification
tsc --noEmitclean.Test-only change; no production code touched.