diff --git a/src/lease-manager.ts b/src/lease-manager.ts index d7671ec10..69e51ec24 100644 --- a/src/lease-manager.ts +++ b/src/lease-manager.ts @@ -270,9 +270,8 @@ export class LeaseManager extends EventEmitter { */ private _dispense(message: Message): void { if (this._subscriber.isOpen) { + message.dispensed = true; process.nextTick(() => { - // Tell the message its dispensed, make sure you do this first as emit blocks - message.dispensed = true; this._subscriber.emit('message', message); }); } diff --git a/test/lease-manager.ts b/test/lease-manager.ts index 7b0de116f..b9a8305f5 100644 --- a/test/lease-manager.ts +++ b/test/lease-manager.ts @@ -402,12 +402,20 @@ describe('LeaseManager', () => { describe('clearNonDispensedMessages', () => { it('should only clear messages with dispensed=false', () => { + leaseManager.setOptions({ + maxMessages: 2, + maxBytes: 10, + allowExcessMessages: false, + }); + const message = new FakeMessage(); const message2 = new FakeMessage(); + message.dispensed = true; message2.dispensed = false; leaseManager.add(message as {} as Message); leaseManager.add(message2 as {} as Message); + leaseManager.clearNonDispensedMessages(); assert.strictEqual(leaseManager.size, 1); assert.strictEqual(leaseManager.bytes, message.length);