|
| 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