forked from yentsun/tasu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
161 lines (141 loc) · 5.3 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const {assert} = require('chai');
const Transport = require('./');
const tp = new Transport({url: 'nats://localhost:4222', group: 'tests', requestTimeout: 100}); // a nats server should be running
let nats;
tp.on('error', () => {
process.exit(1);
});
describe('wrapper', () => {
before(async () => {
await tp.connected();
nats=tp._nats;
});
describe('request', () => {
before((done) => {
tp.listen('request.ok', (message, respond) => {
respond(null, message);
});
tp.listen('request.password', (message, respond) => {
respond(null, message);
});
tp.listen('request.error', (message, respond) => {
respond(new Error('service error'));
});
tp.listen('request.empty', (message, respond) => {
respond(null, null);
});
tp.listen('request.error.detail', (message, respond) => {
respond({detail: 'service error'}, null);
});
done();
});
it('performs a request and returns successful result', async () => {
const {foo} = await tp.request('request.ok', {foo: 'bar'});
assert.equal(foo, 'bar');
});
it('performs a request and screens password field in logs', async () => {
const response = await tp.request('request.password', {
password: 'should not see this',
data: {password: 'should not see this also'}
});
assert.equal(response.password, 'should not see this');
assert.equal(response.data.password, 'should not see this also');
});
it('performs a request and returns error', async () => {
try {
await tp.request('request.error', {foo: 'bar'});
} catch (error) {
assert.equal(error.message, 'service error');
}
});
it('performs a request and returns an empty response', async () => {
const response = await tp.request('request.empty', {foo: 'bar'});
assert.isNull(response);
});
it('performs a request and returns error from error.detail', async () => {
try {
await tp.request('request.error.detail', {foo: 'bar'});
} catch (error) {
assert.equal(error.message, 'service error');
}
});
it('performs a request and returns timeout error', async () => {
try {
await tp.request('request.timeout', {foo: 'bar'});
} catch (error) {
assert.equal(error.message, 'response timeout');
}
}).timeout(110);
});
describe('subscribe', () => {
it('performs a response-subscription to a subject', (done) => {
tp.subscribe('broadcast', (message, replyTo, subject) => {
assert.equal(message.foo, 'bar');
assert.isNotOk(replyTo);
assert.equal(subject, 'broadcast');
done()
});
tp.publish('broadcast', {foo: 'bar'});
})
});
describe('listen', () => {
it('listens to requests', async () => {
tp.listen('request.listen', (message, respond) => {
assert.equal(message.foo, 'bar');
respond(null, {bar: 'foo'});
});
const {bar} = await tp.request('request.listen', {foo: 'bar'});
assert.equal(bar, 'foo');
})
});
describe('process', () => {
it('process a queue message as group member', (done) => {
tp.process('process', (message, subject) => {
assert.equal(message.foo, 'bar');
assert.equal(subject, 'process');
done()
});
tp.publish('process', {foo: 'bar'});
})
});
describe('publish', () => {
it('publishes a message to a subject', (done) => {
tp.process('publish', (message) => {
assert.equal(message.message, 'yay!');
done();
});
tp.publish('publish', {message: 'yay!'});
});
});
describe('unsubscribe', () => {
it('unsubscribes from NATS subject', (done) => {
const sid = tp.subscribe('unsubscribe', ()=>{});
tp._nats.once('unsubscribe', (subId, subject) => {
assert.equal(subId, sid);
assert.equal(subject, 'unsubscribe');
done();
});
tp.unsubscribe(sid);
})
});
describe('subOnce', () => {
it('subscribes, gets one message and unsubscribes', (done) => {
const sid = tp.subOnce('subOnce', (message) => {
assert.equal(message.foo, 'onced'); // FIXME no assertion here
});
tp.publish('subOnce', {foo: 'once'});
tp._nats.once('unsubscribe', (subId, subject) => {
assert.equal(subId, sid);
assert.equal(subject, 'subOnce');
done();
});
});
});
describe('close', () => {
it('closes underlying connection with NATS', (done) => {
tp.close();
assert.isTrue(tp._nats.closed);
done();
})
});
});