1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Add monitor transaction warning / error

This commit is contained in:
Ruben Bridgewater
2016-04-21 02:54:42 +02:00
parent f7c4d131be
commit eae16938cd
3 changed files with 59 additions and 8 deletions

View File

@@ -71,14 +71,9 @@ Multi.prototype.monitor = Multi.prototype.MONITOR = function monitor (callback)
this.queue.push(['monitor', [], monitor_callback(this._client, callback)]); this.queue.push(['monitor', [], monitor_callback(this._client, callback)]);
return this; return this;
} }
var err = new Error( // Set multi monitoring to indicate the exec that it should abort
'You used the monitor command in combination with a transaction. Due to faulty return values of ' + // Remove this "hack" as soon as Redis might fix this
'Redis in this context, the monitor command is now executed without transaction instead and ignored ' + this.monitoring = true;
'in the multi statement.'
);
err.command = 'MONITOR';
utils.reply_in_order(this._client, callback, err);
this._client.monitor('monitor', callback);
return this; return this;
}; };

View File

@@ -85,6 +85,14 @@ function multi_callback (self, err, replies) {
} }
Multi.prototype.exec_transaction = function exec_transaction (callback) { Multi.prototype.exec_transaction = function exec_transaction (callback) {
if (this.monitoring || this._client.monitoring) {
var err = new Error(
'Using transaction with a client that is in monitor mode does not work due to faulty return values of Redis.'
);
err.command = 'EXEC';
err.code = 'EXECABORT';
return utils.reply_in_order(this._client, callback, err);
}
var self = this; var self = this;
var len = self.queue.length; var len = self.queue.length;
self.errors = []; self.errors = [];

View File

@@ -3,6 +3,7 @@
var assert = require('assert'); var assert = require('assert');
var config = require('./lib/config'); var config = require('./lib/config');
var helper = require('./helper'); var helper = require('./helper');
var utils = require('../lib/utils');
var redis = config.redis; var redis = config.redis;
var zlib = require('zlib'); var zlib = require('zlib');
var client; var client;
@@ -129,6 +130,53 @@ describe("The 'multi' method", function () {
client = redis.createClient.apply(null, args); client = redis.createClient.apply(null, args);
}); });
describe('monitor and transactions do not work together', function () {
it('results in a execabort', function (done) {
// Check that transactions in combination with monitor result in an error
client.monitor(function (e) {
client.on('error', function (err) {
assert.strictEqual(err.code, 'EXECABORT');
done();
});
var multi = client.multi();
multi.set('hello', 'world');
multi.exec();
});
});
it('results in a execabort #2', function (done) {
// Check that using monitor with a transactions results in an error
client.multi().set('foo', 'bar').monitor().exec(function (err, res) {
assert.strictEqual(err.code, 'EXECABORT');
done();
});
});
it('sanity check', function (done) {
// Remove the listener and add it back again after the error
var mochaListener = helper.removeMochaListener();
process.on('uncaughtException', function (err) {
helper.removeMochaListener();
process.on('uncaughtException', mochaListener);
done();
});
// Check if Redis still has the error
client.monitor();
client.send_command('multi');
client.send_command('set', ['foo', 'bar']);
client.send_command('get', ['foo']);
client.send_command('exec', function (err, res) {
// res[0] is going to be the monitor result of set
// res[1] is going to be the result of the set command
assert(utils.monitor_regex.test(res[0]));
assert.strictEqual(res[1], 'OK');
assert.strictEqual(res.length, 2);
client.end(false);
});
});
});
it('executes a pipelined multi properly in combination with the offline queue', function (done) { it('executes a pipelined multi properly in combination with the offline queue', function (done) {
var multi1 = client.multi(); var multi1 = client.multi();
multi1.set('m1', '123'); multi1.set('m1', '123');