From 0cdf8969d658eebaff891f6d873da0e01499aadb Mon Sep 17 00:00:00 2001 From: McFly Date: Wed, 13 Mar 2024 01:03:36 +0100 Subject: [PATCH] fix(microservices): fix usage noassert flag in rabbitmq --- packages/microservices/client/client-rmq.ts | 2 +- packages/microservices/server/server-rmq.ts | 2 +- packages/microservices/test/client/client-rmq.spec.ts | 8 ++++++++ packages/microservices/test/server/server-rmq.spec.ts | 10 ++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/microservices/client/client-rmq.ts b/packages/microservices/client/client-rmq.ts index 81410caedbb..aaea663950b 100644 --- a/packages/microservices/client/client-rmq.ts +++ b/packages/microservices/client/client-rmq.ts @@ -187,7 +187,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..f6e89e5dfb2 100644 --- a/packages/microservices/server/server-rmq.ts +++ b/packages/microservices/server/server-rmq.ts @@ -145,7 +145,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..2f78aa5fa71 100644 --- a/packages/microservices/test/client/client-rmq.spec.ts +++ b/packages/microservices/test/client/client-rmq.spec.ts @@ -119,6 +119,7 @@ describe('ClientRMQ', function () { const prefetchCount = 10; let consumeStub: sinon.SinonStub; + let noAssertStub: sinon.SinonStub; let channel: any = {}; beforeEach(() => { @@ -131,14 +132,21 @@ describe('ClientRMQ', function () { prefetch: sinon.spy(), }; consumeStub = sinon.stub(client, 'consumeChannel').callsFake(() => null); + noAssertStub = sinon.stub(client as any, 'noAssert').value(false); }); afterEach(() => { consumeStub.restore(); + noAssertStub.restore(); }); it('should call "assertQueue" with queue and queue options', async () => { await client.setupChannel(channel, () => null); expect(channel.assertQueue.calledWith(queue, queueOptions)).to.be.true; }); + it('should not call "assertQueue" when "noAssert" flag is true', async () => { + sinon.stub(client as any, 'noAssert').value(true); + await client.setupChannel(channel, () => null); + expect(channel.assertQueue.notCalled).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..68e865df601 100644 --- a/packages/microservices/test/server/server-rmq.spec.ts +++ b/packages/microservices/test/server/server-rmq.spec.ts @@ -182,6 +182,7 @@ describe('ServerRMQ', () => { const isGlobalPrefetchCount = true; const prefetchCount = 10; + let noAssertStub: sinon.SinonStub; let channel: any = {}; beforeEach(() => { @@ -195,11 +196,20 @@ describe('ServerRMQ', () => { prefetch: sinon.spy(), consume: sinon.spy(), }; + noAssertStub = sinon.stub(server as any, 'noAssert').value(false); + }); + afterEach(() => { + noAssertStub.restore(); }); it('should call "assertQueue" with queue and queue options', async () => { await server.setupChannel(channel, () => null); expect(channel.assertQueue.calledWith(queue, queueOptions)).to.be.true; }); + it('should not call "assertQueue" when "noAssert" flag is true', async () => { + sinon.stub(server as any, 'noAssert').value(true); + await server.setupChannel(channel, () => null); + expect(channel.assertQueue.notCalled).to.be.true; + }); it('should call "prefetch" with prefetchCount and "isGlobalPrefetchCount"', async () => { await server.setupChannel(channel, () => null); expect(channel.prefetch.calledWith(prefetchCount, isGlobalPrefetchCount))