From 0cb795d9802b8d240baf64dc85b8a9b7f7e17f38 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 24 May 2016 16:22:41 -0400 Subject: [PATCH] test: add bitcoind test for early shutdown while connecting --- lib/services/bitcoind.js | 7 ++++++- test/services/bitcoind.unit.js | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 69063d3f..788066bb 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -886,10 +886,12 @@ Bitcoin.prototype._spawnChildProcess = function(callback) { Bitcoin.prototype._connectProcess = function(config, callback) { var self = this; var node = {}; + var exitShutdown = false; async.retry({times: 60, interval: self.startRetryInterval}, function(done) { if (self.node.stopping) { - return done(new Error('Stopping while trying to connect to bitcoind.')); + exitShutdown = true; + return done(); } node.client = new BitcoinRPC({ @@ -906,6 +908,9 @@ Bitcoin.prototype._connectProcess = function(config, callback) { if (err) { return callback(err); } + if (exitShutdown) { + return callback(new Error('Stopping while trying to connect to bitcoind.')); + } self._initZmqSubSocket(node, config.zmqpubrawtx); self._subscribeZmqEvents(node); diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index 39ef2296..285a91f1 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -1738,6 +1738,27 @@ describe('Bitcoin Service', function() { }); describe('#_connectProcess', function() { + it('will give error if connecting while shutting down', function(done) { + var config = { + node: { + network: bitcore.Networks.testnet + }, + spawn: { + datadir: 'testdir', + exec: 'testpath' + } + }; + var bitcoind = new BitcoinService(config); + bitcoind.node.stopping = true; + bitcoind.startRetryInterval = 100; + bitcoind._loadTipFromNode = sinon.stub(); + bitcoind._connectProcess({}, function(err) { + err.should.be.instanceof(Error); + err.message.should.match(/Stopping while trying to connect/); + bitcoind._loadTipFromNode.callCount.should.equal(0); + done(); + }); + }); it('will give error from loadTipFromNode after 60 retries', function(done) { var bitcoind = new BitcoinService(baseConfig); bitcoind._loadTipFromNode = sinon.stub().callsArgWith(1, new Error('test'));