Skip to content

Commit f5fafbe

Browse files
committed
--wip-- [skip ci]
1 parent 0c11413 commit f5fafbe

File tree

1 file changed

+66
-11
lines changed

1 file changed

+66
-11
lines changed

packages/client/lib/client/index.spec.ts

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL, waitTillBeenCalled } from '../test-utils';
33
import RedisClient, { RedisClientOptions, RedisClientType } from '.';
4-
import { AbortError, ClientClosedError, ClientOfflineError, ConnectionTimeoutError, DisconnectsClientError, ErrorReply, MultiErrorReply, SocketClosedUnexpectedlyError, WatchError } from '../errors';
4+
import { AbortError, ClientClosedError, ClientOfflineError, ConnectionTimeoutError, DisconnectsClientError, ErrorReply, MultiErrorReply, SocketClosedUnexpectedlyError, TimeoutError, WatchError } from '../errors';
55
import { defineScript } from '../lua-script';
6-
import { spy } from 'sinon';
6+
import { spy, stub } from 'sinon';
77
import { once } from 'node:events';
88
import { MATH_FUNCTION, loadMathFunction } from '../commands/FUNCTION_LOAD.spec';
99
import { RESP_TYPES } from '../RESP/decoder';
@@ -252,17 +252,72 @@ describe('Client', () => {
252252
});
253253
}, GLOBAL.SERVERS.OPEN);
254254

255-
testUtils.testWithClient('AbortError', client => {
256-
const controller = new AbortController();
257-
controller.abort();
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');
258259

259-
return assert.rejects(
260-
client.sendCommand(['PING'], {
261-
abortSignal: controller.signal
262-
}),
263-
AbortError
264-
);
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();
265273
}, GLOBAL.SERVERS.OPEN);
274+
275+
276+
});
277+
278+
279+
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+
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();
297+
}, GLOBAL.SERVERS.OPEN);
298+
299+
300+
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();
314+
}, {
315+
...GLOBAL.SERVERS.OPEN,
316+
clientOptions: {
317+
commandOptions: {
318+
timeout: 5
319+
}
320+
}
266321
});
267322

268323
testUtils.testWithClient('undefined and null should not break the client', async client => {

0 commit comments

Comments
 (0)