Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't crash if an already-drained/removed queue gets flushed again #1747

Merged
merged 1 commit into from Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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