From ed6d91b74607f920992dfea2f2712e0afcdb8027 Mon Sep 17 00:00:00 2001 From: Soroosh Merajian Date: Sat, 2 Mar 2024 23:24:55 +0100 Subject: [PATCH] fix(microservices): fix rabbitmq no-assert not being applied correctly --- packages/microservices/client/client-rmq.ts | 6 +++-- packages/microservices/server/server-rmq.ts | 6 +++-- .../test/client/client-rmq.spec.ts | 23 +++++++++++++++++- .../test/server/server-rmq.spec.ts | 24 ++++++++++++++++++- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/packages/microservices/client/client-rmq.ts b/packages/microservices/client/client-rmq.ts index 81410caedbb..b47f3474d99 100644 --- a/packages/microservices/client/client-rmq.ts +++ b/packages/microservices/client/client-rmq.ts @@ -78,7 +78,9 @@ export class ClientRMQ extends ClientProxy { this.persistent = this.getOptionsProp(this.options, 'persistent') || RQM_DEFAULT_PERSISTENT; this.noAssert = - this.getOptionsProp(this.options, 'noAssert') || RQM_DEFAULT_NO_ASSERT; + this.getOptionsProp(this.options, 'noAssert') ?? + this.queueOptions.noAssert ?? + RQM_DEFAULT_NO_ASSERT; loadPackage('amqplib', ClientRMQ.name, () => require('amqplib')); rmqPackage = loadPackage('amqp-connection-manager', ClientRMQ.name, () => @@ -187,7 +189,7 @@ export class ClientRMQ extends ClientProxy { this.getOptionsProp(this.options, 'isGlobalPrefetchCount') || RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT; - if (!this.queueOptions.noAssert) { + if (!this.noAssert) { await channel.assertQueue(this.queue, this.queueOptions); } await channel.prefetch(prefetchCount, isGlobalPrefetchCount); diff --git a/packages/microservices/server/server-rmq.ts b/packages/microservices/server/server-rmq.ts index 3f003cf94d7..7a75efcf5ef 100644 --- a/packages/microservices/server/server-rmq.ts +++ b/packages/microservices/server/server-rmq.ts @@ -65,7 +65,9 @@ export class ServerRMQ extends Server implements CustomTransportStrategy { this.getOptionsProp(this.options, 'queueOptions') || RQM_DEFAULT_QUEUE_OPTIONS; this.noAssert = - this.getOptionsProp(this.options, 'noAssert') || RQM_DEFAULT_NO_ASSERT; + this.getOptionsProp(this.options, 'noAssert') ?? + this.queueOptions.noAssert ?? + RQM_DEFAULT_NO_ASSERT; this.loadPackage('amqplib', ServerRMQ.name, () => require('amqplib')); rmqPackage = this.loadPackage( @@ -145,7 +147,7 @@ export class ServerRMQ extends Server implements CustomTransportStrategy { } public async setupChannel(channel: any, callback: Function) { - if (!this.queueOptions.noAssert) { + if (!this.noAssert) { await channel.assertQueue(this.queue, this.queueOptions); } await channel.prefetch(this.prefetchCount, this.isGlobalPrefetchCount); diff --git a/packages/microservices/test/client/client-rmq.spec.ts b/packages/microservices/test/client/client-rmq.spec.ts index 6b1533d6689..b9083e1fa4c 100644 --- a/packages/microservices/test/client/client-rmq.spec.ts +++ b/packages/microservices/test/client/client-rmq.spec.ts @@ -11,6 +11,19 @@ describe('ClientRMQ', function () { let client: ClientRMQ; + describe('constructor', () => { + it(`should fallback to queueOptions.noAssert when 'noAssert' is undefined`, () => { + const queueOptions = { + noAssert: true, + }; + const instance = new ClientRMQ({ + queueOptions, + }); + + expect(instance).property('noAssert').to.eq(queueOptions.noAssert); + }); + }); + describe('connect', () => { let createClientStub: sinon.SinonStub; let handleErrorsSpy: sinon.SinonSpy; @@ -135,10 +148,18 @@ describe('ClientRMQ', function () { afterEach(() => { consumeStub.restore(); }); - it('should call "assertQueue" with queue and queue options', async () => { + it('should call "assertQueue" with queue and queue options when noAssert is false', async () => { + client['noAssert'] = false; + await client.setupChannel(channel, () => null); expect(channel.assertQueue.calledWith(queue, queueOptions)).to.be.true; }); + it('should not call "assertQueue" when noAssert is true', async () => { + client['noAssert'] = true; + + await client.setupChannel(channel, () => null); + expect(channel.assertQueue.called).not.to.be.true; + }); it('should call "prefetch" with prefetchCount and "isGlobalPrefetchCount"', async () => { await client.setupChannel(channel, () => null); expect(channel.prefetch.calledWith(prefetchCount, isGlobalPrefetchCount)) diff --git a/packages/microservices/test/server/server-rmq.spec.ts b/packages/microservices/test/server/server-rmq.spec.ts index 31762616e29..365acd7a686 100644 --- a/packages/microservices/test/server/server-rmq.spec.ts +++ b/packages/microservices/test/server/server-rmq.spec.ts @@ -14,6 +14,20 @@ describe('ServerRMQ', () => { beforeEach(() => { server = new ServerRMQ({}); }); + + describe('constructor', () => { + it(`should fallback to queueOptions.noAssert when 'noAssert' is undefined`, () => { + const queueOptions = { + noAssert: true, + }; + const instance = new ServerRMQ({ + queueOptions, + }); + + expect(instance).property('noAssert').to.eq(queueOptions.noAssert); + }); + }); + describe('listen', () => { let createClient: sinon.SinonStub; let onStub: sinon.SinonStub; @@ -196,10 +210,18 @@ describe('ServerRMQ', () => { consume: sinon.spy(), }; }); - it('should call "assertQueue" with queue and queue options', async () => { + it('should call "assertQueue" with queue and queue options when noAssert is false', async () => { + server['noAssert' as any] = false; + await server.setupChannel(channel, () => null); expect(channel.assertQueue.calledWith(queue, queueOptions)).to.be.true; }); + it('should not call "assertQueue" when noAssert is true', async () => { + server['noAssert' as any] = true; + + await server.setupChannel(channel, () => null); + expect(channel.assertQueue.called).not.to.be.true; + }); it('should call "prefetch" with prefetchCount and "isGlobalPrefetchCount"', async () => { await server.setupChannel(channel, () => null); expect(channel.prefetch.calledWith(prefetchCount, isGlobalPrefetchCount))