Skip to content

Commit bacb7e1

Browse files
committed
fix: keep sentinels of options immutable
Close #936
1 parent e593e34 commit bacb7e1

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

lib/connectors/SentinelConnector/SentinelIterator.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ function isSentinelEql(
1313
export default class SentinelIterator
1414
implements Iterator<Partial<ISentinelAddress>> {
1515
private cursor: number = 0;
16+
private sentinels: Array<Partial<ISentinelAddress>>;
1617

17-
constructor(private sentinels: Array<Partial<ISentinelAddress>>) {}
18+
constructor(sentinels: Array<Partial<ISentinelAddress>>) {
19+
this.sentinels = sentinels.slice(0);
20+
}
1821

1922
next() {
2023
const done = this.cursor >= this.sentinels.length;

test/functional/sentinel.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Redis from "../../lib/redis";
22
import MockServer from "../helpers/mock_server";
33
import { expect } from "chai";
4+
import * as sinon from "sinon";
45

56
describe("sentinel", function() {
67
describe("connect", function() {
@@ -121,6 +122,12 @@ describe("sentinel", function() {
121122

122123
it("should add additionally discovered sentinels when resolving successfully", function(done) {
123124
var sentinels = [{ host: "127.0.0.1", port: 27379 }];
125+
var cloned;
126+
127+
sinon.stub(sentinels, "slice").callsFake((start, end) => {
128+
cloned = [].slice.call(sentinels, start, end);
129+
return cloned;
130+
});
124131

125132
var sentinel = new MockServer(27379, function(argv) {
126133
if (argv[0] === "sentinel" && argv[1] === "get-master-addr-by-name") {
@@ -136,7 +143,7 @@ describe("sentinel", function() {
136143
sentinel.once("disconnect", function() {
137144
redis.disconnect();
138145
master.disconnect(function() {
139-
expect(sentinels.length).to.eql(2);
146+
expect(cloned.length).to.eql(2);
140147
sentinel.disconnect(done);
141148
});
142149
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect } from "chai";
2+
import SentinelIterator from "../../../../lib/connectors/SentinelConnector/SentinelIterator";
3+
4+
describe("SentinelIterator", () => {
5+
it("keep the options immutable", () => {
6+
function getSentinels() {
7+
return [{ host: "127.0.0.1", port: 30001 }];
8+
}
9+
const sentinels = getSentinels();
10+
11+
const iter = new SentinelIterator(sentinels);
12+
iter.add({ host: "127.0..0.1", port: 30002 });
13+
14+
expect(sentinels).to.eql(getSentinels());
15+
expect(iter.next().value.port).to.eql(30001);
16+
expect(iter.next().value.port).to.eql(30002);
17+
});
18+
});

0 commit comments

Comments
 (0)