redis-mock icon indicating copy to clipboard operation
redis-mock copied to clipboard

multi(cmds).exec hangs with no output

Open gurupras opened this issue 4 years ago • 1 comments

I'm new to Redis in general, but have been using redis-mock in the past to test my code. Recently, in one of my redis libraries, I ran into a strange scenario where a test would repeatedly fail when using redis-mock but would pass when using an actual Redis server.

The following test times out every time regardless of the timeout interval.

test.only('test delete', async () => {
  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"]'
    ]
  ]
  await new Promise((resolve, reject) => {
    const client = Redis.createClient()
    client.multi(cmds).exec((err, result) => {
      if (err) {
        return reject(err)
      }
      resolve(result)
    })
  })
})

environment node: v12.13.0 redis-mock: 0.49.0 jest: 26.0.1 @babel/core: 7.9.6 babel-jest: 26.0.1

gurupras avatar May 05 '20 16:05 gurupras

I think this is caused by an issue in lib/client/redis-client.js where the del function does not correctly handle multiple arguments. Instead of

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

it should be

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

but it requires a tweak to getKeysVarArgs as well. I've submitted a PR that addresses this.

cfredhart avatar Feb 07 '21 06:02 cfredhart