1 | var assert = require('assert');
|
2 | var PubSub = require('../lib/pubsub_service');
|
3 |
|
4 | var Response = function(cb) {
|
5 | this.cb = cb;
|
6 | };
|
7 | Response.prototype.push = function(topic, options) {
|
8 | var r = this;
|
9 | var Stream = function() {
|
10 | this.topic = topic;
|
11 | this.options = options;
|
12 | };
|
13 | Stream.prototype.end = function (data){
|
14 | r.cb(data);
|
15 | };
|
16 | Stream.prototype.on = function () {};
|
17 |
|
18 | return new Stream();
|
19 | };
|
20 |
|
21 | describe('Pubsub Service', function() {
|
22 | it('exposes subscribe / publish', function() {
|
23 | var ps = new PubSub();
|
24 | assert.equal(typeof ps.publish, 'function');
|
25 | assert.equal(typeof ps.subscribe, 'function');
|
26 | });
|
27 |
|
28 | it('subscribe takes a callback and topic', function() {
|
29 | var ps = new PubSub();
|
30 | ps.subscribe('some-topic', function(topic, name){});
|
31 | });
|
32 |
|
33 | it('subscribe takes a spdy response object', function() {
|
34 | var ps = new PubSub();
|
35 | var r = new Response(function() {});
|
36 | ps.subscribe('some-topic', {response: r});
|
37 | });
|
38 |
|
39 | it('publish does not fail when there are no listeners', function() {
|
40 | var ps = new PubSub();
|
41 | ps.publish('some-topic', 123);
|
42 | });
|
43 |
|
44 | it('publish passes to callback', function(done) {
|
45 | var ps = new PubSub();
|
46 | var received = 0;
|
47 | ps.subscribe('some-topic', function() {
|
48 | received++;
|
49 | });
|
50 | ps.publish('some-topic', 123);
|
51 |
|
52 | setTimeout(function(){
|
53 | assert.equal(received, 1);
|
54 | done();
|
55 | }, 1);
|
56 | });
|
57 |
|
58 | it('publish passes to response', function(done) {
|
59 | var ps = new PubSub();
|
60 | var received = 0;
|
61 | var r = new Response(function() {
|
62 | received++;
|
63 | });
|
64 |
|
65 | ps.subscribe('some-topic', {response: r});
|
66 | ps.publish('some-topic', 123);
|
67 |
|
68 | setTimeout(function(){
|
69 | assert.equal(received, 1);
|
70 | done();
|
71 | }, 1);
|
72 | });
|
73 |
|
74 |
|
75 | it('publish passes to response and callback on same topic', function(done) {
|
76 | var ps = new PubSub();
|
77 | var receivedA = 0;
|
78 | var receivedB = 0;
|
79 | var r = new Response(function() {
|
80 | receivedA++;
|
81 | });
|
82 |
|
83 | ps.subscribe('some-topic', {response: r});
|
84 | ps.subscribe('some-topic', function() {receivedB++;});
|
85 | ps.publish('some-topic', 123);
|
86 |
|
87 | setTimeout(function(){
|
88 | assert.equal(receivedA, 1);
|
89 | assert.equal(receivedB, 1);
|
90 | done();
|
91 | }, 1);
|
92 | });
|
93 |
|
94 |
|
95 | it('unsubscribe should remove listener', function(done) {
|
96 | var ps = new PubSub();
|
97 | var receivedA = 0;
|
98 | var listener = function() {receivedA++;};
|
99 |
|
100 | ps.subscribe('some-topic', listener);
|
101 | ps.publish('some-topic', 123);
|
102 |
|
103 | setTimeout(function(){
|
104 | assert.equal(receivedA, 1);
|
105 | ps.unsubscribe('some-topic', listener);
|
106 | ps.publish('some-topic', 123);
|
107 | setTimeout(function(){
|
108 | assert.equal(receivedA, 1);
|
109 | ps.unsubscribe('some-topic', listener);
|
110 | done();
|
111 | }, 1);
|
112 | }, 1);
|
113 | });
|
114 |
|
115 |
|
116 |
|
117 | });
|