Skip to content

Commit

Permalink
fix(connection): allow passing connection string into IORedis (#2746)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashroch authored Sep 18, 2024
1 parent f6546fb commit 73005e8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/classes/redis-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/redis-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type * as IORedis from 'ioredis';

export interface BaseOptions {
skipVersionCheck?: boolean;
url?: string;
}

export type RedisOptions = IORedis.RedisOptions & BaseOptions;
Expand Down
64 changes: 64 additions & 0 deletions tests/test_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 73005e8

Please sign in to comment.