@@ -1354,10 +1354,10 @@ test('interval should be maintained when using await between adds (issue #182)',
1354
1354
} ) ;
1355
1355
1356
1356
// Check intervals between tasks
1357
- for ( let i = 1 ; i < timestamps . length ; i ++ ) {
1358
- const interval = timestamps [ i ] - timestamps [ i - 1 ] ;
1357
+ for ( let index = 1 ; index < timestamps . length ; index ++ ) {
1358
+ const interval = timestamps [ index ] - timestamps [ index - 1 ] ;
1359
1359
// Allow 10ms tolerance for timing
1360
- t . true ( interval >= 90 , `Interval between task ${ i } and ${ i + 1 } was ${ interval } ms, expected >= 90ms` ) ;
1360
+ t . true ( interval >= 90 , `Interval between task ${ index } and ${ index + 1 } was ${ interval } ms, expected >= 90ms` ) ;
1361
1361
}
1362
1362
} ) ;
1363
1363
@@ -1393,9 +1393,9 @@ test('interval maintained when queue becomes empty multiple times', async t => {
1393
1393
} ) ;
1394
1394
1395
1395
// Check all intervals
1396
- for ( let i = 1 ; i < timestamps . length ; i ++ ) {
1397
- const interval = timestamps [ i ] - timestamps [ i - 1 ] ;
1398
- t . true ( interval >= 90 , `Interval between task ${ i } and ${ i + 1 } was ${ interval } ms, expected >= 90ms` ) ;
1396
+ for ( let index = 1 ; index < timestamps . length ; index ++ ) {
1397
+ const interval = timestamps [ index ] - timestamps [ index - 1 ] ;
1398
+ t . true ( interval >= 90 , `Interval between task ${ index } and ${ index + 1 } was ${ interval } ms, expected >= 90ms` ) ;
1399
1399
}
1400
1400
} ) ;
1401
1401
@@ -1614,8 +1614,8 @@ test('process exits cleanly after interval tasks complete', async t => {
1614
1614
1615
1615
// Execute tasks that complete quickly with long interval
1616
1616
const tasks = [ ] ;
1617
- for ( let i = 0 ; i < 4 ; i ++ ) {
1618
- tasks . push ( queue . add ( ( ) => `result-${ i } ` ) ) ;
1617
+ for ( let index = 0 ; index < 4 ; index ++ ) {
1618
+ tasks . push ( queue . add ( ( ) => `result-${ index } ` ) ) ;
1619
1619
}
1620
1620
1621
1621
await Promise . all ( tasks ) ;
@@ -1625,3 +1625,32 @@ test('process exits cleanly after interval tasks complete', async t => {
1625
1625
// This ensures both intervalId and timeoutId are cleared when idle
1626
1626
t . pass ( ) ;
1627
1627
} ) ;
1628
+
1629
+ test ( 'intervalCap should be respected with high concurrency (issue #126)' , async t => {
1630
+ const queue = new PQueue ( {
1631
+ concurrency : 5000 ,
1632
+ intervalCap : 1000 ,
1633
+ interval : 1000 ,
1634
+ carryoverConcurrencyCount : true ,
1635
+ } ) ;
1636
+
1637
+ const results : number [ ] = [ ] ;
1638
+ const startTime = Date . now ( ) ;
1639
+
1640
+ // Add 5000 tasks that complete immediately
1641
+ const promises = [ ] ;
1642
+ for ( let index = 0 ; index < 5000 ; index ++ ) {
1643
+ promises . push ( queue . add ( async ( ) => {
1644
+ results . push ( Date . now ( ) - startTime ) ;
1645
+ } ) ) ;
1646
+ }
1647
+
1648
+ await Promise . all ( promises ) ;
1649
+
1650
+ // Check that no more than intervalCap tasks started in the first interval
1651
+ const firstInterval = results . filter ( timestamp => timestamp < 1000 ) ;
1652
+ t . true ( firstInterval . length <= 1000 , `Expected ≤1000 tasks in first interval, got ${ firstInterval . length } ` ) ;
1653
+
1654
+ // Check that tasks actually completed (basic sanity check)
1655
+ t . is ( results . length , 5000 , 'All tasks should complete' ) ;
1656
+ } ) ;
0 commit comments