Skip to content

Commit 7f4ee5e

Browse files
committed
add socketTimeout functional tests
1 parent 26904a0 commit 7f4ee5e

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

test/functional/socketTimeout.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { expect } from "chai";
2+
import Redis from "../../lib/Redis";
3+
4+
describe("socketTimeout", () => {
5+
const timeoutMs = 500;
6+
7+
it("should ensure correct startup with password (https://github.com/redis/ioredis/issues/1919)", (done) => {
8+
let timeoutObj: NodeJS.Timeout;
9+
10+
const redis = new Redis({
11+
socketTimeout: timeoutMs,
12+
lazyConnect: true,
13+
password: "foobared",
14+
});
15+
16+
redis.on("error", (err) => {
17+
clearTimeout(timeoutObj);
18+
done(err.toString());
19+
});
20+
21+
redis.connect(() => {
22+
timeoutObj = setTimeout(() => {
23+
done();
24+
}, timeoutMs * 2);
25+
});
26+
});
27+
28+
it("should not throw error when socketTimeout is set and no command is sent", (done) => {
29+
let timeoutObj: NodeJS.Timeout;
30+
31+
const redis = new Redis({
32+
socketTimeout: timeoutMs,
33+
lazyConnect: true,
34+
});
35+
36+
redis.on("error", (err) => {
37+
clearTimeout(timeoutObj);
38+
done(err.toString());
39+
});
40+
41+
redis.connect(() => {
42+
timeoutObj = setTimeout(() => {
43+
done();
44+
}, timeoutMs * 2);
45+
});
46+
});
47+
48+
it("should throw if socket timeout is reached", (done) => {
49+
const redis = new Redis({
50+
socketTimeout: timeoutMs,
51+
lazyConnect: true,
52+
});
53+
54+
redis.on("error", (err) => {
55+
expect(err.message).to.include("Socket timeout");
56+
done();
57+
});
58+
59+
redis.connect(() => {
60+
redis.stream.removeAllListeners("data");
61+
redis.ping();
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)