add tests

This commit is contained in:
Patrick Nagurny 2015-08-21 16:13:11 -04:00
parent b0ab45f3f7
commit a9e5ee6f1a
4 changed files with 121 additions and 106 deletions

View File

@ -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');
});
};

View File

@ -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();

View File

@ -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();
});
});
});

View File

@ -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();
});
});