From 87bab26e882aae9aff7fe26266fdfd025a772e03 Mon Sep 17 00:00:00 2001 From: "jhaugh42@gmail.com" Date: Mon, 20 Jun 2016 16:38:14 -0400 Subject: [PATCH] teardown tests completed --- lib/fudd.js | 23 ++++++++++++---------- test/unit/fudd.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/lib/fudd.js b/lib/fudd.js index dee0659..d5a0e0b 100644 --- a/lib/fudd.js +++ b/lib/fudd.js @@ -39,17 +39,20 @@ var Fudd = { series(establishChannel, function(error, connection, channel) { if(error) return finalCallback(error); - var teardownMethods = []; - //config.exchanges + var infrastructure = []; - mapEach(config.exchanges, Fudd._deleteExchange.bind(null, channel), function(err) { - if (err) return finalCallback(err); + config.exchanges.reduce(function(series, exchangeDefinition) { + infrastructure.push(Fudd._deleteExchange.bind(null, channel, exchangeDefinition)); + }, infrastructure); - mapEach(config.queues, Fudd._deleteQueue.bind(null, channel), function(err) { - if (err) return finalCallback(err); + config.queues.reduce(function(series, queueDefinition) { + series.push(Fudd._deleteQueue.bind(null, channel, queueDefinition)); + return series; + }, infrastructure); - finalCallback(); - }); + series(infrastructure, function(error) { + if (error) return finalCallback(error); + Fudd._disconnect(connection, finalCallback); }); }); }, @@ -77,10 +80,10 @@ var Fudd = { } }, _deleteExchange: function(channel, exchangeDefinition, callback) { - channel.deleteExchange(exchangeDefinition.name, {}, callback); + channel.deleteExchange(exchangeDefinition.name, {}, function(error) { callback(error)}); }, _deleteQueue: function(channel, queueDefinition, callback) { - channel.deleteQueue(queueDefinition.name, {}, callback); + channel.deleteQueue(queueDefinition.name, {}, function(error) { callback(error)}); }, _connect: function(config, callback) { var url = formatAmqpUrl(config.cluster); diff --git a/test/unit/fudd.js b/test/unit/fudd.js index 5c598bc..ab3bb05 100644 --- a/test/unit/fudd.js +++ b/test/unit/fudd.js @@ -179,6 +179,55 @@ describe('fudd', function() { deleteQueueBindStub.reset(); Fudd.teardown(config, callbackSpy); }); + + it('should bind config to the _connect function', function() { + expect(connectBindStub.args[0]).to.eql([ + null, config + ]); + }); + + it('should invoke series with the bound _connect and Fudd._create channel functions', function() { + expect(seriesStub.args[0][0]).to.eql([boundConnect, Fudd._createChannel]); + }); + + it('should call the finalCallback if the first series call calls back with an error', function() { + var expectedError = new Error('things happened'); + seriesStub.callArgWith(1, expectedError); + expect(callbackSpy.args[0]).to.eql([expectedError]); + }); + + it('should call _deleteExchange.bind for each exchange in the config', function() { + seriesStub.callArgWith(1, null, 'connection', 'channel'); + expect(deleteExchangeBindStub.callCount).to.equal(config.exchanges.length); + }); + + it('should call _deleteExchange.bind for each queue in the config', function() { + seriesStub.callArgWith(1, null, 'connection', 'channel'); + expect(deleteQueueBindStub.callCount).to.equal(config.queues.length); + }); + + it('should invoke series again with a sequence of functions derived from the config', function() { + seriesStub.callArgWith(1, null, 'connection', 'channel'); + expect(seriesStub.args[1][0]).to.eql([ + boundDeleteExchange, + boundDeleteQueue + ]); + }); + + it('should call the final callback with the error returned from creating the infrastructure', function() { + var expectedError = new Error('error creating infrastructure'); + seriesStub.callArgWith(1, null, 'connection', 'channel'); + seriesStub.callArgWith(1, expectedError); + expect(callbackSpy.args[0]).to.eql([expectedError]); + }); + + it('should invoke Fudd._disconnect wtih the connection and callback', function() { + seriesStub.callArgWith(1, null, 'connection', 'channel'); + seriesStub.callArgWith(1, null); + expect(disconnectStub.args[0]).to.eql([ + 'connection', callbackSpy + ]); + }); }); }); });