Skip to content

Commit

Permalink
fix: select db on connect event to prevent subscribe errors
Browse files Browse the repository at this point in the history
Closes #255
  • Loading branch information
luin committed Feb 19, 2016
1 parent 13ac1c4 commit 829bf26
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/redis/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ exports.connectHandler = function (self) {
});
}

if (self.condition.select) {
self.selectBuffer(self.condition.select);
}

if (!self.options.enableReadyCheck) {
exports.readyHandler(self)();
}
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 16 additions & 3 deletions test/functional/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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');
});
});
});

0 comments on commit 829bf26

Please sign in to comment.