1+ import ConcurrentPriorityWorkerQueue from "./index" ;
2+ import { assert } from "chai" ;
3+ import sleep = require( "sleep-promise" ) ;
4+
5+ async function timeToRun ( fn : ( ) => Promise < void > ) {
6+ const start = Date . now ( ) ;
7+ await fn ( ) ;
8+ return Date . now ( ) - start ;
9+ }
10+
11+ describe ( "Tests" , ( ) => {
12+ it ( "Empty worker" , function ( ) {
13+ const queue = new ConcurrentPriorityWorkerQueue < number , number > ( { worker : async ( x ) => x } ) ;
14+ assert . equal ( queue . length , 0 , "Queue length is zero" ) ;
15+ assert . equal ( queue . determineNextPosition ( 0 ) , 0 , "Next position is 0" ) ;
16+ assert . isFalse ( queue . willQueue ( ) , "Will queue is false" ) ;
17+ } )
18+ it ( "Worker with single concurrency" , async function ( ) {
19+ const queue = new ConcurrentPriorityWorkerQueue < number , number > ( { worker : async ( x ) => {
20+ await sleep ( x )
21+ return 1 ;
22+ } } ) ;
23+ const promises : Promise < any > [ ] = [ ] ;
24+ promises . push ( ( async function ( ) {
25+ const timePromise = timeToRun ( async ( ) => { await queue . enqueue ( 1000 , 0 ) } )
26+ // While that's running, do some more tests
27+ assert . equal ( queue . length , 0 , "Queue length is 0" ) ;
28+ assert . equal ( queue . determineNextPosition ( 0 ) , 1 , "Next position is 1" ) ;
29+ assert . isTrue ( queue . willQueue ( ) , "Will queue is true" ) ;
30+ const time = await timePromise
31+ assert . isTrue ( time > 1000 )
32+ } ) ( ) )
33+ promises . push ( ( async function ( ) {
34+ const timePromise = timeToRun ( async ( ) => { await queue . enqueue ( 5000 , 0 ) } )
35+ // While that's running, do some more tests
36+ assert . equal ( queue . length , 1 , "Queue length is 1" ) ;
37+ assert . equal ( queue . determineNextPosition ( 0 ) , 2 , "Next position is 2" ) ;
38+ const time = await timePromise
39+ assert . isTrue ( time > 5900 )
40+ } ) ( ) )
41+ await Promise . all ( promises )
42+ } )
43+ it ( "Worker with double concurrency" , async function ( ) {
44+ const queue = new ConcurrentPriorityWorkerQueue < number , number > ( { worker : async ( x ) => await sleep ( x ) , limit : 2 } ) ;
45+ const promises : Promise < any > [ ] = [ ] ;
46+ promises . push ( ( async function ( ) {
47+ const timePromise = timeToRun ( async ( ) => { await queue . enqueue ( 1000 , 0 ) } )
48+ // While that's running, do some more tests
49+ assert . equal ( queue . length , 0 , "Queue length is 0" ) ;
50+ assert . equal ( queue . determineNextPosition ( 0 ) , 0 , "Next position is 0" ) ;
51+ assert . isFalse ( queue . willQueue ( ) , "Will queue is false" ) ;
52+ const time = await timePromise
53+ assert . isTrue ( time > 1000 )
54+ } ) ( ) )
55+ promises . push ( ( async function ( ) {
56+ const timePromise = timeToRun ( async ( ) => { await queue . enqueue ( 5000 , 0 ) } )
57+ // While that's running, do some more tests
58+ assert . equal ( queue . length , 0 , "Queue length is 0" ) ;
59+ assert . equal ( queue . determineNextPosition ( 0 ) , 1 , "Next position is 1" )
60+ const time = await timePromise
61+ assert . isTrue ( time > 5000 && time < 5900 )
62+ } ) ( ) )
63+ await Promise . all ( promises )
64+ } )
65+ it ( "Worker with single concurrency and different priorities" , async function ( ) {
66+ const data : number [ ] = [ ] ;
67+ const queue = new ConcurrentPriorityWorkerQueue < number , number > ( { worker : async ( x ) => {
68+ await sleep ( x )
69+ return data . push ( x )
70+ } } ) ;
71+ await Promise . all ( [ [ 1000 , 0 ] , [ 1500 , 0 ] , [ 2000 , 1 ] ] . map ( async ( [ time , priority ] ) => {
72+ await queue . enqueue ( time , priority )
73+ } ) )
74+ assert . deepEqual < number [ ] > ( data , [ 1000 , 2000 , 1500 ] , "Data is [1000, 2000, 1500]" )
75+ } )
76+ it ( "Worker with double concurrency and different priorities" , async function ( ) {
77+ const data : number [ ] = [ ] ;
78+ const queue = new ConcurrentPriorityWorkerQueue < number , number > ( { worker : async ( x ) => {
79+ await sleep ( x )
80+ return data . push ( x )
81+ } , limit : 2 } ) ;
82+ const promises : Promise < any > [ ] = [ ] ;
83+ promises . push ( ( async function ( ) {
84+ const timePromise = queue . enqueue ( 500 , 0 )
85+ // While that's running, do some more tests
86+ assert . equal ( queue . length , 0 , "Queue length is 0" ) ;
87+ assert . equal ( queue . determineNextPosition ( 0 ) , 0 , "Next position is 0" ) ;
88+ assert . isFalse ( queue . willQueue ( ) , "Will queue is false" ) ;
89+ await timePromise
90+ } ) ( ) )
91+ promises . push ( ( async function ( ) {
92+ const timePromise = queue . enqueue ( 1000 , 1 )
93+ // While that's running, do some more tests
94+ assert . equal ( queue . length , 0 , "Queue length is 0" ) ;
95+ assert . equal ( queue . determineNextPosition ( 0 ) , 1 , "Next position is 1" )
96+ assert . equal ( queue . determineNextPosition ( 1 ) , 1 , "Next position is 1" )
97+ await timePromise
98+ } ) ( ) )
99+ promises . push ( ( async function ( ) {
100+ const timePromise = queue . enqueue ( 2000 , 0 )
101+ // While that's running, do some more tests
102+ assert . equal ( queue . length , 1 , "Queue length is 1" ) ;
103+ assert . equal ( queue . determineNextPosition ( 0 ) , 2 , "Next position is 2" )
104+ assert . equal ( queue . determineNextPosition ( 1 ) , 1 , "Next position is 1" )
105+ await timePromise
106+ } ) ( ) )
107+ promises . push ( ( async function ( ) {
108+ await queue . enqueue ( 1500 , 1 )
109+ } ) ( ) )
110+ await Promise . all ( promises )
111+ assert . deepEqual < number [ ] > ( data , [ 500 , 1000 , 1500 , 2000 ] , "Data is [500, 1000, 1500, 2000]" )
112+ } )
113+ } )
0 commit comments