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(
this.node.services,
function(mod, next) {
mod.blockHandler.call(mod, block, add, function(err, ops) {
if (err) {
return next(err);
}
if (ops) {
operations = operations.concat(ops);
}
next();
});
if(mod.blockHandler) {
$.checkArgument(typeof mod.blockHandler === 'function', 'blockHandler must be a function');
mod.blockHandler.call(mod, block, add, function(err, ops) {
if (err) {
return next(err);
}
if (ops) {
$.checkArgument(Array.isArray(ops), 'blockHandler for ' + mod.name + ' returned non-array');
operations = operations.concat(ops);
}
next();
});
} else {
setImmediate(next);
}
},
function(err) {
if (err) {

View File

@ -630,6 +630,9 @@ describe('DB Service', function() {
Service1.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op1', 'op2', 'op3']);
var Service2 = function() {};
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.services = {
service1: new Service1(),
@ -657,6 +660,31 @@ describe('DB Service', function() {
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() {