This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
forked from ceejbot/nsq-relayer
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
115 lines (100 loc) · 2.23 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*global describe:true, it:true, before:true, after:true, beforeEach: true, afterEach:true */
'use strict';
var
demand = require('must'),
createRelayer = require('./index'),
sinon = require('sinon')
;
describe('nsq-relayer', () =>
{
it('exports a constructor', function()
{
createRelayer.must.be.a.function();
const r = createRelayer();
r.must.be.instanceof(createRelayer.NSQRelayer);
r.close();
});
it('obeys its options', function()
{
const spy = sinon.spy(process, 'on');
const r = createRelayer({
event: 'zaphod'
});
spy.called.must.be.true();
spy.calledWith('zaphod').must.be.true();
spy.restore();
r.close();
});
it('defaults options when they are not provided', function()
{
const spy = sinon.spy(process, 'on');
const r = createRelayer();
spy.called.must.be.true();
spy.calledWith('nsq').must.be.true();
spy.restore();
r.close();
});
it('listens for the configured event', function(done)
{
const r = createRelayer();
r.handleEvent = function(msg)
{
msg.must.be.an.object();
msg.payload.must.equal('hello world');
process.removeAllListeners('nsq');
r.close();
done();
};
process.emit('nsq', { payload: 'hello world'});
});
it('posts to nsq on receiving an event', function(done)
{
const r = createRelayer();
const msg = { payload: 'hello world' };
r.nsq.publish = function(topic, msg)
{
topic.must.equal('relayed');
msg.must.be.an.object();
msg.payload.must.equal('hello world');
r.close();
done();
};
process.emit('nsq', msg);
});
it('logs on error', function(done)
{
const r = createRelayer();
r.nsq.publish = function()
{
return Promise.reject(new Error('wat'));
};
const msg = { payload: 'hello world'};
var count = 0;
r.logger.error = function()
{
count++;
if (count === 2)
{
r.close();
done();
}
};
r.handleEvent(msg);
});
it('exposes close()', function(done)
{
const r = createRelayer();
var count = 0;
const close = r.nsq.close.bind(r.nsq);
r.nsq.close = function()
{
count++;
close();
};
const eventCount = process.listeners('nsq').length;
r.close();
(process.listeners('nsq').length - eventCount).must.equal(-1);
count.must.equal(1);
done();
});
});