@@ -6,6 +6,7 @@ import delay from 'delay';
66import timeSpan from 'time-span' ;
77import randomInt from 'random-int' ;
88import pDefer from 'p-defer' ;
9+ import { TimeoutError } from 'p-timeout' ;
910import PQueue from '../source/index.js' ;
1011
1112const fixture = Symbol ( 'fixture' ) ;
@@ -131,65 +132,91 @@ test('.add() - priority defaults to 0 when undefined', async () => {
131132 assert . deepEqual ( result , [ 'first' , 'priority' , 'second' , 'third' ] ) ;
132133} ) ;
133134
134- test ( '.add() - timeout without throwing' , async ( ) => {
135- const result : string [ ] = [ ] ;
136- const queue = new PQueue ( { timeout : 300 , throwOnTimeout : false } ) ;
137- queue . add ( async ( ) => {
138- await delay ( 400 ) ;
139- result . push ( '🐌' ) ;
140- } ) ;
141- queue . add ( async ( ) => {
142- await delay ( 250 ) ;
143- result . push ( '🦆' ) ;
144- } ) ;
145- queue . add ( async ( ) => {
146- await delay ( 310 ) ;
147- result . push ( '🐢' ) ;
148- } ) ;
149- queue . add ( async ( ) => {
150- await delay ( 100 ) ;
151- result . push ( '🐅' ) ;
152- } ) ;
153- queue . add ( async ( ) => {
154- result . push ( '⚡️' ) ;
135+ test ( '.add() - timeout always throws' , async ( ) => {
136+ const queue = new PQueue ( { timeout : 300 } ) ;
137+ const errors : unknown [ ] = [ ] ;
138+
139+ // Task that will timeout
140+ await assert . rejects (
141+ queue . add ( async ( ) => {
142+ await delay ( 400 ) ;
143+ return '🐌' ;
144+ } ) ,
145+ TimeoutError ,
146+ 'Task exceeding timeout should throw TimeoutError' ,
147+ ) ;
148+
149+ // Task that completes within timeout
150+ const result = await queue . add ( async ( ) => {
151+ await delay ( 200 ) ;
152+ return '🦆' ;
155153 } ) ;
154+
155+ assert . equal ( result , '🦆' , 'Task within timeout should complete normally' ) ;
156+
157+ // Test with very short timeout
158+ await assert . rejects (
159+ queue . add ( async ( ) => delay ( 100 ) , { timeout : 10 } ) ,
160+ TimeoutError ,
161+ 'Short timeout should throw' ,
162+ ) ;
163+
156164 await queue . onIdle ( ) ;
157- assert . deepEqual ( result , [ '⚡️' , '🐅' , '🦆' ] ) ;
158165} ) ;
159166
160- test . skip ( '.add() - timeout with throwing' , async ( ) => {
161- const result : string [ ] = [ ] ;
162- const queue = new PQueue ( { timeout : 300 , throwOnTimeout : true } ) ;
163- await assert . rejects ( queue . add ( async ( ) => {
164- await delay ( 400 ) ;
165- result . push ( '🐌' ) ;
166- } ) ) ;
167- queue . add ( async ( ) => {
167+ test ( '.add() - timeout behavior' , async ( ) => {
168+ const queue = new PQueue ( { timeout : 300 } ) ;
169+
170+ // Test multiple timeouts
171+ await assert . rejects (
172+ queue . add ( async ( ) => {
173+ await delay ( 400 ) ;
174+ return '🐌' ;
175+ } ) ,
176+ TimeoutError ,
177+ ) ;
178+
179+ // Task that completes
180+ const result = await queue . add ( async ( ) => {
168181 await delay ( 200 ) ;
169- result . push ( '🦆' ) ;
182+ return '🦆' ;
170183 } ) ;
184+ assert . equal ( result , '🦆' ) ;
185+
186+ // Test timeout override
187+ const longResult = await queue . add ( async ( ) => {
188+ await delay ( 400 ) ;
189+ return '🐢' ;
190+ } , { timeout : 500 } ) ;
191+ assert . equal ( longResult , '🐢' , 'Task should complete with extended timeout' ) ;
192+
171193 await queue . onIdle ( ) ;
172- assert . deepEqual ( result , [ '🦆' ] ) ;
173194} ) ;
174195
175196test ( '.add() - change timeout in between' , async ( ) => {
176197 const result : string [ ] = [ ] ;
177198 const initialTimeout = 50 ;
178199 const newTimeout = 200 ;
179- const queue = new PQueue ( { timeout : initialTimeout , throwOnTimeout : false , concurrency : 2 } ) ;
180- queue . add ( async ( ) => {
200+ const queue = new PQueue ( { timeout : initialTimeout , concurrency : 2 } ) ;
201+
202+ // This task will timeout with initial timeout of 50ms
203+ await assert . rejects ( queue . add ( async ( ) => {
181204 const { timeout} = queue ;
182205 assert . equal ( timeout , initialTimeout ) ;
183206 await delay ( 300 ) ;
184207 result . push ( '🐌' ) ;
185- } ) ;
208+ } ) , TimeoutError ) ;
209+
186210 queue . timeout = newTimeout ;
187- queue . add ( async ( ) => {
211+
212+ // This task will complete within the new timeout of 200ms
213+ await queue . add ( async ( ) => {
188214 const { timeout} = queue ;
189215 assert . equal ( timeout , newTimeout ) ;
190216 await delay ( 100 ) ;
191217 result . push ( '🐅' ) ;
192218 } ) ;
219+
193220 await queue . onIdle ( ) ;
194221 assert . deepEqual ( result , [ '🐅' ] ) ;
195222} ) ;
0 commit comments