Skip to content

Commit

Permalink
fix: keep sentinels of options immutable
Browse files Browse the repository at this point in the history
Close #936
  • Loading branch information
luin committed Jul 22, 2019
1 parent e593e34 commit bacb7e1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/connectors/SentinelConnector/SentinelIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ function isSentinelEql(
export default class SentinelIterator
implements Iterator<Partial<ISentinelAddress>> {
private cursor: number = 0;
private sentinels: Array<Partial<ISentinelAddress>>;

constructor(private sentinels: Array<Partial<ISentinelAddress>>) {}
constructor(sentinels: Array<Partial<ISentinelAddress>>) {
this.sentinels = sentinels.slice(0);
}

next() {
const done = this.cursor >= this.sentinels.length;
Expand Down
9 changes: 8 additions & 1 deletion test/functional/sentinel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Redis from "../../lib/redis";
import MockServer from "../helpers/mock_server";
import { expect } from "chai";
import * as sinon from "sinon";

describe("sentinel", function() {
describe("connect", function() {
Expand Down Expand Up @@ -121,6 +122,12 @@ describe("sentinel", function() {

it("should add additionally discovered sentinels when resolving successfully", function(done) {
var sentinels = [{ host: "127.0.0.1", port: 27379 }];
var cloned;

sinon.stub(sentinels, "slice").callsFake((start, end) => {
cloned = [].slice.call(sentinels, start, end);
return cloned;
});

var sentinel = new MockServer(27379, function(argv) {
if (argv[0] === "sentinel" && argv[1] === "get-master-addr-by-name") {
Expand All @@ -136,7 +143,7 @@ describe("sentinel", function() {
sentinel.once("disconnect", function() {
redis.disconnect();
master.disconnect(function() {
expect(sentinels.length).to.eql(2);
expect(cloned.length).to.eql(2);
sentinel.disconnect(done);
});
});
Expand Down
18 changes: 18 additions & 0 deletions test/unit/connectors/SentinelConnector/SentinelIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from "chai";
import SentinelIterator from "../../../../lib/connectors/SentinelConnector/SentinelIterator";

describe("SentinelIterator", () => {
it("keep the options immutable", () => {
function getSentinels() {
return [{ host: "127.0.0.1", port: 30001 }];
}
const sentinels = getSentinels();

const iter = new SentinelIterator(sentinels);
iter.add({ host: "127.0..0.1", port: 30002 });

expect(sentinels).to.eql(getSentinels());
expect(iter.next().value.port).to.eql(30001);
expect(iter.next().value.port).to.eql(30002);
});
});

0 comments on commit bacb7e1

Please sign in to comment.