From 73005e8583110f43914df879aef3481b42f3b3af Mon Sep 17 00:00:00 2001 From: Lukas Hroch <223967+lukashroch@users.noreply.github.com> Date: Wed, 18 Sep 2024 02:32:54 +0100 Subject: [PATCH] fix(connection): allow passing connection string into IORedis (#2746) --- src/classes/redis-connection.ts | 3 +- src/interfaces/redis-options.ts | 1 + tests/test_connection.ts | 64 +++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/classes/redis-connection.ts b/src/classes/redis-connection.ts index 80150f08c0..c068765759 100644 --- a/src/classes/redis-connection.ts +++ b/src/classes/redis-connection.ts @@ -211,7 +211,8 @@ export class RedisConnection extends EventEmitter { private async init() { if (!this._client) { - this._client = new IORedis(this.opts); + const { url, ...rest } = this.opts; + this._client = url ? new IORedis(url, rest) : new IORedis(rest); } increaseMaxListeners(this._client, 3); diff --git a/src/interfaces/redis-options.ts b/src/interfaces/redis-options.ts index 4155948f08..3d31443659 100644 --- a/src/interfaces/redis-options.ts +++ b/src/interfaces/redis-options.ts @@ -2,6 +2,7 @@ import type * as IORedis from 'ioredis'; export interface BaseOptions { skipVersionCheck?: boolean; + url?: string; } export type RedisOptions = IORedis.RedisOptions & BaseOptions; diff --git a/tests/test_connection.ts b/tests/test_connection.ts index 037fe8e7a0..56ad77d5ce 100644 --- a/tests/test_connection.ts +++ b/tests/test_connection.ts @@ -37,6 +37,70 @@ describe('connection', () => { await connection.quit(); }); + describe('establish ioredis connection', () => { + it('should connect with host:port', async () => { + const queue = new Queue('valid-host-port', { + connection: { + host: 'localhost', + port: 6379, + retryStrategy: () => null, + }, + }); + + const client = await queue.waitUntilReady(); + expect(client.status).to.be.eql('ready'); + + await queue.close(); + }); + + it('should fail with invalid host:port', async () => { + const queue = new Queue('invalid-host-port', { + connection: { + host: 'localhost', + port: 9000, + retryStrategy: () => null, + }, + }); + + await expect(queue.waitUntilReady()).to.be.eventually.rejectedWith( + 'connect ECONNREFUSED 127.0.0.1:9000', + ); + }); + + it('should connect with connection URL', async () => { + const queue = new Queue('valid-url', { + connection: { + url: 'redis://localhost:6379', + // Make sure defaults are not being used + host: '1.1.1.1', + port: 2222, + retryStrategy: () => null, + }, + }); + + const client = await queue.waitUntilReady(); + expect(client.status).to.be.eql('ready'); + + await queue.close(); + }); + + it('should fail with invalid connection URL', async () => { + const queue = new Queue('invalid-url', { + connection: { + url: 'redis://localhost:9001', + // Make sure defaults are not being used + host: '1.1.1.1', + port: 2222, + retryStrategy: () => null, + }, + }); + + await expect(queue.waitUntilReady()).to.be.eventually.rejectedWith( + 'connect ECONNREFUSED 127.0.0.1:9001', + ); + }); + }); + describe('prefix', () => { it('should throw exception if using prefix with ioredis', async () => { const connection = new IORedis({