Skip to content

Commit f64515c

Browse files
committed
Fix flaky test
1 parent 46a8b5f commit f64515c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,21 @@ test('queues calls beyond limit', async t => {
433433
const limit = 2;
434434
const interval = 100;
435435
const throttled = pThrottle({limit, interval})(() => Date.now());
436-
const start = Date.now();
437436

438-
const firstBatch = Promise.all([throttled(), throttled()]);
439-
await delay(50); // Ensure the first batch is within the limit
440-
const secondBatch = Promise.all([throttled(), throttled()]);
437+
// Use the actual timestamps of the first window as the anchor
438+
const firstBatchPromise = Promise.all([throttled(), throttled()]);
439+
await delay(50); // Schedule next calls within the same window
440+
const secondBatchPromise = Promise.all([throttled(), throttled()]);
441441

442-
const results = await Promise.all([firstBatch, secondBatch]);
443-
const end = Date.now();
442+
const [firstBatch, secondBatch] = await Promise.all([firstBatchPromise, secondBatchPromise]);
444443

445-
// Check that the second batch was executed after the interval
446-
for (const time of results[1]) {
447-
t.true(time - start >= interval);
448-
}
444+
const anchor = Math.min(...firstBatch);
445+
const epsilon = 5; // Allow tiny jitter from timers/clock rounding
449446

450-
t.true(end - start >= interval);
447+
// Second batch should run in the next window relative to the first
448+
for (const time of secondBatch) {
449+
t.true(time - anchor >= (interval - epsilon));
450+
}
451451
});
452452

453453
test('resets interval after inactivity', async t => {

0 commit comments

Comments
 (0)