From 9c90f05c736dd3c9921b082b2353d3cbe0cdaa34 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 24 May 2016 13:35:08 -0400 Subject: [PATCH] test: more coverage for bitcoind --- test/services/bitcoind.unit.js | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index cde71c63..39cf7e97 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -1176,6 +1176,61 @@ describe('Bitcoin Service', function() { done(); }, 200); }); + it('it will clear interval if node is stopping', function(done) { + var config = { + node: { + network: bitcore.Networks.testnet + }, + spawn: { + datadir: 'testdir', + exec: 'testpath' + } + }; + var bitcoind = new BitcoinService(config); + var getBestBlockHash = sinon.stub().callsArgWith(0, {code: -1, message: 'error'}); + var node = { + _tipUpdateInterval: 1, + client: { + getBestBlockHash: getBestBlockHash + } + }; + bitcoind._checkSyncedAndSubscribeZmqEvents(node); + setTimeout(function() { + bitcoind.node.stopping = true; + var count = getBestBlockHash.callCount; + setTimeout(function() { + getBestBlockHash.callCount.should.equal(count); + done(); + }, 100); + }, 100); + }); + it('will not set interval if synced is true', function(done) { + var bitcoind = new BitcoinService(baseConfig); + bitcoind._updateTip = sinon.stub(); + bitcoind._subscribeZmqEvents = sinon.stub(); + var getBestBlockHash = sinon.stub().callsArgWith(0, null, { + result: '00000000000000001bb82a7f5973618cfd3185ba1ded04dd852a653f92a27c45' + }); + var info = { + result: { + verificationprogress: 1.00 + } + }; + var getBlockchainInfo = sinon.stub().callsArgWith(0, null, info); + var node = { + _tipUpdateInterval: 1, + client: { + getBestBlockHash: getBestBlockHash, + getBlockchainInfo: getBlockchainInfo + } + }; + bitcoind._checkSyncedAndSubscribeZmqEvents(node); + setTimeout(function() { + getBestBlockHash.callCount.should.equal(1); + getBlockchainInfo.callCount.should.equal(1); + done(); + }, 200); + }); }); describe('#_subscribeZmqEvents', function() { @@ -1224,6 +1279,24 @@ describe('Bitcoin Service', function() { var message = new Buffer('abcdef', 'hex'); node.zmqSubSocket.emit('message', topic, message); }); + it('will ignore unknown topic types', function(done) { + var bitcoind = new BitcoinService(baseConfig); + bitcoind._zmqBlockHandler = sinon.stub(); + bitcoind._zmqTransactionHandler = sinon.stub(); + var node = { + zmqSubSocket: new EventEmitter() + }; + node.zmqSubSocket.subscribe = sinon.stub(); + bitcoind._subscribeZmqEvents(node); + node.zmqSubSocket.on('message', function() { + bitcoind._zmqBlockHandler.callCount.should.equal(0); + bitcoind._zmqTransactionHandler.callCount.should.equal(0); + done(); + }); + var topic = new Buffer('unknown', 'utf8'); + var message = new Buffer('abcdef', 'hex'); + node.zmqSubSocket.emit('message', topic, message); + }); }); describe('#_initZmqSubSocket', function() {