Skip to content

Commit d39f696

Browse files
author
Ruben Bridgewater
committed
Add tests and emit UNCERTAIN_STATE errors
1 parent 0ec2c43 commit d39f696

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,8 @@ RedisClient.prototype.connection_gone = function (why) {
530530
// error.message = 'Command aborted in uncertain state and queued for next connection.';
531531
// }
532532
this.flush_and_error(error, ['command_queue']);
533+
error.message = 'Redis connection lost and commands aborted in uncertain state. They might have been processed.';
534+
this.emit('error', error);
533535
}
534536

535537
if (this.retry_max_delay !== null && this.retry_delay > this.retry_max_delay) {
@@ -1109,11 +1111,12 @@ Multi.prototype.execute_callback = function (err, replies) {
11091111

11101112
if (err) {
11111113
// The errors would be circular
1112-
err.errors = err.code !== 'CONNECTION_BROKEN' ? this.errors : [];
1114+
var connection_error = ['CONNECTION_BROKEN', 'UNCERTAIN_STATE'].indexOf(err.code) !== -1;
1115+
err.errors = connection_error ? [] : this.errors;
11131116
if (this.callback) {
11141117
this.callback(err);
1115-
} else if (err.code !== 'CONNECTION_BROKEN') {
1116-
// Exclude CONNECTION_BROKEN so that error won't be emitted twice
1118+
// Exclude connection errors so that those errors won't be emitted twice
1119+
} else if (!connection_error) {
11171120
this._client.emit('error', err);
11181121
}
11191122
return;

test/commands/multi.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,19 @@ describe("The 'multi' method", function () {
553553
});
554554
});
555555

556+
it("emits error once if reconnecting after multi has been executed but not yet returned without callback", function (done) {
557+
helper.serverVersionAtLeast.call(this, client, [2, 6, 5]);
558+
559+
client.on('error', function(err) {
560+
assert.strictEqual(err.code, 'UNCERTAIN_STATE');
561+
done();
562+
});
563+
564+
client.multi().set("foo", 'bar').get('foo').exec();
565+
// Abort connection before the value returned
566+
client.stream.destroy();
567+
});
568+
556569
});
557570
});
558571
});

test/connection.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ describe("connection tests", function () {
107107
});
108108

109109
});
110+
111+
it("emits error once if reconnecting after command has been executed but not yet returned without callback", function (done) {
112+
client = redis.createClient.apply(redis.createClient, args);
113+
client.on('error', function(err) {
114+
assert.strictEqual(err.code, 'UNCERTAIN_STATE');
115+
done();
116+
});
117+
118+
client.on('ready', function() {
119+
client.set("foo", 'bar');
120+
// Abort connection before the value returned
121+
client.stream.destroy();
122+
});
123+
});
110124
});
111125

112126
describe("when not connected", function () {

0 commit comments

Comments
 (0)