From 3043263e3baa5a35bb9f1c8f3666b65133804e6d Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 3 Jun 2016 15:54:01 -0400 Subject: [PATCH] node: handle undefined service config --- lib/node.js | 15 ++++++++++----- test/node.unit.js | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/node.js b/lib/node.js index 1c97c361..23d6b04a 100644 --- a/lib/node.js +++ b/lib/node.js @@ -179,13 +179,18 @@ Node.prototype.getServiceOrder = function() { Node.prototype._startService = function(serviceInfo, callback) { var self = this; - $.checkState(_.isObject(serviceInfo.config)); - $.checkState(!serviceInfo.config.node); - $.checkState(!serviceInfo.config.name); - log.info('Starting ' + serviceInfo.name); - var config = serviceInfo.config; + var config; + if (serviceInfo.config) { + $.checkState(_.isObject(serviceInfo.config)); + $.checkState(!serviceInfo.config.node); + $.checkState(!serviceInfo.config.name); + config = serviceInfo.config; + } else { + config = {}; + } + config.node = this; config.name = serviceInfo.name; var service = new serviceInfo.module(config); diff --git a/test/node.unit.js b/test/node.unit.js index dc7b4a3e..85e5be67 100644 --- a/test/node.unit.js +++ b/test/node.unit.js @@ -233,6 +233,33 @@ describe('Bitcore Node', function() { getData.callCount.should.equal(1); }); }); + it('will handle config not being set', function() { + var node = new Node(baseConfig); + function TestService() {} + util.inherits(TestService, BaseService); + TestService.prototype.start = sinon.stub().callsArg(0); + var getData = sinon.stub(); + TestService.prototype.getData = getData; + TestService.prototype.getAPIMethods = function() { + return [ + ['getData', this, this.getData, 1] + ]; + }; + var service = { + name: 'testservice', + module: TestService, + }; + node._startService(service, function(err) { + if (err) { + throw err; + } + TestService.prototype.start.callCount.should.equal(1); + should.exist(node.services.testservice); + should.exist(node.getData); + node.getData(); + getData.callCount.should.equal(1); + }); + }); it('will give an error from start', function() { var node = new Node(baseConfig); function TestService() {}