diff --git a/lib/cluster/ConnectionPool.ts b/lib/cluster/ConnectionPool.ts index 5a117bad..1daebb3d 100644 --- a/lib/cluster/ConnectionPool.ts +++ b/lib/cluster/ConnectionPool.ts @@ -29,7 +29,7 @@ export default class ConnectionPool extends EventEmitter { getNodes(role: NodeRole = "all"): Redis[] { const nodeRecords = this.nodeRecords[role]; - return Object.keys(nodeRecords).map((key) => nodeRecords[key].redis); + return Object.keys(nodeRecords).map((key) => nodeRecords[key]?.redis); } getInstanceByKey(key: NodeKey): Redis { @@ -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..3f993ffc --- /dev/null +++ b/test/functional/cluster/ConnectionPool.ts @@ -0,0 +1,12 @@ +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; + expect(new ConnectionPool({}).getNodes("all")).to.be.eql([]); + }); + }); +});