check for duplicate events

This commit is contained in:
Patrick Nagurny 2015-09-03 17:29:28 -04:00
parent 7ac429fbd2
commit a0e40ffd15
3 changed files with 89 additions and 22 deletions

View File

@ -247,13 +247,13 @@ DB.prototype.estimateFee = function(blocks, callback) {
DB.prototype.getPublishEvents = function() {
return [
{
name: 'transaction',
name: 'db/transaction',
scope: this,
subscribe: this.subscribe.bind(this, 'transaction'),
unsubscribe: this.unsubscribe.bind(this, 'transaction')
},
{
name: 'block',
name: 'db/block',
scope: this,
subscribe: this.subscribe.bind(this, 'block'),
unsubscribe: this.unsubscribe.bind(this, 'block')

View File

@ -6,6 +6,8 @@ var bodyParser = require('body-parser');
var socketio = require('socket.io');
var BaseService = require('../service');
var inherits = require('util').inherits;
var index = require('../');
var log = index.log;
var WebService = function(options) {
var self = this;
@ -13,6 +15,7 @@ var WebService = function(options) {
this.port = options.port || this.node.port || 3456;
this.node.on('ready', function() {
self.eventNames = self.getEventNames();
self.setupAllRoutes();
self.server.listen(self.port);
self.createMethodsMap();
@ -51,8 +54,14 @@ WebService.prototype.stop = function(callback) {
WebService.prototype.setupAllRoutes = function() {
for(var key in this.node.services) {
var subApp = new express.Router();
this.app.use('/' + this.node.services[key].getRoutePrefix(), subApp);
this.node.services[key].setupRoutes(subApp, express);
var service = this.node.services[key];
if(service.getRoutePrefix && service.setupRoutes) {
this.app.use('/' + this.node.services[key].getRoutePrefix(), subApp);
this.node.services[key].setupRoutes(subApp, express);
} else {
log.info('Not setting up routes for ' + service.name);
}
}
};
@ -73,7 +82,32 @@ WebService.prototype.createMethodsMap = function() {
args: args
};
});
}
};
WebService.prototype.getEventNames = function() {
var events = this.node.getAllPublishEvents();
var eventNames = [];
function addEventName(name) {
if(eventNames.indexOf(name) > -1) {
throw new Error('Duplicate event ' + name);
}
eventNames.push(name);
};
events.forEach(function(event) {
addEventName(event.name);
if(event.extraEvents) {
event.extraEvents.forEach(function(name) {
addEventName(name);
});
}
});
return eventNames;
};
WebService.prototype.socketHandler = function(socket) {
var self = this;
@ -90,23 +124,7 @@ WebService.prototype.socketHandler = function(socket) {
bus.unsubscribe(name, params);
});
var events = self.node.getAllPublishEvents();
var eventNames = [];
events.forEach(function(event) {
eventNames.push(event.name);
if(event.extraEvents) {
eventNames = eventNames.concat(event.extraEvents);
}
});
eventNames = eventNames.filter(function(value, index, self) {
// remove any duplicates
return self.indexOf(value) === index;
});
eventNames.forEach(function(eventName) {
this.eventNames.forEach(function(eventName) {
bus.on(eventName, function() {
if(socket.connected) {
var results = [];

View File

@ -102,6 +102,54 @@ describe('WebService', function() {
});
});
describe('#getEventNames', function() {
it('should get event names', function() {
var Module1 = function() {};
Module1.prototype.getPublishEvents = function() {
return [
{
name: 'event1',
extraEvents: ['event2']
}
];
};
var module1 = new Module1();
var node = {
on: sinon.spy(),
getAllPublishEvents: sinon.stub().returns(module1.getPublishEvents())
};
var web = new WebService({node: node});
var events = web.getEventNames();
events.should.deep.equal(['event1', 'event2']);
});
it('should throw an error if there is a duplicate event', function() {
var Module1 = function() {};
Module1.prototype.getPublishEvents = function() {
return [
{
name: 'event1',
extraEvents: ['event1']
}
];
};
var module1 = new Module1();
var node = {
on: sinon.spy(),
getAllPublishEvents: sinon.stub().returns(module1.getPublishEvents())
};
var web = new WebService({node: node});
(function() {
var events = web.getEventNames();
}).should.throw('Duplicate event event1');
});
});
describe('#socketHandler', function() {
var bus = new EventEmitter();
@ -127,6 +175,7 @@ describe('WebService', function() {
it('on message should call socketMessageHandler', function(done) {
web = new WebService({node: node});
web.eventNames = web.getEventNames();
web.socketMessageHandler = function(param1) {
param1.should.equal('data');
done();