forked from redis/node-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub.spec.js
356 lines (310 loc) · 14.2 KB
/
pubsub.spec.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
'use strict';
var assert = require("assert");
var config = require("./lib/config");
var helper = require("./helper");
var redis = config.redis;
describe("publish/subscribe", function () {
helper.allTests(function(parser, ip, args) {
describe("using " + parser + " and " + ip, function () {
var pub = null;
var sub = null;
var channel = "test channel";
var channel2 = "test channel 2";
var message = "test message";
beforeEach(function (done) {
var end = helper.callFuncAfter(done, 2);
pub = redis.createClient.apply(redis.createClient, args);
sub = redis.createClient.apply(redis.createClient, args);
pub.once("connect", function () {
pub.flushdb(function () {
end();
});
});
sub.once("connect", function () {
end();
});
});
describe('disable resubscribe', function () {
beforeEach(function (done) {
sub.end(false);
sub = redis.createClient({
disable_resubscribing: true
});
sub.once("connect", function () {
done();
});
});
it('does not fire subscribe events after reconnecting', function (done) {
var a = false;
sub.on("subscribe", function (chnl, count) {
if (chnl === channel2) {
if (a) {
return done(new Error('Test failed'));
}
assert.equal(2, count);
sub.stream.destroy();
}
});
sub.on('reconnecting', function() {
a = true;
sub.on('ready', function () {
setTimeout(done, 250);
});
});
sub.subscribe(channel, channel2);
});
});
describe('subscribe', function () {
it('fires a subscribe event for each channel subscribed to even after reconnecting', function (done) {
var a = false;
sub.on("subscribe", function (chnl, count) {
if (chnl === channel2) {
assert.equal(2, count);
if (a) {
return done();
}
sub.stream.destroy();
}
});
sub.on('reconnecting', function() {
a = true;
});
sub.subscribe(channel, channel2);
});
it('receives messages on subscribed channel', function (done) {
var end = helper.callFuncAfter(done, 2);
sub.on("subscribe", function (chnl, count) {
pub.publish(channel, message, function (err, res) {
helper.isNumber(1)(err, res);
end();
});
});
sub.on("message", function (chnl, msg) {
assert.equal(chnl, channel);
assert.equal(msg, message);
end();
});
sub.subscribe(channel);
});
it('receives messages if subscribe is called after unsubscribe', function (done) {
var end = helper.callFuncAfter(done, 2);
sub.once("subscribe", function (chnl, count) {
pub.publish(channel, message, function (err, res) {
helper.isNumber(1)(err, res);
end();
});
});
sub.on("message", function (chnl, msg) {
assert.equal(chnl, channel);
assert.equal(msg, message);
end();
});
sub.subscribe(channel);
sub.unsubscribe(channel);
sub.subscribe(channel);
});
it('handles SUB_UNSUB_MSG_SUB', function (done) {
sub.subscribe('chan8');
sub.subscribe('chan9');
sub.unsubscribe('chan9');
pub.publish('chan8', 'something');
sub.subscribe('chan9', function () {
return done();
});
});
it('handles SUB_UNSUB_MSG_SUB 2', function (done) {
sub.psubscribe('abc*');
sub.subscribe('xyz');
sub.unsubscribe('xyz');
pub.publish('abcd', 'something');
sub.subscribe('xyz', function () {
return done();
});
});
it('emits end event if quit is called from within subscribe', function (done) {
sub.on("end", function () {
return done();
});
sub.on("subscribe", function (chnl, count) {
sub.quit();
});
sub.subscribe(channel);
});
it('handles SUBSCRIBE_CLOSE_RESUBSCRIBE', function (done) {
var count = 0;
/* Create two clients. c1 subscribes to two channels, c2 will publish to them.
c2 publishes the first message.
c1 gets the message and drops its connection. It must resubscribe itself.
When it resubscribes, c2 publishes the second message, on the same channel
c1 gets the message and drops its connection. It must resubscribe itself, again.
When it resubscribes, c2 publishes the third message, on the second channel
c1 gets the message and drops its connection. When it reconnects, the test ends.
*/
sub.on("message", function(channel, message) {
if (channel === "chan1") {
assert.strictEqual(message, "hi on channel 1");
sub.stream.end();
} else if (channel === "chan2") {
assert.strictEqual(message, "hi on channel 2");
sub.stream.end();
} else {
sub.quit();
pub.quit();
assert.fail("test failed");
}
});
sub.subscribe("chan1", "chan2");
sub.on("ready", function(err, results) {
count++;
if (count === 1) {
pub.publish("chan1", "hi on channel 1");
return;
} else if (count === 2) {
pub.publish("chan2", "hi on channel 2");
} else {
sub.quit(function() {
pub.quit(function() {
return done();
});
});
}
});
pub.publish("chan1", "hi on channel 1");
});
});
describe('unsubscribe', function () {
it('fires an unsubscribe event', function (done) {
sub.on("subscribe", function (chnl, count) {
sub.unsubscribe(channel);
});
sub.subscribe(channel);
sub.on("unsubscribe", function (chnl, count) {
assert.equal(chnl, channel);
assert.strictEqual(count, 0);
return done();
});
});
it('puts client back into write mode', function (done) {
sub.on("subscribe", function (chnl, count) {
sub.unsubscribe(channel);
});
sub.subscribe(channel);
sub.on("unsubscribe", function (chnl, count) {
pub.incr("foo", helper.isNumber(1, done));
});
});
it('does not complain when unsubscribe is called and there are no subscriptions', function (done) {
sub.unsubscribe(function (err, res) {
assert.strictEqual(err, null);
assert.strictEqual(res, null);
done();
});
});
it('executes callback when unsubscribe is called and there are no subscriptions', function (done) {
pub.unsubscribe(function (err, results) {
assert.strictEqual(null, results);
return done(err);
});
});
});
describe('psubscribe', function () {
// test motivated by issue #753
it('allows all channels to be subscribed to using a * pattern', function (done) {
sub.psubscribe('*');
sub.on("pmessage", function(pattern, channel, message) {
assert.strictEqual(pattern, '*');
assert.strictEqual(channel, '/foo');
assert.strictEqual(message, 'hello world');
return done();
});
pub.publish('/foo', 'hello world');
});
});
describe('punsubscribe', function () {
it('does not complain when punsubscribe is called and there are no subscriptions', function () {
sub.punsubscribe();
});
it('executes callback when punsubscribe is called and there are no subscriptions', function (done) {
pub.punsubscribe(function (err, results) {
assert.strictEqual(null, results);
return done(err);
});
});
});
describe('fail for other commands while in pub sub mode', function () {
it('return error if only pub sub commands are allowed', function (done) {
sub.subscribe('channel');
// Ping is allowed even if not listed as such!
sub.ping(function (err, res) {
assert.strictEqual(err, null);
assert.strictEqual(res[0], 'pong');
});
// Get is forbidden
sub.get('foo', function (err, res) {
assert.strictEqual(err.message, 'ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context');
assert.strictEqual(err.command, 'GET');
});
// Quit is allowed
sub.quit(done);
});
it('emit error if only pub sub commands are allowed without callback', function (done) {
sub.subscribe('channel');
sub.on('error', function (err) {
assert.strictEqual(err.message, 'ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context');
assert.strictEqual(err.command, 'GET');
done();
});
sub.get('foo');
});
});
it("should not publish a message multiple times per command", function (done) {
var published = {};
function subscribe(message) {
sub.removeAllListeners('subscribe');
sub.removeAllListeners('message');
sub.removeAllListeners('unsubscribe');
sub.on('subscribe', function () {
pub.publish('/foo', message);
});
sub.on('message', function (channel, message) {
if (published[message]) {
done(new Error('Message published more than once.'));
}
published[message] = true;
});
sub.on('unsubscribe', function (channel, count) {
assert.strictEqual(count, 0);
});
sub.subscribe('/foo');
}
subscribe('hello');
setTimeout(function () {
sub.unsubscribe();
setTimeout(function () {
subscribe('world');
setTimeout(done, 50);
}, 40);
}, 40);
});
// TODO: Fix pub sub
// And there's more than just those two issues
describe.skip('FIXME: broken pub sub', function () {
it("should not publish a message without any publish command", function (done) {
pub.set('foo', 'message');
pub.set('bar', 'hello');
pub.mget('foo', 'bar');
pub.subscribe('channel');
pub.on('message', function (msg) {
done(new Error('This message should not have been published: ' + msg));
});
setTimeout(done, 200);
});
});
afterEach(function () {
// Explicitly ignore still running commands
pub.end(false);
sub.end(false);
});
});
});
});