diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index 98bb18989..47cc79573 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -250,6 +250,9 @@ class Cluster< this.emit("+node", redis); }); this.connectionPool.on("drain", () => { + if (this.status === "end") { + return; + } this.setStatus("close"); }); this.connectionPool.on("nodeError", (error, key) => { diff --git a/test/functional/cluster/disconnection.ts b/test/functional/cluster/disconnection.ts index 40e33e01d..df29d285a 100644 --- a/test/functional/cluster/disconnection.ts +++ b/test/functional/cluster/disconnection.ts @@ -1,8 +1,10 @@ import * as sinon from "sinon"; -import { expect } from "chai"; +import { expect, use } from "chai"; import { Cluster } from "../../../lib"; import MockServer from "../../helpers/mock_server"; +use(require("chai-as-promised")); + describe("disconnection", () => { afterEach(() => { sinon.restore(); @@ -47,4 +49,28 @@ describe("disconnection", () => { server.disconnect(); }); + + it("should stay ended when the nodes end after disconnect", async () => { + const slotTable = [ + [0, 8000, ["127.0.0.1", 30001]], + [8001, 16383, ["127.0.0.1", 30002]], + ]; + new MockServer(30001, () => {}, slotTable); + new MockServer(30002, () => {}, slotTable); + + const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }]); + await new Promise((resolve) => cluster.once("ready", resolve)); + const nodesEnded = cluster + .nodes() + .map((node) => new Promise((resolve) => node.once("end", resolve))); + + cluster.disconnect(); + await Promise.all(nodesEnded); + await new Promise((resolve) => setImmediate(resolve)); + + expect(cluster.status).to.equal("end"); + await expect(cluster.get("foo")).to.be.rejectedWith( + "Connection is closed." + ); + }); });