UNPKG

4.91 kBJavaScriptView Raw
1var assert = require('assert');
2var PubSub = require('../lib/pubsub_service');
3
4
5function makeFakeRequest(fd) {
6 return { connection: { socket: { _handle: { fd: (fd || 1) }} }};
7}
8var Response = function(cb) {
9 this.cb = cb;
10};
11Response.prototype.push = function(topic, options) {
12 var r = this;
13 var Stream = function() {
14 this.topic = topic;
15 this.options = options;
16 };
17 Stream.prototype.end = function (data){
18 r.cb(data);
19 };
20 Stream.prototype.on = function () {};
21
22 return new Stream();
23};
24
25describe('Pubsub Service', function() {
26 it('exposes subscribe / publish', function() {
27 var ps = new PubSub();
28 assert.equal(typeof ps.publish, 'function');
29 assert.equal(typeof ps.subscribe, 'function');
30 });
31
32 it('subscribe takes a callback and topic', function() {
33 var ps = new PubSub();
34 ps.subscribe('some-topic', function(topic, name){});
35 });
36
37 it('subscribe takes a spdy response object', function() {
38 var ps = new PubSub();
39 var r = new Response(function() {});
40 ps.subscribe('some-topic', {request: makeFakeRequest(1), response: r});
41 });
42
43 it('publish does not fail when there are no listeners', function() {
44 var ps = new PubSub();
45 ps.publish('some-topic', 123);
46 });
47
48 it('publish passes to callback', function(done) {
49 var ps = new PubSub();
50 var received = 0;
51 ps.subscribe('some-topic', function() {
52 received++;
53 });
54 ps.publish('some-topic', 123);
55
56 setTimeout(function(){
57 assert.equal(received, 1);
58 done();
59 }, 1);
60 });
61
62 it('publish passes to response', function(done) {
63 var ps = new PubSub();
64 var received = 0;
65 var r = new Response(function() {
66 received++;
67 });
68
69 ps.subscribe('some-topic', {request: makeFakeRequest(1), response: r});
70 ps.publish('some-topic', 123);
71
72 setTimeout(function(){
73 assert.equal(received, 1);
74 done();
75 }, 1);
76 });
77
78
79 it('publish passes to response and callback on same topic', function(done) {
80 var ps = new PubSub();
81 var receivedA = 0;
82 var receivedB = 0;
83 var r = new Response(function() {
84 receivedA++;
85 });
86
87 ps.subscribe('some-topic', {request: makeFakeRequest(1), response: r});
88 ps.subscribe('some-topic', function() {receivedB++;});
89 ps.publish('some-topic', 123);
90
91 setTimeout(function(){
92 assert.equal(receivedA, 1);
93 assert.equal(receivedB, 1);
94 done();
95 }, 1);
96 });
97
98
99 it('unsubscribe should remove listener', function(done) {
100 var ps = new PubSub();
101 var receivedA = 0;
102 var listener = function() {receivedA++;};
103
104 ps.subscribe('some-topic', listener);
105 ps.publish('some-topic', 123);
106
107 setTimeout(function(){
108 assert.equal(receivedA, 1);
109 ps.unsubscribe('some-topic', listener);
110 ps.publish('some-topic', 123);
111 setTimeout(function(){
112 assert.equal(receivedA, 1);
113 ps.unsubscribe('some-topic', listener);
114 done();
115 }, 1);
116 }, 1);
117 });
118
119 it('one http subscription and one callback that match the same event will emit one event on both', function(done) {
120 var ps = new PubSub();
121 var receivedA = 0;
122 var receivedB = 0;
123
124 var r1 = new Response(function() {
125 receivedA++;
126 });
127
128 var listener = function() {receivedB++;};
129
130 ps.subscribe('led/123/state', {request: makeFakeRequest(1), response: r1 });
131 ps.subscribe('led/*/state', listener);
132 ps.publish('led/123/state', 123);
133
134 setTimeout(function(){
135 assert.equal(receivedA, 1);
136 assert.equal(receivedB, 1);
137 done();
138 }, 10);
139 });
140
141 it('two subscriptions with callback that match the same event will emit one event on both', function(done) {
142 var ps = new PubSub();
143 var receivedA = 0;
144 var receivedB = 0;
145 var receivedC = 0;
146
147 var listener1 = function() {receivedA++;};
148 var listener2 = function() {receivedB++;};
149 var listener3 = function() {receivedC++;};
150
151 ps.subscribe('led/123/state', listener1);
152 ps.subscribe('led/*/state', listener2);
153 ps.subscribe('led/*/state', listener3);
154 ps.publish('led/123/state', 123);
155
156 setTimeout(function(){
157 assert.equal(receivedA, 1);
158 assert.equal(receivedB, 1);
159 done();
160 }, 1);
161 });
162
163 it('two http subscriptions that match the same event will only emit event on the first subscription', function(done) {
164 var ps = new PubSub();
165 var receivedA = 0;
166 var receivedB = 0;
167
168 var r1 = new Response(function() {
169 receivedA++;
170 });
171
172 var r2 = new Response(function() {
173 receivedB++;
174 });
175
176 ps.subscribe('led/123/state', {request: makeFakeRequest(1), response: r1 });
177 ps.subscribe('led/*/state', {request: makeFakeRequest(1), response: r2 });
178 ps.publish('led/123/state', 123);
179
180 setTimeout(function(){
181 assert.equal(receivedA, 1);
182 assert.equal(receivedB, 0);
183 done();
184 }, 1);
185 });
186
187
188
189});