From 6711c94d1b682e2a4664515ac6f39e68e0070aa0 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 23 Nov 2015 11:20:20 +0100 Subject: [PATCH] Add duplicate function to duplicate the current client instance Fixes #919 --- README.md | 4 ++++ index.js | 9 +++++++++ test/node_redis.spec.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/README.md b/README.md index df7519c143..243942683b 100644 --- a/README.md +++ b/README.md @@ -572,6 +572,10 @@ the second word as first parameter: client.multi().script('load', 'return 1').exec(...); client.multi([['script', 'load', 'return 1']]).exec(...); +## client.duplicate([options]) + +Duplicate all current options and return a new redisClient instance. All options passed to the duplicate function are going to replace the original option. + ## client.send_command(command_name[, [args][, callback]]) Used internally to send commands to Redis. Nearly all Redis commands have been added to the `client` object. diff --git a/index.js b/index.js index 2212f1b143..c41f0c82a2 100644 --- a/index.js +++ b/index.js @@ -135,6 +135,15 @@ RedisClient.prototype.create_stream = function () { RedisClient.prototype.cork = noop; RedisClient.prototype.uncork = noop; +RedisClient.prototype.duplicate = function (options) { + var existing_options = clone(this.options); + options = clone(options); + for (var elem in options) { // jshint ignore: line + existing_options[elem] = options[elem]; + } + return new RedisClient(existing_options); +}; + RedisClient.prototype.initialize_retry_vars = function () { this.retry_timer = null; this.retry_totaltime = 0; diff --git a/test/node_redis.spec.js b/test/node_redis.spec.js index ca13878597..39cabc8118 100644 --- a/test/node_redis.spec.js +++ b/test/node_redis.spec.js @@ -42,6 +42,45 @@ describe("The node_redis client", function () { }); }); + describe('duplicate', function () { + it('check if all options got copied properly', function(done) { + var client2 = client.duplicate(); + assert(client.connected); + assert(!client2.connected); + for (var elem in client.options) { + if (client.options.hasOwnProperty(elem)) { + assert.strictEqual(client2.options[elem], client.options[elem]); + } + } + client2.on('ready', function () { + client2.end(true); + done(); + }); + }); + + it('check if all new options replaced the old ones', function(done) { + var client2 = client.duplicate({ + no_ready_check: true + }); + assert(client.connected); + assert(!client2.connected); + assert.strictEqual(client.options.no_ready_check, undefined); + assert.strictEqual(client2.options.no_ready_check, true); + assert.notDeepEqual(client.options, client2.options); + for (var elem in client.options) { + if (client.options.hasOwnProperty(elem)) { + if (elem !== 'no_ready_check') { + assert.strictEqual(client2.options[elem], client.options[elem]); + } + } + } + client2.on('ready', function () { + client2.end(true); + done(); + }); + }); + }); + describe('big data', function () { // Check if the fast mode for big strings is working correct