File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ export default class ProcessingPriorityQueue {
47
47
48
48
public setConcurrencyLimit ( concurrencyLimit : number ) : void {
49
49
this . concurrencyLimit = concurrencyLimit ;
50
+ this . process ( )
50
51
}
51
52
52
53
public async enqueue ( ptask : PTask < any , any > ) : Promise < any > {
Original file line number Diff line number Diff line change @@ -721,4 +721,42 @@ describe("PriorityTask", () => {
721
721
} ) ;
722
722
} , 10 ) ;
723
723
} ) ;
724
+
725
+ it ( "should start previously schedules tasks when concurrencyLimit is increased" , ( done ) => {
726
+ const CONCURRENCY_LIMIT = 2 ;
727
+ const QUEUE_NAME = 'pokeymans'
728
+ PTask . setConcurrencyLimit ( CONCURRENCY_LIMIT , QUEUE_NAME ) ;
729
+
730
+ const delayedOnRun = async ( a : number ) => {
731
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
732
+ return a ;
733
+ } ;
734
+
735
+ // Prepare tasks
736
+ const ptasks = Array . from ( { length : CONCURRENCY_LIMIT + 1 } ) . map ( ( _ , i ) => {
737
+ return new PTask < number , number > ( {
738
+ priority : i ,
739
+ args : i ,
740
+ onRun : delayedOnRun ,
741
+ queueName : QUEUE_NAME ,
742
+ } ) ;
743
+ } ) ;
744
+
745
+ // run all tasks
746
+ const promises = ptasks . map ( ( ptask ) => ptask . run ( ) ) ;
747
+
748
+ setTimeout ( ( ) => {
749
+ PTask . setConcurrencyLimit ( CONCURRENCY_LIMIT + 1 , QUEUE_NAME ) ;
750
+
751
+ const runningTasks = PTask . getAllPTasks ( QUEUE_NAME ) . filter ( ( ptask ) => ptask . status === "running" ) ;
752
+ const pendingTasks = PTask . getAllPTasks ( QUEUE_NAME ) . filter ( ( ptask ) => ptask . status === "pending" ) ;
753
+
754
+ expect ( runningTasks . length ) . toBe ( CONCURRENCY_LIMIT + 1 ) ;
755
+ expect ( pendingTasks . length ) . toBe ( 0 ) ;
756
+
757
+ Promise . all ( promises ) . then ( ( ) => {
758
+ done ( )
759
+ } ) ;
760
+ } , 10 ) ;
761
+ } ) ;
724
762
} ) ;
You can’t perform that action at this time.
0 commit comments