diff --git a/index.js b/index.js index e82d2e5f49..283c743fb1 100644 --- a/index.js +++ b/index.js @@ -285,7 +285,12 @@ RedisClient.prototype.on_ready = function () { // magically restore any modal commands from a previous connection if (this.selected_db !== null) { + // this trick works if and only if the following send_command + // never goes into the offline queue + var pub_sub_mode = this.pub_sub_mode; + this.pub_sub_mode = false; this.send_command('select', [this.selected_db]); + this.pub_sub_mode = pub_sub_mode; } if (this.pub_sub_mode === true) { // only emit "ready" when all subscriptions were made again @@ -494,8 +499,8 @@ RedisClient.prototype.return_error = function (err) { var command_obj = this.command_queue.shift(), queue_len = this.command_queue.getLength(); if (this.pub_sub_mode === false && queue_len === 0) { - this.emit("idle"); this.command_queue = new Queue(); + this.emit("idle"); } if (this.should_buffer && queue_len <= this.command_queue_low_water) { this.emit("drain"); @@ -585,8 +590,8 @@ RedisClient.prototype.return_reply = function (reply) { queue_len = this.command_queue.getLength(); if (this.pub_sub_mode === false && queue_len === 0) { - this.emit("idle"); this.command_queue = new Queue(); // explicitly reclaim storage from old Queue + this.emit("idle"); } if (this.should_buffer && queue_len <= this.command_queue_low_water) { this.emit("drain"); diff --git a/test.js b/test.js index 4b26dca9c9..29fd567880 100644 --- a/test.js +++ b/test.js @@ -724,6 +724,27 @@ tests.reconnect = function () { }); }; +tests.reconnect_select_db_after_pubsub = function() { + var name = "reconnect_select_db_after_pubsub"; + + client.select(test_db_num); + client.set(name, "one"); + client.subscribe('ChannelV', function (err, res) { + client.stream.destroy(); + }); + + client.on("reconnecting", function on_recon(params) { + client.on("ready", function on_connect() { + client.unsubscribe('ChannelV', function (err, res) { + client.get(name, require_string("one", name)); + client.removeListener("connect", on_connect); + client.removeListener("reconnecting", on_recon); + next(name); + }); + }); + }); +}; + tests.idle = function () { var name = "idle";