This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
behave_like_an_ascoltatore.js
446 lines (373 loc) · 10.8 KB
/
behave_like_an_ascoltatore.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"use strict";
var wrap = require("./util").wrap;
var steed = require("steed")();
/**
* This is a shared mocha test for verifying that an
* ascoltatore is behaving like one.
*
* This requires that the test exposes an instance property
* on `this`.
*
* See as an example:
* https://github.com/mcollina/ascoltatori/blob/master/test/redis_ascoltatore_spec.js
*
* @api public
*/
module.exports = function() {
var expect;
// you MUST depend on chai only if you plan to run
// this test
expect = require("chai").expect;
it("should have a subscribe function", function() {
var that = this;
expect(that.instance).to.respondTo("subscribe");
});
it("should have an publish function", function() {
var that = this;
expect(that.instance).to.respondTo("publish");
});
it("should support a publish/subscribe pattern", function(done) {
var that = this;
that.instance.subscribe("hello", wrap(done), function() {
that.instance.publish("hello");
});
});
it("should support 'pub/sub' combination for pub/sub", function(done) {
var that = this;
that.instance.sub("hello", wrap(done), function() {
that.instance.pub("hello");
});
});
it("should not raise an exception if pub is called without a done callback", function(done) {
this.instance.pub("hello");
setTimeout(done, 50);
});
it("should not raise an exception if sub is called without a done callback", function(done) {
var that = this;
that.instance.sub("hello", function() {});
setTimeout(function() {
// still, some Ascoltatore are steed
// and we must give them some time
done();
}, 10);
});
it("should accept a done callback in pub", function(done) {
this.instance.pub("hello", "world", done);
});
it("should support multi-level wildcard at start of topic", function(done) {
var that = this;
that.instance.sub("*/hello", wrap(done), function() {
that.instance.pub("42/there/hello");
});
});
it("should support multi-level wildcard in middle of topic", function(done) {
var that = this;
that.instance.sub("hello/*/end", wrap(done), function() {
that.instance.pub("hello/there/42/end");
});
});
it("should support multi-level wildcard at end of topic", function(done) {
var that = this;
that.instance.sub("hello/*", wrap(done), function() {
that.instance.pub("hello/there/42");
});
});
it("should support multi-level wildcard at the end of a topic with no separator", function(done) {
var that = this;
that.instance.sub("hello/*", wrap(done), function() {
that.instance.pub("hello");
});
});
it("should support single-level wildcard at start of topic", function(done) {
var that = this;
that.instance.sub("+/hello", wrap(done), function() {
that.instance.pub("42/hello");
});
});
it("should support single-level wildcard in middle of topic", function(done) {
var that = this;
that.instance.sub("hello/+/end", wrap(done), function() {
that.instance.pub("hello/42/end");
});
});
it("should support single-level wildcard at end of topic", function(done) {
var that = this;
that.instance.sub("hello/+", wrap(done), function() {
that.instance.pub("hello/42");
});
});
it("should support both wildcards in topic", function(done) {
var that = this;
that.instance.sub("hello/+/there/*/end", wrap(done), function() {
that.instance.pub("hello/foo/there/bar/42/end");
});
});
it("should not match multiple levels with single wildcard", function(done) {
var that = this,
callback = null;
callback = function(topic) {
expect(topic).to.equal(["hello", "42", "there"].join(that.separator));
done();
};
that.instance.sub("hello/+/there", callback, function () {
that.instance.pub("hello/42/43/there");
that.instance.pub("hello/42/there");
});
});
it("should unsubscribe from wildcard topics independently", function(done) {
var that = this,
callback1 = null,
callback2 = null;
callback1 = function(topic) {
expect(topic).to.equal(["hello", "42", "there"].join(that.separator));
done();
};
callback2 = function () { };
that.instance.sub("hello/*/there", callback2, function () {
that.instance.sub("hello/+/there", callback1, function () {
that.instance.unsub("hello/*/there", callback2, function () {
that.instance.pub("hello/42/there");
});
});
});
});
it("should call each matching callback", function(done) {
var that = this,
callback = null,
count = 0;
callback = function(topic) {
expect(topic).to.equal(["hello", "42"].join(that.separator));
count += 1;
if (count === 2) {
done();
}
};
that.instance.sub("hello/42", callback, function () {
that.instance.sub("hello/*", callback, function () {
that.instance.pub("hello/42");
});
});
});
it("should publish the topic name", function(done) {
var that = this;
that.instance.sub("hello/*", function(topic) {
expect(topic).to.equal(["hello", "42"].join(that.separator));
done();
}, function() {
that.instance.pub("hello/42");
});
});
it("should publish the passed argument", function(done) {
var that = this;
that.instance.sub("hello/*", function(topic, value) {
expect(value.toString()).to.equal("42");
done();
}, function() {
that.instance.pub("hello/123", "42");
});
});
it("should have an unsubscribe function", function() {
var that = this;
expect(that.instance).to.respondTo("unsubscribe");
});
it("should have an unsub function", function() {
var that = this;
expect(that.instance).to.respondTo("unsub");
});
it("should remove a listener", function(done) {
var that = this;
var funcToRemove = null;
funcToRemove = function(topic, value) {
throw "that should never run";
};
steed.series([
function(cb) {
that.instance.sub("hello", funcToRemove, cb);
},
function(cb) {
that.instance.sub("hello", wrap(done), cb);
},
function(cb) {
that.instance.unsub("hello", funcToRemove, cb);
},
function(cb) {
that.instance.pub("hello", null, cb);
}
]);
});
it("should remove a listener for global searches", function(done) {
var that = this,
funcToRemove = null;
funcToRemove = function(topic, value) {
throw "that should never run";
};
steed.series([
function(cb) {
that.instance.sub("hello/42", wrap(done), cb);
},
function(cb) {
that.instance.sub("hello/*", funcToRemove, cb);
},
function(cb) {
that.instance.unsub("hello/*", funcToRemove, cb);
},
function(cb) {
that.instance.pub("hello/42", null, cb);
}
]);
});
it("support at least 10 listeners", function(done) {
var instance = this.instance,
counter = 11,
i = null,
callback = null,
subscribe = null,
a = [];
callback = function() {
counter = counter - 1;
if (counter === 0) {
done();
}
};
subscribe = function (done) {
instance.subscribe("hello", callback, done);
};
for (i = counter; i > 0; i = i - 1) {
a.push(subscribe);
}
steed.parallel(a, instance.publish.bind(instance, "hello", null));
});
it("should emit the ready event", function(done) {
this.instance.on("ready", done);
});
it("should support at least 12 listeners as an EventEmitter", function(done) {
var counter = 11,
i = null,
callback, a = [];
callback = function() {
counter = counter - 1;
if (counter === 0) {
done();
}
};
for (i = counter; i > 0; i = i - 1) {
a.push(this.instance.on.bind(this.instance, "ready", callback));
}
steed.parallel(a);
});
it("should support removing a single listener", function(done) {
var that = this,
funcToRemove;
funcToRemove = function(topic, value) {
throw "that should never run";
};
steed.series([
function(cb) {
that.instance.sub("hello", wrap(done), cb);
},
function(cb) {
that.instance.sub("hello", funcToRemove, cb);
},
function(cb) {
that.instance.unsub("hello", funcToRemove, cb);
},
function(cb) {
that.instance.pub("hello", null, cb);
}
]);
});
it("should have a close function", function() {
var that = this;
expect(that.instance).to.respondTo("close");
});
it("should throw an error if publishing when the ascoltatore has been closed", function(done) {
this.instance.close(function() {
expect(function() {
this.instance.publish("hello", "world");
}).to.throw;
done();
});
});
it("should throw an error if subscribing when the ascoltatore has been closed", function(done) {
this.instance.close(function() {
expect(function() {
this.instance.subscribe("hello");
}).to.throw;
done();
});
});
it("should close using a callback", function(done) {
var that = this;
this.instance.publish("hello", "world", function() {
that.instance.close(done);
});
});
it("should allow the close method to be called twice", function(done) {
var that = this;
steed.series([
this.instance.publish.bind(this.instance, "hello", "world"),
this.instance.close.bind(this.instance),
this.instance.close.bind(this.instance)
], done);
});
it("should not deliver message twice for double subscription", function(done) {
var that = this,
count = 2,
sub = null,
d = null;
d = function() {
count = count - 1;
if (count === 0) {
done();
}
};
sub = function(cb) {
var called = false;
that.instance.sub("hello", function() {
expect(called).to.be.equal(false);
called = true;
d();
}, cb);
};
steed.series([
sub, sub,
function(cb) {
that.instance.pub("hello", "ahha", cb);
}
]);
});
it("should not deliver message twice for multiple subscriptions using wildcards", function(done) {
var that = this,
count = 3,
sub = null,
d = null;
d = function() {
count = count - 1;
if (count === 0) {
done();
}
};
sub = function(topic, cb) {
var called = false;
that.instance.sub(topic, function() {
expect(called).to.equal(false);
called = true;
d();
}, cb);
};
steed.series([
function (cb) {
sub("a/+", cb);
},
function (cb) {
sub("+/b", cb);
},
function (cb) {
sub("a/b", cb);
},
function(cb) {
that.instance.pub("a/b", "ahha", cb);
}
]);
});
};