Skip to content

Commit

Permalink
Avoid possible race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
jjm340 committed Nov 17, 2023
1 parent 4505a04 commit 720dcc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/lease-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
8 changes: 8 additions & 0 deletions test/lease-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 720dcc4

Please sign in to comment.