Merge pull request #212 from pnagurny/feature/blockHandlerCheck

Check for blockHandler on services
This commit is contained in:
Braydon Fuller 2015-09-09 15:12:11 -04:00
commit cd73763610
2 changed files with 44 additions and 9 deletions

View File

@ -366,15 +366,22 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
async.eachSeries( async.eachSeries(
this.node.services, this.node.services,
function(mod, next) { function(mod, next) {
mod.blockHandler.call(mod, block, add, function(err, ops) { if(mod.blockHandler) {
if (err) { $.checkArgument(typeof mod.blockHandler === 'function', 'blockHandler must be a function');
return next(err);
} mod.blockHandler.call(mod, block, add, function(err, ops) {
if (ops) { if (err) {
operations = operations.concat(ops); return next(err);
} }
next(); if (ops) {
}); $.checkArgument(Array.isArray(ops), 'blockHandler for ' + mod.name + ' returned non-array');
operations = operations.concat(ops);
}
next();
});
} else {
setImmediate(next);
}
}, },
function(err) { function(err) {
if (err) { if (err) {

View File

@ -630,6 +630,9 @@ describe('DB Service', function() {
Service1.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op1', 'op2', 'op3']); Service1.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op1', 'op2', 'op3']);
var Service2 = function() {}; var Service2 = function() {};
Service2.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op4', 'op5']); Service2.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op4', 'op5']);
var Service3 = function() {};
var Service4 = function() {};
Service4.prototype.blockHandler = sinon.stub().callsArgWith(2, null, 'bad-value');
db.node = {}; db.node = {};
db.node.services = { db.node.services = {
service1: new Service1(), service1: new Service1(),
@ -657,6 +660,31 @@ describe('DB Service', function() {
done(); done();
}); });
}); });
it('should not give an error if a service does not have blockHandler', function(done) {
db.node = {};
db.node.services = {
service3: new Service3()
};
db.runAllBlockHandlers('block', true, function(err) {
should.not.exist(err);
done();
});
});
it('should throw an error if blockHandler gives unexpected result', function() {
db.node = {};
db.node.services = {
service4: new Service4()
};
(function() {
db.runAllBlockHandlers('block', true, function(err) {
should.not.exist(err);
});
}).should.throw('bitcore.ErrorInvalidArgument');
});
}); });
describe('#getAPIMethods', function() { describe('#getAPIMethods', function() {