Merge pull request #443 from braydonf/opt-service-methods

node: optional getAPIMethods and getPublishEvents
This commit is contained in:
Chris Kleeschulte 2016-06-13 11:06:03 -04:00 committed by GitHub
commit ae7359cf93
2 changed files with 75 additions and 18 deletions

View File

@ -102,8 +102,10 @@ Node.prototype.getAllAPIMethods = function() {
var methods = []; var methods = [];
for(var i in this.services) { for(var i in this.services) {
var mod = this.services[i]; var mod = this.services[i];
if (mod.getAPIMethods) {
methods = methods.concat(mod.getAPIMethods()); methods = methods.concat(mod.getAPIMethods());
} }
}
return methods; return methods;
}; };
@ -115,8 +117,10 @@ Node.prototype.getAllPublishEvents = function() {
var events = []; var events = [];
for (var i in this.services) { for (var i in this.services) {
var mod = this.services[i]; var mod = this.services[i];
if (mod.getPublishEvents) {
events = events.concat(mod.getPublishEvents()); events = events.concat(mod.getPublishEvents());
} }
}
return events; return events;
}; };
@ -204,6 +208,7 @@ Node.prototype._startService = function(serviceInfo, callback) {
} }
// add API methods // add API methods
if (service.getAPIMethods) {
var methodData = service.getAPIMethods(); var methodData = service.getAPIMethods();
var methodNameConflicts = []; var methodNameConflicts = [];
methodData.forEach(function(data) { methodData.forEach(function(data) {
@ -223,6 +228,7 @@ Node.prototype._startService = function(serviceInfo, callback) {
if (methodNameConflicts.length > 0) { if (methodNameConflicts.length > 0) {
return callback(new Error('Existing API method(s) exists: ' + methodNameConflicts.join(', '))); return callback(new Error('Existing API method(s) exists: ' + methodNameConflicts.join(', ')));
} }
}
callback(); callback();

View File

@ -139,6 +139,21 @@ describe('Bitcore Node', function() {
var methods = node.getAllAPIMethods(); var methods = node.getAllAPIMethods();
methods.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']); methods.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']);
}); });
it('will handle service without getAPIMethods defined', function() {
var node = new Node(baseConfig);
node.services = {
db: {
getAPIMethods: sinon.stub().returns(['db1', 'db2']),
},
service1: {},
service2: {
getAPIMethods: sinon.stub().returns(['mdb1', 'mdb2'])
}
};
var methods = node.getAllAPIMethods();
methods.should.deep.equal(['db1', 'db2', 'mdb1', 'mdb2']);
});
}); });
describe('#getAllPublishEvents', function() { describe('#getAllPublishEvents', function() {
@ -158,6 +173,20 @@ describe('Bitcore Node', function() {
var events = node.getAllPublishEvents(); var events = node.getAllPublishEvents();
events.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']); events.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']);
}); });
it('will handle service without getPublishEvents defined', function() {
var node = new Node(baseConfig);
node.services = {
db: {
getPublishEvents: sinon.stub().returns(['db1', 'db2']),
},
service1: {},
service2: {
getPublishEvents: sinon.stub().returns(['mdb1', 'mdb2'])
}
};
var events = node.getAllPublishEvents();
events.should.deep.equal(['db1', 'db2', 'mdb1', 'mdb2']);
});
}); });
describe('#getServiceOrder', function() { describe('#getServiceOrder', function() {
@ -370,6 +399,28 @@ describe('Bitcore Node', function() {
}); });
}); });
it('will handle service with getAPIMethods undefined', function(done) {
var node = new Node(baseConfig);
function TestService() {}
util.inherits(TestService, BaseService);
TestService.prototype.start = sinon.stub().callsArg(0);
TestService.prototype.getData = function() {};
node.getServiceOrder = sinon.stub().returns([
{
name: 'test',
module: TestService,
config: {}
},
]);
node.start(function() {
TestService.prototype.start.callCount.should.equal(1);
done();
});
});
}); });
describe('#getNetworkName', function() { describe('#getNetworkName', function() {