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

feat(microservices): support nats queue per handler #14200

Merged
merged 2 commits into from
Nov 25, 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
8 changes: 5 additions & 3 deletions packages/microservices/server/server-nats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ export class ServerNats<
}

public bindEvents(client: Client) {
const queue = this.getOptionsProp(this.options, 'queue');
const subscribe = (channel: string) =>
const subscribe = (channel: string, queue: string) =>
client.subscribe(channel, {
queue,
callback: this.getMessageHandler(channel).bind(this),
});

const defaultQueue = this.getOptionsProp(this.options, 'queue');
const registeredPatterns = [...this.messageHandlers.keys()];
for (const channel of registeredPatterns) {
const sub = subscribe(channel);
const handlerRef = this.messageHandlers.get(channel);
const queue = handlerRef.extras?.queue ?? defaultQueue;
const sub = subscribe(channel, queue);
this.subscriptions.push(sub);
}
}
Expand Down
17 changes: 16 additions & 1 deletion packages/microservices/test/server/server-nats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,26 @@ describe('ServerNats', () => {
[pattern]: messageHandler,
});
});
it('should subscribe to each acknowledge patterns', () => {

it('should subscribe to every pattern', () => {
server.bindEvents(natsClient);
expect(subscribeSpy.calledWith(pattern)).to.be.true;
});

it('should use a per pattern queue if provided', () => {
const queue = 'test';
untypedServer.messageHandlers = objectToMap({
[pattern]: Object.assign(messageHandler, {
extras: {
queue,
},
}),
});
server.bindEvents(natsClient);
const lastCall = subscribeSpy.lastCall;
expect(lastCall.args[1].queue).to.be.eql(queue);
});

it('should fill the subscriptions array properly', () => {
server.bindEvents(natsClient);
expect(server['subscriptions'].length).to.be.equals(1);
Expand Down