Skip to content

Commit be8a5c7

Browse files
authored
Merge pull request #441 from braydonf/undef-config
node: handle undefined service config
2 parents 4e78500 + 3043263 commit be8a5c7

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lib/node.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,18 @@ Node.prototype.getServiceOrder = function() {
179179
Node.prototype._startService = function(serviceInfo, callback) {
180180
var self = this;
181181

182-
$.checkState(_.isObject(serviceInfo.config));
183-
$.checkState(!serviceInfo.config.node);
184-
$.checkState(!serviceInfo.config.name);
185-
186182
log.info('Starting ' + serviceInfo.name);
187183

188-
var config = serviceInfo.config;
184+
var config;
185+
if (serviceInfo.config) {
186+
$.checkState(_.isObject(serviceInfo.config));
187+
$.checkState(!serviceInfo.config.node);
188+
$.checkState(!serviceInfo.config.name);
189+
config = serviceInfo.config;
190+
} else {
191+
config = {};
192+
}
193+
189194
config.node = this;
190195
config.name = serviceInfo.name;
191196
var service = new serviceInfo.module(config);

test/node.unit.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ describe('Bitcore Node', function() {
233233
getData.callCount.should.equal(1);
234234
});
235235
});
236+
it('will handle config not being set', function() {
237+
var node = new Node(baseConfig);
238+
function TestService() {}
239+
util.inherits(TestService, BaseService);
240+
TestService.prototype.start = sinon.stub().callsArg(0);
241+
var getData = sinon.stub();
242+
TestService.prototype.getData = getData;
243+
TestService.prototype.getAPIMethods = function() {
244+
return [
245+
['getData', this, this.getData, 1]
246+
];
247+
};
248+
var service = {
249+
name: 'testservice',
250+
module: TestService,
251+
};
252+
node._startService(service, function(err) {
253+
if (err) {
254+
throw err;
255+
}
256+
TestService.prototype.start.callCount.should.equal(1);
257+
should.exist(node.services.testservice);
258+
should.exist(node.getData);
259+
node.getData();
260+
getData.callCount.should.equal(1);
261+
});
262+
});
236263
it('will give an error from start', function() {
237264
var node = new Node(baseConfig);
238265
function TestService() {}

0 commit comments

Comments
 (0)