Skip to content

Commit b3b884c

Browse files
fix: handle non-existant redis node gracefully (#34)
* fix: handle non-existant redis gracefully Signed-off-by: Gürgün Dayıoğlu <[email protected]> * add tests Signed-off-by: Gürgün Dayıoğlu <[email protected]> Co-authored-by: Martin Slota <[email protected]> * fix eql Signed-off-by: Gürgün Dayıoğlu <[email protected]> --------- Signed-off-by: Gürgün Dayıoğlu <[email protected]> Co-authored-by: Martin Slota <[email protected]>
1 parent dbc41e6 commit b3b884c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/cluster/ConnectionPool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class ConnectionPool extends EventEmitter {
2929

3030
getNodes(role: NodeRole = "all"): Redis[] {
3131
const nodeRecords = this.nodeRecords[role];
32-
return Object.keys(nodeRecords).map((key) => nodeRecords[key].redis);
32+
return Object.keys(nodeRecords).map((key) => nodeRecords[key]?.redis);
3333
}
3434

3535
getInstanceByKey(key: NodeKey): Redis {
@@ -39,7 +39,7 @@ export default class ConnectionPool extends EventEmitter {
3939
getSampleInstance(role: NodeRole): Redis {
4040
const keys = Object.keys(this.nodeRecords[role]);
4141
const sampleKey = sample(keys);
42-
return this.nodeRecords[role][sampleKey].redis;
42+
return this.nodeRecords[role][sampleKey]?.redis;
4343
}
4444

4545
/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect } from "chai";
2+
3+
import ConnectionPool from "../../../lib/cluster/ConnectionPool";
4+
5+
describe("The cluster connection pool", () => {
6+
describe("when not connected", () => {
7+
it("does not throw when fetching a sample node", () => {
8+
expect(new ConnectionPool({}).getSampleInstance("all")).to.be.undefined;
9+
expect(new ConnectionPool({}).getNodes("all")).to.be.eql([]);
10+
});
11+
});
12+
});

0 commit comments

Comments
 (0)