Skip to content

Commit

Permalink
fix: consumer registered twice during setup
Browse files Browse the repository at this point in the history
Fixes #297

If consume is called before the setup function is completed, the
same consumer can be registered twice and cause a precondition
error.
  • Loading branch information
luddd3 committed Sep 30, 2022
1 parent b9ed998 commit 1ca216a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ChannelWrapper.ts
Expand Up @@ -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 };
Expand Down
14 changes: 14 additions & 0 deletions test/ChannelWrapperTest.ts
Expand Up @@ -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. */
Expand Down

0 comments on commit 1ca216a

Please sign in to comment.