@@ -433,21 +433,21 @@ test('queues calls beyond limit', async t => {
433
433
const limit = 2 ;
434
434
const interval = 100 ;
435
435
const throttled = pThrottle ( { limit, interval} ) ( ( ) => Date . now ( ) ) ;
436
- const start = Date . now ( ) ;
437
436
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 ( ) ] ) ;
441
441
442
- const results = await Promise . all ( [ firstBatch , secondBatch ] ) ;
443
- const end = Date . now ( ) ;
442
+ const [ firstBatch , secondBatch ] = await Promise . all ( [ firstBatchPromise , secondBatchPromise ] ) ;
444
443
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
449
446
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
+ }
451
451
} ) ;
452
452
453
453
test ( 'resets interval after inactivity' , async t => {
0 commit comments