Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/BackbeatConsumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class BackbeatConsumer extends EventEmitter {
// a deferred un-assign gives up when this no longer matches
this._rebalanceId = 0;
this._shuttingDown = false;
this._closeCallbacks = null;

this._messagesConsumed = 0;
// this variable represents how many kafka messages have been
Expand Down Expand Up @@ -1341,6 +1342,12 @@ class BackbeatConsumer extends EventEmitter {
* @return {undefined}
*/
close(cb) {
if (this._closeCallbacks) {
this._closeCallbacks.push(cb);
return undefined;
}
this._closeCallbacks = [cb];

if (this._publishOffsetsCronTimer) {
clearInterval(this._publishOffsetsCronTimer);
this._publishOffsetsCronTimer = null;
Expand Down Expand Up @@ -1393,7 +1400,11 @@ class BackbeatConsumer extends EventEmitter {
this._consumer.disconnect();
return undefined;
},
], () => cb());
], () => {
const callbacks = this._closeCallbacks;
this._closeCallbacks = null;
callbacks.forEach(done => done());
});
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/backbeatConsumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,33 @@ describe('backbeatConsumer', () => {
});
});

it('should answer every caller once when closed twice', done => {
queueIdle = false;
ledgerCount = 1;

let first = false;
let second = 0;
consumer.close(() => {
first = true;
});
consumer.close(() => {
second++;
});

setImmediate(() => {
queueIdle = true;
ledgerCount = 0;
drainCallbacks[drainCallbacks.length - 1]();

setTimeout(() => {
assert.strictEqual(first, true);
assert.strictEqual(second, 1);
assert(consumer._consumer.disconnect.calledOnce);
done();
}, 20);
});
});

it('should not wait for the drain once the consumer is disconnected', done => {
// the drain watchdog fires on a wedged task and disconnects, so the
// work it is waiting on can never complete
Expand Down
Loading