Skip to content

Commit 5b98338

Browse files
committed
test(socket): add tests for socketTimeout option
1 parent 4313803 commit 5b98338

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

packages/client/lib/client/socket.spec.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import { strict as assert } from 'node:assert';
22
import { spy } from 'sinon';
33
import { once } from 'node:events';
44
import RedisSocket, { RedisSocketOptions } from './socket';
5+
import testUtils, { GLOBAL } from '../test-utils';
6+
import { setTimeout } from 'timers/promises';
57

68
describe('Socket', () => {
79
function createSocket(options: RedisSocketOptions): RedisSocket {
8-
const socket = new RedisSocket(
9-
() => Promise.resolve(),
10-
options
11-
);
10+
const socket = new RedisSocket(() => Promise.resolve(), options);
1211

1312
socket.on('error', () => {
1413
// ignore errors
@@ -84,4 +83,66 @@ describe('Socket', () => {
8483
assert.equal(socket.isOpen, false);
8584
});
8685
});
86+
87+
describe('socketTimeout', () => {
88+
const timeout = 50;
89+
testUtils.testWithClient(
90+
'should timeout with positive socketTimeout values',
91+
async client => {
92+
let timedOut = false;
93+
94+
assert.equal(client.isReady, true, 'client.isReady');
95+
assert.equal(client.isOpen, true, 'client.isOpen');
96+
97+
client.on('error', err => {
98+
assert.equal(
99+
err.message,
100+
`Socket timeout timeout. Expecting data, but didn't receive any in ${timeout}ms.`
101+
);
102+
103+
assert.equal(client.isReady, false, 'client.isReady');
104+
105+
// This is actually a bug with the onSocketError implementation,
106+
// the client should be closed before the error is emitted
107+
process.nextTick(() => {
108+
assert.equal(client.isOpen, false, 'client.isOpen');
109+
});
110+
111+
timedOut = true;
112+
});
113+
await setTimeout(timeout * 2);
114+
if (!timedOut) assert.fail('Should have timed out by now');
115+
},
116+
{
117+
...GLOBAL.SERVERS.OPEN,
118+
clientOptions: {
119+
socket: {
120+
socketTimeout: timeout
121+
}
122+
}
123+
}
124+
);
125+
126+
testUtils.testWithClient(
127+
'should not timeout with undefined socketTimeout',
128+
async client => {
129+
130+
assert.equal(client.isReady, true, 'client.isReady');
131+
assert.equal(client.isOpen, true, 'client.isOpen');
132+
133+
client.on('error', err => {
134+
assert.fail('Should not have timed out or errored in any way');
135+
});
136+
await setTimeout(100);
137+
},
138+
{
139+
...GLOBAL.SERVERS.OPEN,
140+
clientOptions: {
141+
socket: {
142+
socketTimeout: undefined
143+
}
144+
}
145+
}
146+
);
147+
});
87148
});

packages/client/lib/client/socket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ export default class RedisSocket extends EventEmitter {
260260
}
261261

262262
if (this.#socketTimeout) {
263-
socket.setTimeout(this.#socketTimeout);
264263
socket.once('timeout', () => {
265264
socket.destroy(new SocketTimeoutError(this.#socketTimeout!));
266265
});
266+
socket.setTimeout(this.#socketTimeout);
267267
}
268268

269269
socket

0 commit comments

Comments
 (0)