From 1ca216a47c2abdbb1f4a1e04b9032cb03db17aa2 Mon Sep 17 00:00:00 2001 From: Mathias Lundell Date: Fri, 30 Sep 2022 14:52:13 +0200 Subject: [PATCH] fix: consumer registered twice during setup Fixes #297 If consume is called before the setup function is completed, the same consumer can be registered twice and cause a precondition error. --- src/ChannelWrapper.ts | 5 +++++ test/ChannelWrapperTest.ts | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/ChannelWrapper.ts b/src/ChannelWrapper.ts index 53ff87a4..f0c26ea6 100644 --- a/src/ChannelWrapper.ts +++ b/src/ChannelWrapper.ts @@ -733,6 +733,11 @@ export default class ChannelWrapper extends EventEmitter { consumerTag, }, }; + + if (this._settingUp) { + await this._settingUp; + } + this._consumers.push(consumer); await this._consume(consumer); return { consumerTag }; diff --git a/test/ChannelWrapperTest.ts b/test/ChannelWrapperTest.ts index 917f4e07..d5881a3d 100644 --- a/test/ChannelWrapperTest.ts +++ b/test/ChannelWrapperTest.ts @@ -1283,6 +1283,20 @@ describe('ChannelWrapper', function () { expect(queue2).to.deep.equal([1]); expect(canceledTags).to.deep.equal(['1', '2']); }); + + it('should not register same consumer twice', async function () { + const setup = jest.fn().mockImplementation(() => promiseTools.delay(10)); + + const channelWrapper = new ChannelWrapper(connectionManager, { setup }); + connectionManager.simulateConnect(); + + await channelWrapper.consume('queue', () => {}); + + await channelWrapper.waitForConnect(); + + const channel = getUnderlyingChannel(channelWrapper); + expect(channel.consume).to.have.beenCalledTimes(1); + }); }); /** Returns the arguments of the most recent call to this mock. */