@@ -53,6 +53,76 @@ describe('Create a queue and add to it', () => {
5353 assert . equal ( q . timeout , 0 ) ;
5454 } ) ;
5555 } ) ;
56+
57+ describe ( 'with an async worker' , ( ) => {
58+ it ( 'Still respects concurrency' , ( done ) => {
59+ const q = new TimeQueue ( async ( a , b ) => {
60+ return await new Promise ( ( resolve ) => {
61+ process . nextTick ( ( ) => resolve ( a + b ) ) ;
62+ } ) ;
63+ } , { concurrency : 2 } ) ;
64+ let result1 , result2 , result3 ;
65+ q . push ( 1 , 2 , ( err , result ) => {
66+ assert . ifError ( err ) ;
67+ result1 = result ;
68+ } ) ;
69+ q . push ( 3 , 4 , ( err , result ) => {
70+ assert . ifError ( err ) ;
71+ result2 = result ;
72+ } ) ;
73+ q . push ( 5 , 6 , ( err , result ) => {
74+ assert . ifError ( err ) ;
75+ result3 = result ;
76+ } ) ;
77+ assert . equal ( q . active , 2 ) ;
78+ q . on ( 'drain' , ( ) => {
79+ assert . equal ( result1 , 3 ) ;
80+ assert . equal ( result2 , 7 ) ;
81+ assert . equal ( result3 , 11 ) ;
82+ done ( ) ;
83+ } ) ;
84+ } ) ;
85+ it ( 'Pushing to worker can be awaited' , async ( ) => {
86+ const q = new TimeQueue ( async ( a , b ) => {
87+ return new Promise ( ( resolve ) => {
88+ process . nextTick ( ( ) => resolve ( a + b ) ) ;
89+ } ) ;
90+ } , { concurrency : 1 } ) ;
91+ let result1 = await q . push ( 1 , 2 ) ;
92+ let result2 = await q . push ( 3 , 4 ) ;
93+ assert . equal ( result1 , 3 ) ;
94+ assert . equal ( result2 , 7 ) ;
95+ } ) ;
96+ describe ( 'that errors' , ( ) => {
97+ it ( 'Calls callback with error' , ( done ) => {
98+ const q = new TimeQueue ( async ( a ) => {
99+ return new Promise ( ( resolve , reject ) => {
100+ process . nextTick ( ( ) => reject ( Error ( 'no: ' + a ) ) ) ;
101+ } ) ;
102+ } ) ;
103+ q . push ( 'one' , ( err ) => {
104+ assert . ok ( err ) ;
105+ assert . equal ( err . message , 'no: one' ) ;
106+ done ( ) ;
107+ } ) ;
108+ } ) ;
109+ it ( 'Throws when awaited' , async ( ) => {
110+ const q = new TimeQueue ( async ( a ) => {
111+ return new Promise ( ( resolve , reject ) => {
112+ process . nextTick ( ( ) => reject ( Error ( 'no: ' + a ) ) ) ;
113+ } ) ;
114+ } ) ;
115+ try {
116+ await q . push ( 'okay' ) ;
117+ } catch ( err ) {
118+ assert . ok ( err ) ;
119+ assert . equal ( err . message , 'no: okay' ) ;
120+ return ;
121+ }
122+ throw Error ( 'shoult not get here' ) ;
123+ } ) ;
124+ } ) ;
125+ } ) ;
56126} ) ;
57127
58128
0 commit comments