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

fix(connection): allow passing connection string into IORedis #2746

Merged
merged 1 commit into from
Sep 18, 2024
Merged
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: 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
Loading