From 829bf26f60d0af4068bd424e4f411d386ddf7679 Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Fri, 19 Feb 2016 12:42:53 +0800 Subject: [PATCH] fix: select db on connect event to prevent subscribe errors Closes #255 --- lib/redis/event_handler.js | 5 ++++- test/functional/select.js | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/redis/event_handler.js b/lib/redis/event_handler.js index e6bd8e61..8b5a85ef 100644 --- a/lib/redis/event_handler.js +++ b/lib/redis/event_handler.js @@ -19,6 +19,10 @@ exports.connectHandler = function (self) { }); } + if (self.condition.select) { + self.selectBuffer(self.condition.select); + } + if (!self.options.enableReadyCheck) { exports.readyHandler(self)(); } @@ -121,7 +125,6 @@ exports.readyHandler = function (self) { } var item; var finalSelect = self.prevCondition ? self.prevCondition.select : self.condition.select; - self.condition.select = 0; if (self.options.connectionName) { debug('set the connection mane [%s]', self.options.connectionName); diff --git a/test/functional/select.js b/test/functional/select.js index 17e000ba..933dec3e 100644 --- a/test/functional/select.js +++ b/test/functional/select.js @@ -50,6 +50,9 @@ describe('select', function () { it('should emit "select" event when db changes', function (done) { var changes = []; var redis = new Redis(); + redis.on('select', function (db) { + changes.push(db); + }); redis.select('2', function () { expect(changes).to.eql([2]); redis.select('4', function () { @@ -60,10 +63,20 @@ describe('select', function () { }); }); }); + }); - redis.on('select', function (db) { - console.log('select', db); - changes.push(db); + it('should be sent on the connect event', function (done) { + var redis = new Redis({ db: 2 }); + var selectBuffer = redis.selectBuffer; + redis.selectBuffer = function () { + return selectBuffer.apply(redis, arguments).then(function () { + redis.selectBuffer = selectBuffer; + redis.disconnect(); + done(); + }); + }; + redis.on('connect', function () { + redis.subscribe('anychannel'); }); }); });