diff --git a/test/functional/socketTimeout.ts b/test/functional/socketTimeout.ts new file mode 100644 index 0000000..dbbf4b5 --- /dev/null +++ b/test/functional/socketTimeout.ts @@ -0,0 +1,64 @@ +import { expect } from "chai"; +import Redis from "../../lib/Redis"; + +describe("socketTimeout", () => { + const timeoutMs = 500; + + it("should ensure correct startup with password (https://github.com/redis/ioredis/issues/1919)", (done) => { + let timeoutObj: NodeJS.Timeout; + + const redis = new Redis({ + socketTimeout: timeoutMs, + lazyConnect: true, + password: "foobared", + }); + + redis.on("error", (err) => { + clearTimeout(timeoutObj); + done(err.toString()); + }); + + redis.connect(() => { + timeoutObj = setTimeout(() => { + done(); + }, timeoutMs * 2); + }); + }); + + it("should not throw error when socketTimeout is set and no command is sent", (done) => { + let timeoutObj: NodeJS.Timeout; + + const redis = new Redis({ + socketTimeout: timeoutMs, + lazyConnect: true, + }); + + redis.on("error", (err) => { + clearTimeout(timeoutObj); + done(err.toString()); + }); + + redis.connect(() => { + timeoutObj = setTimeout(() => { + done(); + }, timeoutMs * 2); + }); + }); + + it("should throw if socket timeout is reached", (done) => { + const redis = new Redis({ + socketTimeout: timeoutMs, + lazyConnect: true, + }); + + redis.on("error", (err) => { + expect(err.message).to.include("Socket timeout"); + done(); + }); + + redis.connect(() => { + redis.stream.removeAllListeners("data"); + redis.ping(); + }); + }); +});