From 0d4d4d74166fe368d1d5231283178fe5623078bc Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 14 Oct 2015 02:24:11 +0200 Subject: [PATCH] Fix multi not being executed on node 0.10 if not yet ready. Closes #889 --- changelog.md | 6 ++++++ index.js | 20 +++++++++++++------- test/commands/multi.spec.js | 22 +++++++++++++++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/changelog.md b/changelog.md index f271ae20bb..0a1c23d10d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,12 @@ Changelog ========= +## v.2.2.3 - 14 Oct, 2015 + +Bugfixes + +- Fix multi not being executed on Node 0.10.x if node_redis not yet ready ([@BridgeAR](https://github.com/BridgeAR)) + ## v.2.2.2 - 14 Oct, 2015 Bugfixes diff --git a/index.js b/index.js index 8988aa68d9..5a71bca39a 100644 --- a/index.js +++ b/index.js @@ -38,9 +38,13 @@ function RedisClient(stream, options) { this.pipeline = 0; if (!stream.cork) { - stream.cork = function noop() { - self.pipeline_queue = new Queue(); - }; + this.cork = function noop (len) {}; + this.once('ready', function () { + self.cork = function (len) { + self.pipeline = len; + self.pipeline_queue = new Queue(len); + }; + }); stream.uncork = function noop() {}; this.write = this.writeStream; } @@ -128,6 +132,10 @@ RedisClient.prototype.install_stream_listeners = function() { }); }; +RedisClient.prototype.cork = function (len) { + this.stream.cork(); +}; + RedisClient.prototype.initialize_retry_vars = function () { this.retry_timer = null; this.retry_totaltime = 0; @@ -1074,8 +1082,7 @@ Multi.prototype.exec_transaction = function (callback) { var cb; this.errors = []; this.callback = callback; - this._client.stream.cork(); - this._client.pipeline = len + 2; + this._client.cork(len + 2); this.wants_buffers = new Array(len); this.send_command('multi', []); // drain queue, callback will catch 'QUEUED' or error @@ -1192,8 +1199,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct return true; } this.results = new Array(len); - this._client.stream.cork(); - this._client.pipeline = len; + this._client.cork(len); var lastCallback = function (cb) { return function (err, res) { cb(err, res); diff --git a/test/commands/multi.spec.js b/test/commands/multi.spec.js index 7ebfc63b29..775bbcc150 100644 --- a/test/commands/multi.spec.js +++ b/test/commands/multi.spec.js @@ -50,6 +50,26 @@ describe("The 'multi' method", function () { describe("when connected", function () { var client; + beforeEach(function (done) { + client = redis.createClient.apply(redis.createClient, args); + client.once("connect", done); + }); + + afterEach(function () { + client.end(); + }); + + it("executes a pipelined multi properly in combination with the offline queue", function (done) { + var multi1 = client.multi(); + multi1.set("m1", "123"); + multi1.get('m1'); + multi1.exec(done); + }); + }); + + describe("when ready", function () { + var client; + beforeEach(function (done) { client = redis.createClient.apply(redis.createClient, args); client.once("ready", function () { @@ -73,7 +93,7 @@ describe("The 'multi' method", function () { assert.strictEqual(notBuffering, true); }); - it("runs normal calls inbetween multis", function (done) { + it("runs normal calls in-between multis", function (done) { var multi1 = client.multi(); multi1.set("m1", "123"); client.set('m2', '456', done);