Skip to content

Commit

Permalink
feat(cluster): add #duplicate() method (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrondin authored and luin committed Jul 14, 2019
1 parent 90b1aec commit 8b150c2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,29 @@ class Cluster extends EventEmitter {
);
}

/**
* Create a new instance with the same startup nodes and options as the current one.
*
* @example
* ```js
* var cluster = new Redis.Cluster([{ host: "127.0.0.1", port: "30001" }]);
* var anotherCluster = cluster.duplicate();
* ```
*
* @public
* @param {(Array<string | number | object>)} [overrideStartupNodes=[]]
* @param {IClusterOptions} [overrideOptions={}]
* @memberof Cluster
*/
public duplicate(overrideStartupNodes = [], overrideOptions = {}) {
const startupNodes =
overrideStartupNodes.length > 0
? overrideStartupNodes
: this.startupNodes.slice(0);
const options = Object.assign({}, this.options, overrideOptions);
return new Cluster(startupNodes, options);
}

/**
* Get nodes with the specified role
*
Expand Down
21 changes: 21 additions & 0 deletions test/functional/cluster/duplicate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import MockServer from "../../helpers/mock_server";
import { Cluster } from "../../../lib";
import { expect } from "chai";

describe("cluster:duplicate", () => {
it("clone the options", done => {
var node = new MockServer(30001);
var cluster = new Cluster([]);
var duplicatedCluster = cluster.duplicate([
{ host: "127.0.0.1", port: "30001" }
]);

node.once("connect", function() {
expect(duplicatedCluster.nodes()).to.have.lengthOf(1);
expect(duplicatedCluster.nodes()[0].options.port).to.eql(30001);
cluster.disconnect();
duplicatedCluster.disconnect();
done();
});
});
});

0 comments on commit 8b150c2

Please sign in to comment.