Skip to content

Commit 60e9d0f

Browse files
author
Ruben Bridgewater
committed
Fix multi regression. Closes #889
Allow commands being executed after a Multi / Batch was initiated but not yet executed
1 parent 28d343c commit 60e9d0f

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

index.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -869,14 +869,9 @@ RedisClient.prototype.end = function (flush) {
869869
return this.stream.destroySoon();
870870
};
871871

872-
function Multi(client, args, transaction) {
873-
client.stream.cork();
872+
function Multi(client, args) {
874873
this._client = client;
875874
this.queue = new Queue();
876-
if (transaction) {
877-
this.exec = this.exec_transaction;
878-
this.EXEC = this.exec_transaction;
879-
}
880875
var command, tmp_args;
881876
if (Array.isArray(args)) {
882877
while (tmp_args = args.shift()) {
@@ -892,11 +887,13 @@ function Multi(client, args, transaction) {
892887
}
893888

894889
RedisClient.prototype.multi = RedisClient.prototype.MULTI = function (args) {
895-
return new Multi(this, args, true);
890+
var multi = new Multi(this, args);
891+
multi.exec = multi.EXEC = multi.exec_transaction;
892+
return multi;
896893
};
897894

898895
RedisClient.prototype.batch = RedisClient.prototype.BATCH = function (args) {
899-
return new Multi(this, args, false);
896+
return new Multi(this, args);
900897
};
901898

902899
commands.forEach(function (fullCommand) {
@@ -1077,6 +1074,7 @@ Multi.prototype.exec_transaction = function (callback) {
10771074
var cb;
10781075
this.errors = [];
10791076
this.callback = callback;
1077+
this._client.stream.cork();
10801078
this._client.pipeline = len + 2;
10811079
this.wants_buffers = new Array(len);
10821080
this.send_command('multi', []);
@@ -1194,6 +1192,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
11941192
return true;
11951193
}
11961194
this.results = new Array(len);
1195+
this._client.stream.cork();
11971196
this._client.pipeline = len;
11981197
var lastCallback = function (cb) {
11991198
return function (err, res) {

test/batch.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ describe("The 'batch' method", function () {
7272
});
7373
});
7474

75+
it("runs normal calls inbetween batch", function (done) {
76+
var batch = client.batch();
77+
batch.set("m1", "123");
78+
client.set('m2', '456', done);
79+
});
80+
7581
it("returns an empty array if promisified", function () {
7682
return client.batch().execAsync().then(function(res) {
7783
assert.strictEqual(res.length, 0);

test/commands/multi.spec.js

+41
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,47 @@ describe("The 'multi' method", function () {
7373
assert.strictEqual(notBuffering, true);
7474
});
7575

76+
it("runs normal calls inbetween multis", function (done) {
77+
var multi1 = client.multi();
78+
multi1.set("m1", "123");
79+
client.set('m2', '456', done);
80+
});
81+
82+
it("runs simultaneous multis with the same client", function (done) {
83+
var end = helper.callFuncAfter(done, 2);
84+
85+
var multi1 = client.multi();
86+
multi1.set("m1", "123");
87+
multi1.get('m1');
88+
89+
var multi2 = client.multi();
90+
multi2.set("m2", "456");
91+
multi2.get('m2');
92+
93+
multi1.exec(end);
94+
multi2.exec(function(err, res) {
95+
assert.strictEqual(res[1], '456');
96+
end();
97+
});
98+
});
99+
100+
it("runs simultaneous multis with the same client version 2", function (done) {
101+
var end = helper.callFuncAfter(done, 2);
102+
var multi2 = client.multi();
103+
var multi1 = client.multi();
104+
105+
multi2.set("m2", "456");
106+
multi1.set("m1", "123");
107+
multi1.get('m1');
108+
multi2.get('m2');
109+
110+
multi1.exec(end);
111+
multi2.exec(function(err, res) {
112+
assert.strictEqual(res[1], '456');
113+
end();
114+
});
115+
});
116+
76117
it('roles back a transaction when one command in a sequence of commands fails', function (done) {
77118
var multi1, multi2;
78119
var expected = helper.serverVersionAtLeast(client, [2, 6, 5]) ? helper.isError() : function () {};

0 commit comments

Comments
 (0)