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(rabbitmq): return consumerTag when creating subscriber #769

Merged
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
25 changes: 9 additions & 16 deletions packages/rabbitmq/src/amqp/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,21 +424,18 @@ export class AmqpConnection {
consumeOptions?: ConsumeOptions
): Promise<SubscriptionResult> {
return new Promise((res) => {
let result: SubscriptionResult;
this.selectManagedChannel(msgOptions?.queueOptions?.channel)
.addSetup(async (channel) => {
this.selectManagedChannel(msgOptions?.queueOptions?.channel).addSetup(
async (channel) => {
const consumerTag = await this.setupSubscriberChannel<T>(
handler,
msgOptions,
channel,
originalHandlerName,
consumeOptions
);
result = { consumerTag };
})
.then(() => {
res(result);
});
res({ consumerTag });
}
);
});
}

Expand Down Expand Up @@ -532,20 +529,16 @@ export class AmqpConnection {
rpcOptions: MessageHandlerOptions
): Promise<SubscriptionResult> {
return new Promise((res) => {
let result: SubscriptionResult;
this.selectManagedChannel(rpcOptions?.queueOptions?.channel)
.addSetup(async (channel) => {
this.selectManagedChannel(rpcOptions?.queueOptions?.channel).addSetup(
async (channel) => {
const consumerTag = await this.setupRpcChannel<T, U>(
handler,
rpcOptions,
channel
);
result = { consumerTag };
res({ consumerTag });
})
.then(() => {
res(result);
});
}
);
});
}

Expand Down
58 changes: 58 additions & 0 deletions packages/rabbitmq/src/tests/amqp.connection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { AmqpConnection } from '../amqp/connection';

const mockConsumerTag = 'amq.ctag-mock';

const mockChannel = 'mockChannel';
const mockConfig = {
hostname: 'localhost',
port: 5672,
username: 'guest',
password: 'guest',
vhost: '/',
uri: 'amqp://guest:guest@localhost:5672/',
};

describe('AmqpConnection', () => {
let connection: AmqpConnection;

beforeEach(async () => {
jest
.spyOn(AmqpConnection.prototype as any, 'selectManagedChannel')
.mockReturnValue({
addSetup: jest.fn((callback) => callback(mockChannel)),
});
jest
.spyOn(AmqpConnection.prototype as any, 'setupSubscriberChannel')
.mockReturnValue(mockConsumerTag);
jest
.spyOn(AmqpConnection.prototype as any, 'setupRpcChannel')
.mockReturnValue(mockConsumerTag);

connection = new AmqpConnection(mockConfig);
});

it('should return consumer tag when resolves', async () => {
const mockHandler = jest.fn();
const mockMsgOptions = { queueOptions: { channel: mockChannel } };
const mockHandlerName = 'mockHandlerName';
const mockConsumeOptions = {};

const result = await connection.createSubscriber(
mockHandler,
mockMsgOptions,
mockHandlerName,
mockConsumeOptions
);

expect(result).toEqual({ consumerTag: mockConsumerTag });
});

it('should return consumer tag when resolves', async () => {
const mockHandler = jest.fn();
const mockRpcOptions = { queueOptions: { channel: mockChannel } };

const result = await connection.createRpc(mockHandler, mockRpcOptions);

expect(result).toEqual({ consumerTag: mockConsumerTag });
});
});
Loading