Skip to content

Commit c7a2380

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

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

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

Lines changed: 50 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,51 @@ 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+
assert.equal(client.isOpen, true);
94+
client.on('error', err => {
95+
assert.equal(
96+
err.message,
97+
`Socket timeout timeout. Expecting data, but didn't receive any in ${timeout}ms.`
98+
);
99+
timedOut = true;
100+
});
101+
await setTimeout(timeout! * 2);
102+
if (!timedOut) assert.fail('Should have timed out');
103+
},
104+
{
105+
...GLOBAL.SERVERS.OPEN,
106+
clientOptions: {
107+
socket: {
108+
socketTimeout: timeout
109+
}
110+
}
111+
}
112+
);
113+
114+
testUtils.testWithClient(
115+
'should not timeout with undefined socketTimeout',
116+
async client => {
117+
assert.equal(client.isOpen, true);
118+
client.on('error', err => {
119+
assert.fail('Should not have timed out or errored in any way');
120+
});
121+
await setTimeout(100);
122+
},
123+
{
124+
...GLOBAL.SERVERS.OPEN,
125+
clientOptions: {
126+
socket: {
127+
socketTimeout: undefined
128+
}
129+
}
130+
}
131+
);
132+
});
87133
});

0 commit comments

Comments
 (0)