test pubsub

v1.18.x
Baris Usakli 7 years ago
parent b4d8b7cf38
commit 444fd70ba3

@ -4,6 +4,8 @@ var EventEmitter = require('events');
var nconf = require('nconf'); var nconf = require('nconf');
var real; var real;
var noCluster;
var singleHost;
function get() { function get() {
if (real) { if (real) {
@ -13,14 +15,23 @@ function get() {
var pubsub; var pubsub;
if (nconf.get('isCluster') === 'false') { if (nconf.get('isCluster') === 'false') {
pubsub = new EventEmitter(); if (noCluster) {
pubsub.publish = pubsub.emit.bind(pubsub); real = noCluster;
return real;
}
noCluster = new EventEmitter();
noCluster.publish = noCluster.emit.bind(noCluster);
pubsub = noCluster;
} else if (nconf.get('singleHostCluster')) { } else if (nconf.get('singleHostCluster')) {
pubsub = new EventEmitter(); if (singleHost) {
real = singleHost;
return real;
}
singleHost = new EventEmitter();
if (!process.send) { if (!process.send) {
pubsub.publish = pubsub.emit.bind(pubsub); singleHost.publish = singleHost.emit.bind(singleHost);
} else { } else {
pubsub.publish = function (event, data) { singleHost.publish = function (event, data) {
process.send({ process.send({
action: 'pubsub', action: 'pubsub',
event: event, event: event,
@ -29,10 +40,11 @@ function get() {
}; };
process.on('message', function (message) { process.on('message', function (message) {
if (message && typeof message === 'object' && message.action === 'pubsub') { if (message && typeof message === 'object' && message.action === 'pubsub') {
pubsub.emit(message.event, message.data); singleHost.emit(message.event, message.data);
} }
}); });
} }
pubsub = singleHost;
} else if (nconf.get('redis')) { } else if (nconf.get('redis')) {
pubsub = require('./database/redis/pubsub'); pubsub = require('./database/redis/pubsub');
} else if (nconf.get('mongo')) { } else if (nconf.get('mongo')) {
@ -55,4 +67,7 @@ module.exports = {
removeAllListeners: function (event) { removeAllListeners: function (event) {
get().removeAllListeners(event); get().removeAllListeners(event);
}, },
reset: function () {
real = null;
},
}; };

@ -7,27 +7,29 @@ var db = require('./mocks/databasemock');
var pubsub = require('../src/pubsub'); var pubsub = require('../src/pubsub');
describe('pubsub', function () { describe('pubsub', function () {
it('should use singleHostCluster', function (done) { it('should use the plain event emitter', function (done) {
var oldValue = nconf.get('singleHostCluster'); nconf.set('isCluster', 'false');
nconf.set('singleHostCluster', true); pubsub.reset();
pubsub.on('testEvent', function (message) { pubsub.on('testEvent', function (message) {
assert.equal(message.foo, 1); assert.equal(message.foo, 1);
nconf.set('singleHostCluster', oldValue); nconf.set('isCluster', 'true');
pubsub.removeAllListeners('testEvent'); pubsub.removeAllListeners('testEvent');
pubsub.reset();
done(); done();
}); });
pubsub.publish('testEvent', { foo: 1 }); pubsub.publish('testEvent', { foo: 1 });
}); });
it('should use the current database\'s pubsub', function (done) { it('should use singleHostCluster', function (done) {
var oldValue = nconf.get('singleHostCluster'); var oldValue = nconf.get('singleHostCluster');
nconf.set('singleHostCluster', false); nconf.set('singleHostCluster', true);
pubsub.on('testEvent', function (message) { pubsub.on('testEvent', function (message) {
assert.equal(message.foo, 1); assert.equal(message.foo, 2);
nconf.set('singleHostCluster', oldValue); nconf.set('singleHostCluster', oldValue);
pubsub.removeAllListeners('testEvent'); pubsub.removeAllListeners('testEvent');
pubsub.reset();
done(); done();
}); });
pubsub.publish('testEvent', { foo: 1 }); pubsub.publish('testEvent', { foo: 2 });
}); });
}); });

Loading…
Cancel
Save