From a9e5ee6f1a2645f8775c0ca5b1473da3702864b5 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Fri, 21 Aug 2015 16:13:11 -0400 Subject: [PATCH] add tests --- lib/node.js | 15 ++-- test/chain.unit.js | 18 +++++ test/db.unit.js | 27 +++++++- test/node.unit.js | 167 +++++++++++++++++++-------------------------- 4 files changed, 121 insertions(+), 106 deletions(-) diff --git a/lib/node.js b/lib/node.js index 6495dcfd..04fce0bd 100644 --- a/lib/node.js +++ b/lib/node.js @@ -279,14 +279,18 @@ Node.prototype._syncBitcoind = function() { } }); }, function(err) { - - self.bitcoindSyncing = false; - self.chain.lastSavedMetadataThreshold = 0; if (err) { Error.captureStackTrace(err); return self.emit('error', err); } + if(self.stopping) { + return; + } + + self.bitcoindSyncing = false; + self.chain.lastSavedMetadataThreshold = 0; + // If bitcoind is completely synced if (self.bitcoind.isSynced()) { self.emit('synced'); @@ -417,7 +421,6 @@ Node.prototype._initialize = function() { this.chain.on('ready', function() { log.info('Bitcoin Chain Ready'); self._syncBitcoind(); - self.emit('ready'); }); this.chain.on('error', function(err) { @@ -437,8 +440,10 @@ Node.prototype._initialize = function() { this.start(function(err) { if(err) { - log.error('Failed starting services: ' + err); + return self.emit('error', err); } + + self.emit('ready'); }); }; diff --git a/test/chain.unit.js b/test/chain.unit.js index 9d463005..ff432419 100644 --- a/test/chain.unit.js +++ b/test/chain.unit.js @@ -25,6 +25,24 @@ describe('Bitcoin Chain', function() { }); + describe('#start', function() { + it('should call the callback when base chain is initialized', function(done) { + var chain = new Chain(); + chain.initialize = function() { + chain.emit('initialized'); + }; + + chain.start(done); + }); + }); + + describe('#stop', function() { + it('should call the callback', function(done) { + var chain = new Chain(); + chain.stop(done); + }); + }); + describe('#_writeBlock', function() { it('should update hashes and call putBlock', function(done) { var chain = new Chain(); diff --git a/test/db.unit.js b/test/db.unit.js index e4081a8e..13977f71 100644 --- a/test/db.unit.js +++ b/test/db.unit.js @@ -16,7 +16,7 @@ var Transaction = bitcore.Transaction; describe('Bitcoin DB', function() { var coinbaseAmount = 50 * 1e8; - describe('#initialize', function() { + describe('#start', function() { it('should emit ready', function(done) { var db = new DB({store: memdown}); db._modules = ['mod1', 'mod2']; @@ -24,8 +24,29 @@ describe('Bitcoin DB', function() { on: sinon.spy() }; db.addModule = sinon.spy(); - db.on('ready', done); - db.initialize(); + var readyFired = false; + db.on('ready', function() { + readyFired = true; + }); + db.start(function() { + readyFired.should.equal(true); + done(); + }); + }); + }); + + describe('#stop', function() { + it('should close the store', function(done) { + var db = new DB({store: memdown}); + db.store = { + close: sinon.stub().callsArg(0) + }; + + db.stop(function(err) { + should.not.exist(err); + db.store.close.calledOnce.should.equal(true); + done(); + }); }); }); diff --git a/test/node.unit.js b/test/node.unit.js index 203476ad..deb81863 100644 --- a/test/node.unit.js +++ b/test/node.unit.js @@ -273,7 +273,50 @@ describe('Bitcoind Node', function() { }); node._syncBitcoind(); }); + it('will stop syncing when the node is stopping', function(done) { + var node = new Node({}); + node.Block = Block; + node.bitcoindHeight = 1; + var blockBuffer = new Buffer(blockData); + var block = Block.fromBuffer(blockBuffer); + node.bitcoind = { + getBlock: sinon.stub().callsArgWith(1, null, blockBuffer), + isSynced: sinon.stub().returns(true) + }; + node.chain = { + tip: { + __height: 0, + hash: block.prevHash + }, + saveMetadata: sinon.stub(), + emit: sinon.stub(), + cache: { + hashes: {} + } + }; + node.db = { + _onChainAddBlock: function(block, callback) { + node.chain.tip.__height += 1; + callback(); + } + }; + node.stopping = true; + + var synced = false; + + node.on('synced', function() { + synced = true; + }); + + node._syncBitcoind(); + + setTimeout(function() { + synced.should.equal(false); + done(); + }, 10); + }); }); + describe('#_loadNetwork', function() { it('should use the testnet network if testnet is specified', function() { var config = { @@ -414,101 +457,26 @@ describe('Bitcoind Node', function() { }); }); - describe('#_initializeBitcoind', function() { - it('will call db.initialize() on ready event', function(done) { - var node = new Node({}); - node.bitcoind = new EventEmitter(); - node.bitcoind.getInfo = sinon.stub().returns({blocks: 10}); - node.db = { - initialize: sinon.spy() - }; - sinon.stub(chainlib.log, 'info'); - node.bitcoind.on('ready', function() { - setImmediate(function() { - chainlib.log.info.callCount.should.equal(1); - chainlib.log.info.restore(); - node.db.initialize.callCount.should.equal(1); - node.bitcoindHeight.should.equal(10); - done(); - }); - }); - node._initializeBitcoind(); - node.bitcoind.emit('ready'); - }); - it('will call emit an error from libbitcoind', function(done) { - var node = new Node({}); - node.bitcoind = new EventEmitter(); - node.on('error', function(err) { - should.exist(err); - err.message.should.equal('test error'); - done(); - }); - node._initializeBitcoind(); - node.bitcoind.emit('error', new Error('test error')); - }); - }); - - describe('#_initializeDatabase', function() { - it('will call chain.initialize() on ready event', function(done) { - var node = new Node({}); - node.db = new EventEmitter(); - node.db.addModule = sinon.spy(); - var module = {}; - node.db._modules = [module]; - node.chain = { - initialize: sinon.spy() - }; - sinon.stub(chainlib.log, 'info'); - node.db.on('ready', function() { - setImmediate(function() { - chainlib.log.info.callCount.should.equal(1); - chainlib.log.info.restore(); - node.chain.initialize.callCount.should.equal(1); - done(); - }); - }); - node._initializeDatabase(); - node.db.emit('ready'); - }); - it('will call emit an error from db', function(done) { - var node = new Node({}); - node.db = new EventEmitter(); - node.on('error', function(err) { - should.exist(err); - err.message.should.equal('test error'); - done(); - }); - node._initializeDatabase(); - node.db.emit('error', new Error('test error')); - }); - }); - - describe('#_initializeChain', function() { - it('will call emit an error from chain', function(done) { - var node = new Node({}); - node.chain = new EventEmitter(); - node.on('error', function(err) { - should.exist(err); - err.message.should.equal('test error'); - done(); - }); - node._initializeChain(); - node.chain.emit('error', new Error('test error')); - }); - }); - describe('#_initialize', function() { + var node = new Node({}); + node.chain = { + on: sinon.spy() + }; + node.Block = 'Block'; + node.bitcoind = { + on: sinon.spy() + }; + node.db = { + on: sinon.spy() + }; it('should initialize', function(done) { - var node = new Node({}); - node.chain = {}; - node.Block = 'Block'; - node.bitcoind = 'bitcoind'; - node.db = {}; + node.once('ready', function() { + done(); + }); + + node.start = sinon.stub().callsArg(0); - node._initializeBitcoind = sinon.spy(); - node._initializeDatabase = sinon.spy(); - node._initializeChain = sinon.spy(); node._initialize(); // references @@ -518,18 +486,21 @@ describe('Bitcoind Node', function() { node.chain.db.should.equal(node.db); node.chain.db.should.equal(node.db); - // events - node._initializeBitcoind.callCount.should.equal(1); - node._initializeDatabase.callCount.should.equal(1); - node._initializeChain.callCount.should.equal(1); - // start syncing node.setSyncStrategy = sinon.spy(); - node.on('ready', function() { + + }); + + it('should emit an error if an error occurred starting services', function(done) { + node.once('error', function(err) { + should.exist(err); + err.message.should.equal('error'); done(); }); - node.emit('ready'); + node.start = sinon.stub().callsArgWith(0, new Error('error')); + + node._initialize(); }); });