@@ -253,64 +253,31 @@ describe('Client', () => {
253
253
} , GLOBAL . SERVERS . OPEN ) ;
254
254
255
255
testUtils . testWithClient ( 'AbortError' , async client => {
256
- // Stub setImmediate to delay execution, allowing AbortError to trigger
257
- const originalSetImmediate = global . setImmediate ;
258
- const setImmediateStub = stub ( global , 'setImmediate' ) ;
259
-
260
- const abortIn = 5 ;
261
-
262
- setImmediateStub . callsFake ( ( callback : ( ...args : any [ ] ) => void , ...args : any [ ] ) => {
263
- return originalSetImmediate ( ( ) => {
264
- setTimeout ( ( ) => callback ( ...args ) , abortIn * 2 ) ;
265
- } ) ;
266
- } ) ;
267
-
268
- await assert . rejects ( client . sendCommand ( [ 'PING' ] , {
269
- abortSignal : AbortSignal . timeout ( abortIn )
270
- } ) , AbortError ) ;
271
-
272
- setImmediateStub . restore ( ) ;
256
+ const abortIn = 10 ;
257
+ await delaySetImmediate ( abortIn * 2 , async ( ) => {
258
+ await assert . rejects ( client . sendCommand ( [ 'PING' ] , {
259
+ abortSignal : AbortSignal . timeout ( abortIn )
260
+ } ) , AbortError ) ;
261
+ } )
273
262
} , GLOBAL . SERVERS . OPEN ) ;
274
263
275
-
276
264
} ) ;
277
265
278
266
279
267
testUtils . testWithClient ( 'Timeout with custom timeout config' , async client => {
280
- // Stub setImmediate to delay execution, allowing TimeoutError to trigger
281
- const originalSetImmediate = global . setImmediate ;
282
- const setImmediateStub = stub ( global , 'setImmediate' ) ;
283
-
284
268
const timeoutIn = 5 ;
285
-
286
- setImmediateStub . callsFake ( ( callback : ( ...args : any [ ] ) => void , ...args : any [ ] ) => {
287
- return originalSetImmediate ( ( ) => {
288
- setTimeout ( ( ) => callback ( ...args ) , timeoutIn * 2 ) ;
289
- } ) ;
290
- } ) ;
291
-
292
- await assert . rejects ( client . sendCommand ( [ 'PING' ] , {
293
- timeout : timeoutIn
294
- } ) , TimeoutError ) ;
295
-
296
- setImmediateStub . restore ( ) ;
269
+ await delaySetImmediate ( timeoutIn * 2 , async ( ) => {
270
+ await assert . rejects ( client . sendCommand ( [ 'PING' ] , {
271
+ timeout : timeoutIn
272
+ } ) , TimeoutError ) ;
273
+ } )
297
274
} , GLOBAL . SERVERS . OPEN ) ;
298
275
299
-
300
276
testUtils . testWithClient ( 'Timeout with global timeout config' , async client => {
301
- // Stub setImmediate to delay execution, allowing TimeoutError to trigger
302
- const originalSetImmediate = global . setImmediate ;
303
- const setImmediateStub = stub ( global , 'setImmediate' ) ;
304
-
305
- setImmediateStub . callsFake ( ( callback : ( ...args : any [ ] ) => void , ...args : any [ ] ) => {
306
- return originalSetImmediate ( ( ) => {
307
- setTimeout ( ( ) => callback ( ...args ) , 10 ) ;
308
- } ) ;
309
- } ) ;
310
-
311
- await assert . rejects ( client . sendCommand ( [ 'PING' ] ) , TimeoutError ) ;
312
-
313
- setImmediateStub . restore ( ) ;
277
+ await delaySetImmediate ( 10 , async ( ) => {
278
+ await assert . rejects ( client . sendCommand ( [ 'PING' ] ) , TimeoutError ) ;
279
+ await assert . rejects ( client . ping ( ) , TimeoutError ) ;
280
+ } )
314
281
} , {
315
282
...GLOBAL . SERVERS . OPEN ,
316
283
clientOptions : {
@@ -955,3 +922,23 @@ describe('Client', () => {
955
922
} , GLOBAL . SERVERS . OPEN ) ;
956
923
} ) ;
957
924
} ) ;
925
+
926
+ async function delaySetImmediate ( ms : number , fn : ( ) => Promise < void > ) {
927
+ // Stub setImmediate to delay execution, allowing AbortError to trigger
928
+ const originalSetImmediate = global . setImmediate ;
929
+ let setImmediateStub : any ;
930
+
931
+ try {
932
+ setImmediateStub = stub ( global , 'setImmediate' ) ;
933
+ setImmediateStub . callsFake ( ( callback : ( ...args : any [ ] ) => void , ...args : any [ ] ) => {
934
+ return originalSetImmediate ( ( ) => {
935
+ setTimeout ( ( ) => callback ( ...args ) , ms ) ;
936
+ } ) ;
937
+ } ) ;
938
+ await fn ( ) ;
939
+ } finally {
940
+ if ( setImmediateStub ) {
941
+ setImmediateStub . restore ( ) ;
942
+ }
943
+ }
944
+ }
0 commit comments