Skip to content

Commit

Permalink
fix: don't crash if an already-drained/removed queue gets flushed aga…
Browse files Browse the repository at this point in the history
…in (#1747)
  • Loading branch information
feywind committed Jun 8, 2023
1 parent 2ec6dc9 commit 52ea441
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/publisher/message-queues.ts
Expand Up @@ -354,6 +354,14 @@ export class OrderedQueue extends MessageQueue {
* @fires OrderedQueue#drain
*/
async publish(): Promise<void> {
// If there's nothing to flush, don't try, just short-circuit to the drain event.
// This can happen if we get a publish() call after already being drained, in
// the case that topic.flush() pulls a reference to us before we get deleted.
if (!this.batches.length) {
this.emit('drain');
return;
}

this.inFlight = true;

if (this.pending) {
Expand Down
11 changes: 11 additions & 0 deletions test/publisher/message-queues.ts
Expand Up @@ -729,6 +729,17 @@ describe('Message Queues', () => {

assert.strictEqual(spy.callCount, 1);
});

it('should emit "drain" if already empty on publish', async () => {
const spy = sandbox.spy();
sandbox.stub(queue, '_publish').resolves();

queue.on('drain', spy);
await queue.publish();
await queue.publish();

assert.strictEqual(spy.callCount, 2);
});
});

describe('resumePublishing', () => {
Expand Down

0 comments on commit 52ea441

Please sign in to comment.