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; } /** 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; + }); + }); +});