Skip to content

Commit 2cf6db9

Browse files
authored
Fix timing-fragile Valkey rate-limit boundary test (#7302)
# Description of Changes Fix timing-fragile Valkey rate-limit boundary test --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### Translations (if applicable) - [ ] I ran [`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have run `task check` to verify linters, typechecks, and tests pass - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#7-testing) for more details.
1 parent fa2eb67 commit 2cf6db9

1 file changed

Lines changed: 29 additions & 9 deletions

File tree

app/proprietary/src/test/java/stirling/software/proprietary/cluster/valkey/LiveValkeyIntegrationTest.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,24 +298,44 @@ void rateLimitNoBoundaryDoubling() throws InterruptedException {
298298
ValkeyRateLimitStore store = newRateLimitStore(factoryA);
299299
String key = "boundary-" + java.util.UUID.randomUUID();
300300
long capacity = 5;
301-
Duration window = Duration.ofMillis(500);
301+
// refillGreedy tops the bucket up continuously, one token every window/capacity. A 500ms
302+
// window left the drain loop only 100ms before a 6th token appeared, so a slow Valkey
303+
// round-trip broke the count; 4s spaces refills 800ms apart, clear of any burst.
304+
Duration window = Duration.ofSeconds(4);
305+
long refillIntervalMs = window.toMillis() / capacity;
302306

307+
long drainStart = System.nanoTime();
303308
int firstAllowed = 0;
304309
for (int i = 0; i < 10; i++) {
305310
if (store.tryConsume(key, capacity, window).allowed()) firstAllowed++;
306311
}
307-
assertEquals(capacity, firstAllowed, "must allow exactly capacity tokens initially");
308-
309-
Thread.sleep(window.toMillis() + 50);
312+
long drainMs = (System.nanoTime() - drainStart) / 1_000_000;
313+
// Refill never pauses, so a slow drain earns extra tokens honestly - allow exactly the
314+
// number the elapsed time can have produced and no more.
315+
long earned = drainMs / refillIntervalMs;
316+
assertTrue(
317+
firstAllowed >= capacity && firstAllowed <= capacity + earned,
318+
"initial burst must be capacity ("
319+
+ capacity
320+
+ ") plus at most the "
321+
+ earned
322+
+ " token(s) refilled during a "
323+
+ drainMs
324+
+ "ms drain, got "
325+
+ firstAllowed);
326+
327+
// A fixed-window limiter would hand back a whole fresh capacity at the boundary; a token
328+
// bucket hands back one token per refill interval.
329+
Thread.sleep(refillIntervalMs + 200);
310330
int secondAllowed = 0;
311-
long start = System.nanoTime();
312-
for (int i = 0; i < 20 && (System.nanoTime() - start) < 20_000_000L; i++) {
331+
for (int i = 0; i < 10; i++) {
313332
if (store.tryConsume(key, capacity, window).allowed()) secondAllowed++;
314333
}
315334
assertTrue(
316-
secondAllowed <= capacity,
317-
"token-bucket must not let a fresh full capacity be consumed instantly across"
318-
+ " the boundary; got "
335+
secondAllowed >= 1 && secondAllowed < capacity,
336+
"one refill interval must yield about one token, not a fresh full window of "
337+
+ capacity
338+
+ "; got "
319339
+ secondAllowed);
320340
}
321341

0 commit comments

Comments
 (0)