Add message bus for #677
This commit is contained in:
parent
9ee650461f
commit
d16cfb7328
9 changed files with 207 additions and 5 deletions
52
test/pubsub/redis.js
Normal file
52
test/pubsub/redis.js
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
const assert = require('assert');
|
||||
const { RedisMessageBus } = require('../../lib/pubsub/redis');
|
||||
const { EventEmitter } = require('events');
|
||||
const sinon = require('sinon');
|
||||
|
||||
describe('RedisMessageBus', () => {
|
||||
let pubClient, subClient, messageBus, publishSpy, subscribeSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
pubClient = { publish: () => {} };
|
||||
subClient = new EventEmitter();
|
||||
|
||||
subClient.subscribe = () => {};
|
||||
subscribeSpy = sinon.spy(subClient, 'subscribe');
|
||||
|
||||
publishSpy = sinon.spy(pubClient, 'publish');
|
||||
|
||||
messageBus = new RedisMessageBus(pubClient, subClient, 'test');
|
||||
|
||||
subClient.emit('ready');
|
||||
});
|
||||
|
||||
describe('#onMessage', () => {
|
||||
it('processes a valid message', done => {
|
||||
messageBus.once('testEvent', payload => {
|
||||
assert(subscribeSpy.withArgs('test').calledOnce);
|
||||
assert.deepStrictEqual(payload, { foo: 'bar' });
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
messageBus.onMessage('test', '{"event":"testEvent","payload":{"foo":"bar"}}');
|
||||
});
|
||||
|
||||
it('processes a syntactically invalid message', done => {
|
||||
messageBus.onMessage('test', 'not valid json lol');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#emit', () => {
|
||||
it('emits messages', () => {
|
||||
messageBus.emit('testEvent', { foo: 'bar' });
|
||||
|
||||
assert(publishSpy.withArgs('test', sinon.match(arg => {
|
||||
arg = JSON.parse(arg);
|
||||
return arg.event === 'testEvent' && arg.payload.foo === 'bar';
|
||||
})).calledOnce);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue