Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address issues with argument parsing for del command and multi commands #178

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/client/multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Multi {
* Add a new command to the queue
*/
_command(name, argList) {
if (argList.length === 1 && argList[0] instanceof Array) {
argList = argList[0];
}
const index = this._commands.length;

let callBack;
Expand Down
13 changes: 10 additions & 3 deletions lib/client/redis-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,21 @@ RedisClient.prototype.batch = function (commands) {
const getKeysVarArgs = function (args) {
var keys = [];
var hasCallback = typeof(args[args.length - 1]) === 'function';
for (var i = 0; i < (hasCallback ? args.length - 1 : args.length); i++) {
keys.push(args[i]);
if (args[0] instanceof Array && hasCallback) {
keys = args[0];
} else {
for (var i = 0; i < (hasCallback ? args.length - 1 : args.length); i++) {
keys.push(args[i]);
}
}
var callback = hasCallback ? args[args.length - 1] : undefined;
return {keys: keys, callback: callback};
return { keys: keys, callback: callback };
};

RedisClient.prototype.del = RedisClient.prototype.DEL = function (keys, callback) {
const args = getKeysVarArgs(arguments);
keys = args.keys;
callback = args.callback;
this._selectedDb.del(keys, callback);
};

Expand Down
84 changes: 84 additions & 0 deletions test/client/redis-mock.multi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,90 @@ describe("multi()", function () {
});
});

it("should handle del with multiple keys", function () {
const cmds = [
[
'del',
'mediasoup:rooms:wipe-room-state-restart:routers:30070704-f47a-40bd-ab44-36534e56b4cc'
],
[
'srem',
'mediasoup:rooms:wipe-room-state-restart:servers',
'b5ZizudER5rxZffc6RUrG'
],
[
'hdel',
'mediasoup:rooms:wipe-room-state-restart:producers:b55b81d8-0dfb-4bb8-846d-55e9474d12b6',
'router'
],
[
'srem',
'mediasoup:rooms:wipe-room-state-restart:producers',
'b55b81d8-0dfb-4bb8-846d-55e9474d12b6'
],
[
'hdel',
'mediasoup:rooms:wipe-room-state-restart:producers:c5d11e21-c69d-4b1f-a083-c98efc03a74c',
'router'
],
[
'srem',
'mediasoup:rooms:wipe-room-state-restart:producers',
'c5d11e21-c69d-4b1f-a083-c98efc03a74c'
],
[
'hdel',
'mediasoup:rooms:wipe-room-state-restart:producers:532fcd12-2a8b-47fb-8070-d2f89f9b4da3',
'router'
],
[
'srem',
'mediasoup:rooms:wipe-room-state-restart:producers',
'532fcd12-2a8b-47fb-8070-d2f89f9b4da3'
],
[
'hdel',
'mediasoup:rooms:wipe-room-state-restart:producers:ac517e34-9ca5-4439-95a7-1e0a9ca10439',
'router'
],
[
'srem',
'mediasoup:rooms:wipe-room-state-restart:producers',
'ac517e34-9ca5-4439-95a7-1e0a9ca10439'
],
[
'del',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:address',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:rooms',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:routers',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:routers:30070704-f47a-40bd-ab44-36534e56b4cc:room',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:producers',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:producers:b55b81d8-0dfb-4bb8-846d-55e9474d12b6:room',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:producers:c5d11e21-c69d-4b1f-a083-c98efc03a74c:room',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:producers:532fcd12-2a8b-47fb-8070-d2f89f9b4da3:room',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:producers:ac517e34-9ca5-4439-95a7-1e0a9ca10439:room'
],
[
'sadd',
'mediasoup:servers',
'b5ZizudER5rxZffc6RUrG'
],
[
'set',
'mediasoup:servers:b5ZizudER5rxZffc6RUrG:address',
'["1.0.0.0","2.0.0.0","127.0.0.1","JwX7Cju07gNJIII1K7bXc"]'
]
];
return new Promise((resolve, reject) => {
r.multi(cmds).exec((err, result) => {
if (err) {
return reject(err);
}
resolve(result);
});
});
});

it("should handle extraneous callbacks", function (done) {
var multi = r.multi();
multi.get('foo1').incr('foo1', function (err, result) {
Expand Down