@@ -253,10 +253,9 @@ describe('Client', () => {
253
253
} , GLOBAL . SERVERS . OPEN ) ;
254
254
255
255
testUtils . testWithClient ( 'AbortError' , async client => {
256
- const abortIn = 10 ;
257
- await delaySetImmediate ( abortIn * 2 , async ( ) => {
256
+ await blockSetImmediate ( async ( ) => {
258
257
await assert . rejects ( client . sendCommand ( [ 'PING' ] , {
259
- abortSignal : AbortSignal . timeout ( abortIn )
258
+ abortSignal : AbortSignal . timeout ( 5 )
260
259
} ) , AbortError ) ;
261
260
} )
262
261
} , GLOBAL . SERVERS . OPEN ) ;
@@ -265,19 +264,18 @@ describe('Client', () => {
265
264
266
265
267
266
testUtils . testWithClient ( 'Timeout with custom timeout config' , async client => {
268
- const timeoutIn = 5 ;
269
- await delaySetImmediate ( timeoutIn * 2 , async ( ) => {
267
+ await blockSetImmediate ( async ( ) => {
270
268
await assert . rejects ( client . sendCommand ( [ 'PING' ] , {
271
- timeout : timeoutIn
269
+ timeout : 5
272
270
} ) , TimeoutError ) ;
273
271
} )
274
272
} , GLOBAL . SERVERS . OPEN ) ;
275
273
276
274
testUtils . testWithClient ( 'Timeout with global timeout config' , async client => {
277
- await delaySetImmediate ( 10 , async ( ) => {
278
- await assert . rejects ( client . sendCommand ( [ 'PING' ] ) , TimeoutError ) ;
275
+ await blockSetImmediate ( async ( ) => {
279
276
await assert . rejects ( client . ping ( ) , TimeoutError ) ;
280
- } )
277
+ await assert . rejects ( client . sendCommand ( [ 'PING' ] ) , TimeoutError ) ;
278
+ } ) ;
281
279
} , {
282
280
...GLOBAL . SERVERS . OPEN ,
283
281
clientOptions : {
@@ -923,17 +921,17 @@ describe('Client', () => {
923
921
} ) ;
924
922
} ) ;
925
923
926
- async function delaySetImmediate ( ms : number , fn : ( ) => Promise < void > ) {
927
- // Stub setImmediate to delay execution, allowing AbortError to trigger
928
- const originalSetImmediate = global . setImmediate ;
924
+ /**
925
+ * Executes the provided function in a context where setImmediate is stubbed to not do anything.
926
+ * This blocks setImmediate callbacks from executing
927
+ */
928
+ async function blockSetImmediate ( fn : ( ) => Promise < unknown > ) {
929
929
let setImmediateStub : any ;
930
930
931
931
try {
932
932
setImmediateStub = stub ( global , 'setImmediate' ) ;
933
- setImmediateStub . callsFake ( ( callback : ( ...args : any [ ] ) => void , ...args : any [ ] ) => {
934
- return originalSetImmediate ( ( ) => {
935
- setTimeout ( ( ) => callback ( ...args ) , ms ) ;
936
- } ) ;
933
+ setImmediateStub . callsFake ( ( ) => {
934
+ //Dont call the callback, effectively blocking execution
937
935
} ) ;
938
936
await fn ( ) ;
939
937
} finally {
0 commit comments