Skip to content

Commit

Permalink
Merge pull request #446 from gisode/issue-445
Browse files Browse the repository at this point in the history
Issue #445: Guard against unknown exchange name in queue.unbind().
  • Loading branch information
postwait authored Apr 25, 2017
2 parents ca2e3b5 + acabe02 commit f428c23
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,17 @@ Queue.prototype.unbind = function (exchange, routingKey) {

var exchangeName = exchange instanceof Exchange ? exchange.name : exchange;

// Decrement binding count.
this._bindings[exchangeName][routingKey]--;
if (!this._bindings[exchangeName][routingKey]) {
delete this._bindings[exchangeName][routingKey];
}
if(this._bindings[exchangeName]) {
// Decrement binding count.
this._bindings[exchangeName][routingKey]--;
if (!this._bindings[exchangeName][routingKey]) {
delete this._bindings[exchangeName][routingKey];
}

// If there are no more bindings to this exchange, delete the key for the exchange.
if (!_.keys(this._bindings[exchangeName]).length){
delete this._bindings[exchangeName];
// If there are no more bindings to this exchange, delete the key for the exchange.
if (!_.keys(this._bindings[exchangeName]).length){
delete this._bindings[exchangeName];
}
}

return this._taskPush(methods.queueUnbindOk, function () {
Expand Down
27 changes: 27 additions & 0 deletions test/test-unbind-unknown-exchange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var conn = require('./harness').createConnection();

var caughtError = false;

var later = function(fun) {
setTimeout(fun, 500);
};
conn.once('ready', function () {
puts("connected to " + conn.serverProperties.product);

var q = conn.queue('node-simple-queue');

try {
q.unbind('unknown-exchange', 'routing-key');
} catch(e) {
caughtError = true;
}

later(function() {
conn.end();
process.exit();
});
});

process.addListener('exit', function () {
assert.equal(caughtError, false);
});

0 comments on commit f428c23

Please sign in to comment.