Bug lazyConnect does not allow multiple connect() calls #1192
Open
Description
When a client is created using lazyConnect
, then a connect()
is required and it works fine.
However, when a disconnect()
is called, a new connect()
is not allowed anymore.
Let's consider the following example:
const Redis = require('ioredis-mock');
console.log('Creating client...');
const client = new Redis('redis://localhost:6379', { lazyConnect: true });
console.log(`client.connected: ${client.connected}`);
let attempt = 1;
session()
.then(session)
.catch(e => console.error(e));
async function session() {
console.log('\nAttempt #' + attempt++);
console.log('connecting...');
await client.connect();
console.log(`After connect(): client.connected: ${client.connected}`);
console.log('disconnecting...');
await client.disconnect();
console.log(`After disconnect(): client.connected: ${client.connected}`);
}
The output is:
Creating client...
client.connected: false
Attempt #1
connecting...
After connect(): client.connected: true
disconnecting...
After disconnect(): client.connected: true
Attempt #2
connecting...
Error: Redis is already connecting/connected
at RedisMock.connect (/tmp/ioredis/node_modules/ioredis-mock/lib/index.js:2220:11)
at safelyExecuteCommand (/tmp/ioredis/node_modules/ioredis-mock/lib/index.js:1713:16)
at /tmp/ioredis/node_modules/ioredis-mock/lib/index.js:1721:192
at new Promise (<anonymous>)
at RedisMock.connect (/tmp/ioredis/node_modules/ioredis-mock/lib/index.js:1721:159)
at session (/tmp/ioredis/test.js:16:18)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Solution
Perhaps I'm being too simplistic, but it seems the solution is updating the connected
property to false
on the disconnect()
method.