From 16439eac8b0a749dcdcaba74d4119d82134ae966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=BCrbach?= Date: Sun, 24 Mar 2019 21:04:07 +0100 Subject: [PATCH] Error pending confirmation callbacks on channel close Otherwise waitForConfirms never terminates or terminates node --- lib/channel.js | 6 ++++++ test/channel_api.js | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/channel.js b/lib/channel.js index d2b8786b..fc8f17d6 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -33,6 +33,12 @@ function Channel(connection) { this.on('nack', this.handleConfirm.bind(this, function(cb) { if (cb) cb(new Error('message nacked')); })); + this.on('close', function () { + var cb; + while (cb = this.unconfirmed.shift()) { + if (cb) cb(new Error('channel closed')); + } + }) // message frame state machine this.handleMessage = acceptDeliveryOrReturn; } diff --git a/test/channel_api.js b/test/channel_api.js index 99c9f9c4..bd5473f0 100644 --- a/test/channel_api.js +++ b/test/channel_api.js @@ -581,4 +581,17 @@ confirmtest('wait for confirms', function(ch) { return ch.waitForConfirms(); }) +confirmtest('works when channel is closed', function(ch) { + for (var i=0; i < 1000; i++) { + ch.publish('', '', Buffer.from('foobar'), {}); + } + return ch.close().then(function () { + return ch.waitForConfirms() + }).then(function () { + assert.strictEqual(true, false, 'Wait should have failed.') + }, function (e) { + assert.strictEqual(e.message, 'channel closed') + }); +}); + });