Skip to content

Commit 25aa8f6

Browse files
author
Ruben Bridgewater
committed
Fix monitoring mode not always activating soon enough
1 parent 8b6f2dd commit 25aa8f6

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/individualCommands.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ RedisClient.prototype.monitor = RedisClient.prototype.MONITOR = function monitor
5555
// Use a individual command, as this is a special case that does not has to be checked for any other command
5656
var self = this;
5757
var call_on_write = function () {
58-
// Activating monitor mode has to happen before Redis returned the callback,
59-
// as the client could receive monitoring commands before the callback returned through a race condition
58+
// Activating monitor mode has to happen before Redis returned the callback. The monitor result is returned first.
59+
// Therefore we expect the command to be properly processed. If this is not the case, it's not an issue either.
6060
self.monitoring = true;
6161
};
6262
return this.internal_send_command(new Command('monitor', [], callback, call_on_write));

test/node_redis.spec.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,26 @@ describe('The node_redis client', function () {
336336
});
337337
});
338338

339+
it('should retry all commands even if the offline queue is disabled', function (done) {
340+
var bclient = redis.createClient({
341+
parser: parser,
342+
enableOfflineQueue: false,
343+
retryUnfulfilledCommands: true
344+
});
345+
bclient.once('ready', function () {
346+
bclient.blpop('blocking list 2', 5, function (err, value) {
347+
assert.strictEqual(value[0], 'blocking list 2');
348+
assert.strictEqual(value[1], 'initial value');
349+
bclient.end(true);
350+
done(err);
351+
});
352+
setTimeout(function () {
353+
bclient.stream.destroy();
354+
client.rpush('blocking list 2', 'initial value', helper.isNumber(1));
355+
}, 100);
356+
});
357+
});
358+
339359
});
340360

341361
describe('.end', function () {
@@ -650,6 +670,7 @@ describe('The node_redis client', function () {
650670
});
651671

652672
monitorClient.on('monitor', function (time, args, rawOutput) {
673+
assert.strictEqual(monitorClient.monitoring, true);
653674
responses.push(args);
654675
assert(utils.monitor_regex.test(rawOutput), rawOutput);
655676
if (responses.length === 6) {
@@ -670,6 +691,7 @@ describe('The node_redis client', function () {
670691
});
671692

672693
monitorClient.MONITOR(function (err, res) {
694+
assert.strictEqual(monitorClient.monitoring, true);
673695
assert.strictEqual(res.inspect(), new Buffer('OK').inspect());
674696
client.mget('hello', new Buffer('world'));
675697
});
@@ -688,6 +710,7 @@ describe('The node_redis client', function () {
688710
client.MONITOR(helper.isString('OK'));
689711
client.mget('hello', 'world');
690712
client.on('monitor', function (time, args, rawOutput) {
713+
assert.strictEqual(client.monitoring, true);
691714
assert(utils.monitor_regex.test(rawOutput), rawOutput);
692715
assert.deepEqual(args, ['mget', 'hello', 'world']);
693716
if (i++ === 2) {
@@ -708,6 +731,7 @@ describe('The node_redis client', function () {
708731
assert.deepEqual(res, ['OK', [null, null]]);
709732
});
710733
client.on('monitor', function (time, args, rawOutput) {
734+
assert.strictEqual(client.monitoring, true);
711735
assert(utils.monitor_regex.test(rawOutput), rawOutput);
712736
assert.deepEqual(args, ['mget', 'hello', 'world']);
713737
if (i++ === 2) {
@@ -719,20 +743,22 @@ describe('The node_redis client', function () {
719743
});
720744
});
721745

722-
it('monitor does not activate if the command could not be processed properly', function (done) {
746+
it('monitor activates even if the command could not be processed properly after a reconnect', function (done) {
723747
client.MONITOR(function (err, res) {
724748
assert.strictEqual(err.code, 'UNCERTAIN_STATE');
725749
});
726750
client.on('error', function (err) {}); // Ignore error here
727751
client.stream.destroy();
752+
var end = helper.callFuncAfter(done, 2);
728753
client.on('monitor', function (time, args, rawOutput) {
729-
done(new Error('failed')); // Should not be activated
754+
assert.strictEqual(client.monitoring, true);
755+
end();
730756
});
731757
client.on('reconnecting', function () {
732758
client.get('foo', function (err, res) {
733759
assert(!err);
734-
assert.strictEqual(client.monitoring, false);
735-
setTimeout(done, 10); // The monitor command might be returned a tiny bit later
760+
assert.strictEqual(client.monitoring, true);
761+
end();
736762
});
737763
});
738764
});

0 commit comments

Comments
 (0)