From 8fec46c650645befeed6f1f55448d2ab50faf86a Mon Sep 17 00:00:00 2001 From: Martin Slota Date: Wed, 21 May 2025 11:51:32 +0200 Subject: [PATCH 1/2] Tolerate having no connected nodes in ConnectionPool.getSampleInstance() Signed-off-by: Martin Slota --- lib/cluster/ConnectionPool.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cluster/ConnectionPool.ts b/lib/cluster/ConnectionPool.ts index 5a117bad..d542fc2f 100644 --- a/lib/cluster/ConnectionPool.ts +++ b/lib/cluster/ConnectionPool.ts @@ -39,7 +39,7 @@ export default class ConnectionPool extends EventEmitter { getSampleInstance(role: NodeRole): Redis { const keys = Object.keys(this.nodeRecords[role]); const sampleKey = sample(keys); - return this.nodeRecords[role][sampleKey].redis; + return this.nodeRecords[role][sampleKey]?.redis; } /** From 27bcab5c3a24ec6f88fcad5e7a441cd3ce67f2ab Mon Sep 17 00:00:00 2001 From: Martin Slota Date: Wed, 21 May 2025 12:08:26 +0200 Subject: [PATCH 2/2] Add a regression test Signed-off-by: Martin Slota --- test/functional/cluster/ConnectionPool.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/functional/cluster/ConnectionPool.ts diff --git a/test/functional/cluster/ConnectionPool.ts b/test/functional/cluster/ConnectionPool.ts new file mode 100644 index 00000000..04cfd41c --- /dev/null +++ b/test/functional/cluster/ConnectionPool.ts @@ -0,0 +1,11 @@ +import { expect } from "chai"; + +import ConnectionPool from "../../../lib/cluster/ConnectionPool"; + +describe("The cluster connection pool", () => { + describe("when not connected", () => { + it("does not throw when fetching a sample node", () => { + expect(new ConnectionPool({}).getSampleInstance("all")).to.be.undefined; + }); + }); +});