File tree 2 files changed +35
-1
lines changed
2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -174,7 +174,7 @@ export default class ProcessingPriorityQueue {
174
174
this . process ( ) ;
175
175
}
176
176
177
- if ( this . currentConcurrencyCount <= this . concurrencyLimit && this . priorityQueue . size > 0 ) {
177
+ if ( this . currentConcurrencyCount < this . concurrencyLimit && this . priorityQueue . size > 0 ) {
178
178
this . currentConcurrencyCount ++ ;
179
179
180
180
const { value : sqItem , valid} = this . poll ( ) ;
Original file line number Diff line number Diff line change @@ -685,4 +685,38 @@ describe("PriorityTask", () => {
685
685
} ) ;
686
686
expect ( PTask . getAllPTasks ( "krombopulos" ) . length ) . toBe ( 1 ) ;
687
687
} ) ;
688
+
689
+ it ( "should not schedule more than concurrencyLimit items" , ( done ) => {
690
+ const CONCURRENCY_LIMIT = 2 ;
691
+ PTask . setConcurrencyLimit ( CONCURRENCY_LIMIT ) ;
692
+
693
+ const delayedOnRun = async ( a : number ) => {
694
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
695
+ return a ;
696
+ } ;
697
+
698
+ // Prepare tasks
699
+ const ptasks = Array . from ( { length : CONCURRENCY_LIMIT + 1 } ) . map ( ( _ , i ) => {
700
+ return new PTask < number , number > ( {
701
+ priority : i ,
702
+ args : i ,
703
+ onRun : delayedOnRun ,
704
+ } ) ;
705
+ } ) ;
706
+
707
+ // run all tasks
708
+ const promises = ptasks . map ( ( ptask ) => ptask . run ( ) ) ;
709
+
710
+ setTimeout ( ( ) => {
711
+ const runningTasks = PTask . getAllPTasks ( ) . filter ( ( ptask ) => ptask . status === "running" ) ;
712
+ const pendingTasks = PTask . getAllPTasks ( ) . filter ( ( ptask ) => ptask . status === "pending" ) ;
713
+
714
+ expect ( runningTasks . length ) . toBe ( CONCURRENCY_LIMIT ) ;
715
+ expect ( pendingTasks . length ) . toBe ( 1 ) ;
716
+
717
+ Promise . all ( promises ) . then ( ( ) => {
718
+ done ( )
719
+ } ) ;
720
+ } , 10 ) ;
721
+ } )
688
722
} ) ;
You can’t perform that action at this time.
0 commit comments