| 1 |
// Copyright 2014 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.pubsub.TypedPubSubTest'); |
| 16 |
goog.setTestOnly('goog.pubsub.TypedPubSubTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.pubsub.TopicId'); |
| 20 |
goog.require('goog.pubsub.TypedPubSub'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
var pubsub; |
| 24 |
|
| 25 |
function setUp() { |
| 26 |
pubsub = new goog.pubsub.TypedPubSub(); |
| 27 |
} |
| 28 |
|
| 29 |
function tearDown() { |
| 30 |
pubsub.dispose(); |
| 31 |
} |
| 32 |
|
| 33 |
function testConstructor() { |
| 34 |
assertNotNull('PubSub instance must not be null', pubsub); |
| 35 |
assertTrue( |
| 36 |
'PubSub instance must have the expected type', |
| 37 |
pubsub instanceof goog.pubsub.TypedPubSub); |
| 38 |
} |
| 39 |
|
| 40 |
function testDispose() { |
| 41 |
assertFalse( |
| 42 |
'PubSub instance must not have been disposed of', pubsub.isDisposed()); |
| 43 |
pubsub.dispose(); |
| 44 |
assertTrue('PubSub instance must have been disposed of', pubsub.isDisposed()); |
| 45 |
} |
| 46 |
|
| 47 |
function testSubscribeUnsubscribe() { |
| 48 |
function foo1() {} |
| 49 |
function bar1() {} |
| 50 |
function foo2() {} |
| 51 |
function bar2() {} |
| 52 |
|
| 53 |
/** const */ var FOO = new goog.pubsub.TopicId('foo'); |
| 54 |
/** const */ var BAR = new goog.pubsub.TopicId('bar'); |
| 55 |
/** const */ var BAZ = new goog.pubsub.TopicId('baz'); |
| 56 |
|
| 57 |
assertEquals( |
| 58 |
'Topic "foo" must not have any subscribers', 0, pubsub.getCount(FOO)); |
| 59 |
assertEquals( |
| 60 |
'Topic "bar" must not have any subscribers', 0, pubsub.getCount(BAR)); |
| 61 |
|
| 62 |
pubsub.subscribe(FOO, foo1); |
| 63 |
assertEquals('Topic "foo" must have 1 subscriber', 1, pubsub.getCount(FOO)); |
| 64 |
assertEquals( |
| 65 |
'Topic "bar" must not have any subscribers', 0, pubsub.getCount(BAR)); |
| 66 |
|
| 67 |
pubsub.subscribe(BAR, bar1); |
| 68 |
assertEquals('Topic "foo" must have 1 subscriber', 1, pubsub.getCount(FOO)); |
| 69 |
assertEquals('Topic "bar" must have 1 subscriber', 1, pubsub.getCount(BAR)); |
| 70 |
|
| 71 |
pubsub.subscribe(FOO, foo2); |
| 72 |
assertEquals('Topic "foo" must have 2 subscribers', 2, pubsub.getCount(FOO)); |
| 73 |
assertEquals('Topic "bar" must have 1 subscriber', 1, pubsub.getCount(BAR)); |
| 74 |
|
| 75 |
pubsub.subscribe(BAR, bar2); |
| 76 |
assertEquals('Topic "foo" must have 2 subscribers', 2, pubsub.getCount(FOO)); |
| 77 |
assertEquals('Topic "bar" must have 2 subscribers', 2, pubsub.getCount(BAR)); |
| 78 |
|
| 79 |
assertTrue(pubsub.unsubscribe(FOO, foo1)); |
| 80 |
assertEquals('Topic "foo" must have 1 subscriber', 1, pubsub.getCount(FOO)); |
| 81 |
assertEquals('Topic "bar" must have 2 subscribers', 2, pubsub.getCount(BAR)); |
| 82 |
|
| 83 |
assertTrue(pubsub.unsubscribe(FOO, foo2)); |
| 84 |
assertEquals('Topic "foo" must have no subscribers', 0, pubsub.getCount(FOO)); |
| 85 |
assertEquals('Topic "bar" must have 2 subscribers', 2, pubsub.getCount(BAR)); |
| 86 |
|
| 87 |
assertTrue(pubsub.unsubscribe(BAR, bar1)); |
| 88 |
assertEquals('Topic "foo" must have no subscribers', 0, pubsub.getCount(FOO)); |
| 89 |
assertEquals('Topic "bar" must have 1 subscriber', 1, pubsub.getCount(BAR)); |
| 90 |
|
| 91 |
assertTrue(pubsub.unsubscribe(BAR, bar2)); |
| 92 |
assertEquals('Topic "foo" must have no subscribers', 0, pubsub.getCount(FOO)); |
| 93 |
assertEquals('Topic "bar" must have no subscribers', 0, pubsub.getCount(BAR)); |
| 94 |
|
| 95 |
assertFalse( |
| 96 |
'Unsubscribing a nonexistent topic must return false', |
| 97 |
pubsub.unsubscribe(BAZ, foo1)); |
| 98 |
|
| 99 |
assertFalse( |
| 100 |
'Unsubscribing a nonexistent function must return false', |
| 101 |
pubsub.unsubscribe(FOO, function() {})); |
| 102 |
} |
| 103 |
|
| 104 |
function testSubscribeUnsubscribeWithContext() { |
| 105 |
function foo() {} |
| 106 |
function bar() {} |
| 107 |
|
| 108 |
var contextA = {}; |
| 109 |
var contextB = {}; |
| 110 |
|
| 111 |
/** const */ var TOPIC_X = new goog.pubsub.TopicId('X'); |
| 112 |
|
| 113 |
assertEquals( |
| 114 |
'Topic "X" must not have any subscribers', 0, pubsub.getCount(TOPIC_X)); |
| 115 |
|
| 116 |
pubsub.subscribe(TOPIC_X, foo, contextA); |
| 117 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount(TOPIC_X)); |
| 118 |
|
| 119 |
pubsub.subscribe(TOPIC_X, bar); |
| 120 |
assertEquals( |
| 121 |
'Topic "X" must have 2 subscribers', 2, pubsub.getCount(TOPIC_X)); |
| 122 |
|
| 123 |
pubsub.subscribe(TOPIC_X, bar, contextB); |
| 124 |
assertEquals( |
| 125 |
'Topic "X" must have 3 subscribers', 3, pubsub.getCount(TOPIC_X)); |
| 126 |
|
| 127 |
assertFalse( |
| 128 |
'Unknown function/context combination return false', |
| 129 |
pubsub.unsubscribe(TOPIC_X, foo, contextB)); |
| 130 |
|
| 131 |
assertTrue(pubsub.unsubscribe(TOPIC_X, foo, contextA)); |
| 132 |
assertEquals( |
| 133 |
'Topic "X" must have 2 subscribers', 2, pubsub.getCount(TOPIC_X)); |
| 134 |
|
| 135 |
assertTrue(pubsub.unsubscribe(TOPIC_X, bar)); |
| 136 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount(TOPIC_X)); |
| 137 |
|
| 138 |
assertTrue(pubsub.unsubscribe(TOPIC_X, bar, contextB)); |
| 139 |
assertEquals( |
| 140 |
'Topic "X" must have no subscribers', 0, pubsub.getCount(TOPIC_X)); |
| 141 |
} |
| 142 |
|
| 143 |
function testSubscribeOnce() { |
| 144 |
var called, context; |
| 145 |
|
| 146 |
/** @const */ SOME_TOPIC = new goog.pubsub.TopicId('someTopic'); |
| 147 |
|
| 148 |
called = false; |
| 149 |
pubsub.subscribeOnce(SOME_TOPIC, function() { called = true; }); |
| 150 |
assertEquals( |
| 151 |
'Topic must have one subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 152 |
assertFalse('Subscriber must not have been called yet', called); |
| 153 |
|
| 154 |
pubsub.publish(SOME_TOPIC); |
| 155 |
assertEquals( |
| 156 |
'Topic must have no subscribers', 0, pubsub.getCount(SOME_TOPIC)); |
| 157 |
assertTrue('Subscriber must have been called', called); |
| 158 |
|
| 159 |
context = {called: false}; |
| 160 |
pubsub.subscribeOnce(SOME_TOPIC, function() { this.called = true; }, context); |
| 161 |
assertEquals( |
| 162 |
'Topic must have one subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 163 |
assertFalse('Subscriber must not have been called yet', context.called); |
| 164 |
|
| 165 |
pubsub.publish(SOME_TOPIC); |
| 166 |
assertEquals( |
| 167 |
'Topic must have no subscribers', 0, pubsub.getCount(SOME_TOPIC)); |
| 168 |
assertTrue('Subscriber must have been called', context.called); |
| 169 |
|
| 170 |
context = {called: false, value: 0}; |
| 171 |
pubsub.subscribeOnce(SOME_TOPIC, function(value) { |
| 172 |
this.called = true; |
| 173 |
this.value = value; |
| 174 |
}, context); |
| 175 |
assertEquals( |
| 176 |
'Topic must have one subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 177 |
assertFalse('Subscriber must not have been called yet', context.called); |
| 178 |
assertEquals('Value must have expected value', 0, context.value); |
| 179 |
|
| 180 |
pubsub.publish(SOME_TOPIC, 17); |
| 181 |
assertEquals( |
| 182 |
'Topic must have no subscribers', 0, pubsub.getCount(SOME_TOPIC)); |
| 183 |
assertTrue('Subscriber must have been called', context.called); |
| 184 |
assertEquals('Value must have been updated', 17, context.value); |
| 185 |
} |
| 186 |
|
| 187 |
function testSubscribeOnce_boundFn() { |
| 188 |
var context = {called: false, value: 0}; |
| 189 |
|
| 190 |
/** @const */ SOME_TOPIC = new goog.pubsub.TopicId('someTopic'); |
| 191 |
|
| 192 |
function subscriber(value) { |
| 193 |
this.called = true; |
| 194 |
this.value = value; |
| 195 |
} |
| 196 |
|
| 197 |
pubsub.subscribeOnce(SOME_TOPIC, goog.bind(subscriber, context)); |
| 198 |
assertEquals( |
| 199 |
'Topic must have one subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 200 |
assertFalse('Subscriber must not have been called yet', context.called); |
| 201 |
assertEquals('Value must have expected value', 0, context.value); |
| 202 |
|
| 203 |
pubsub.publish(SOME_TOPIC, 17); |
| 204 |
assertEquals( |
| 205 |
'Topic must have no subscribers', 0, pubsub.getCount(SOME_TOPIC)); |
| 206 |
assertTrue('Subscriber must have been called', context.called); |
| 207 |
assertEquals('Value must have been updated', 17, context.value); |
| 208 |
} |
| 209 |
|
| 210 |
function testSubscribeOnce_partialFn() { |
| 211 |
var called = false; |
| 212 |
var value = 0; |
| 213 |
|
| 214 |
/** @const */ SOME_TOPIC = new goog.pubsub.TopicId('someTopic'); |
| 215 |
|
| 216 |
function subscriber(hasBeenCalled, newValue) { |
| 217 |
called = hasBeenCalled; |
| 218 |
value = newValue; |
| 219 |
} |
| 220 |
|
| 221 |
pubsub.subscribeOnce(SOME_TOPIC, goog.partial(subscriber, true)); |
| 222 |
assertEquals( |
| 223 |
'Topic must have one subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 224 |
assertFalse('Subscriber must not have been called yet', called); |
| 225 |
assertEquals('Value must have expected value', 0, value); |
| 226 |
|
| 227 |
pubsub.publish(SOME_TOPIC, 17); |
| 228 |
assertEquals( |
| 229 |
'Topic must have no subscribers', 0, pubsub.getCount(SOME_TOPIC)); |
| 230 |
assertTrue('Subscriber must have been called', called); |
| 231 |
assertEquals('Value must have been updated', 17, value); |
| 232 |
} |
| 233 |
|
| 234 |
function testSelfResubscribe() { |
| 235 |
var value = null; |
| 236 |
|
| 237 |
/** @const */ SOME_TOPIC = new goog.pubsub.TopicId('someTopic'); |
| 238 |
|
| 239 |
function resubscribe(iteration, newValue) { |
| 240 |
pubsub.subscribeOnce(SOME_TOPIC, goog.partial(resubscribe, iteration + 1)); |
| 241 |
value = newValue + ':' + iteration; |
| 242 |
} |
| 243 |
|
| 244 |
pubsub.subscribeOnce(SOME_TOPIC, goog.partial(resubscribe, 0)); |
| 245 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 246 |
assertNull('Value must be null', value); |
| 247 |
|
| 248 |
pubsub.publish(SOME_TOPIC, 'foo'); |
| 249 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 250 |
assertEquals('Value be as expected', 'foo:0', value); |
| 251 |
|
| 252 |
pubsub.publish(SOME_TOPIC, 'bar'); |
| 253 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 254 |
assertEquals('Value be as expected', 'bar:1', value); |
| 255 |
|
| 256 |
pubsub.publish(SOME_TOPIC, 'baz'); |
| 257 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 258 |
assertEquals('Value be as expected', 'baz:2', value); |
| 259 |
} |
| 260 |
|
| 261 |
function testUnsubscribeByKey() { |
| 262 |
var key1, key2, key3; |
| 263 |
|
| 264 |
/** const */ var TOPIC_X = new goog.pubsub.TopicId('X'); |
| 265 |
/** const */ var TOPIC_Y = new goog.pubsub.TopicId('Y'); |
| 266 |
|
| 267 |
key1 = pubsub.subscribe(TOPIC_X, function() {}); |
| 268 |
key2 = pubsub.subscribe(TOPIC_Y, function() {}); |
| 269 |
|
| 270 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount(TOPIC_X)); |
| 271 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount(TOPIC_Y)); |
| 272 |
assertNotEquals('Subscription keys must be distinct', key1, key2); |
| 273 |
|
| 274 |
pubsub.unsubscribeByKey(key1); |
| 275 |
assertEquals( |
| 276 |
'Topic "X" must have no subscribers', 0, pubsub.getCount(TOPIC_X)); |
| 277 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount(TOPIC_Y)); |
| 278 |
|
| 279 |
key3 = pubsub.subscribe(TOPIC_X, function() {}); |
| 280 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount(TOPIC_X)); |
| 281 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount(TOPIC_Y)); |
| 282 |
assertNotEquals('Subscription keys must be distinct', key1, key3); |
| 283 |
assertNotEquals('Subscription keys must be distinct', key2, key3); |
| 284 |
|
| 285 |
pubsub.unsubscribeByKey(key1); // Obsolete key; should be no-op. |
| 286 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount(TOPIC_X)); |
| 287 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount(TOPIC_Y)); |
| 288 |
|
| 289 |
pubsub.unsubscribeByKey(key2); |
| 290 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount(TOPIC_X)); |
| 291 |
assertEquals( |
| 292 |
'Topic "Y" must have no subscribers', 0, pubsub.getCount(TOPIC_Y)); |
| 293 |
|
| 294 |
pubsub.unsubscribeByKey(key3); |
| 295 |
assertEquals( |
| 296 |
'Topic "X" must have no subscribers', 0, pubsub.getCount(TOPIC_X)); |
| 297 |
assertEquals( |
| 298 |
'Topic "Y" must have no subscribers', 0, pubsub.getCount(TOPIC_Y)); |
| 299 |
} |
| 300 |
|
| 301 |
function testSubscribeUnsubscribeMultiple() { |
| 302 |
function foo() {} |
| 303 |
function bar() {} |
| 304 |
|
| 305 |
var context = {}; |
| 306 |
|
| 307 |
/** const */ var TOPIC_X = new goog.pubsub.TopicId('X'); |
| 308 |
/** const */ var TOPIC_Y = new goog.pubsub.TopicId('Y'); |
| 309 |
/** const */ var TOPIC_Z = new goog.pubsub.TopicId('Z'); |
| 310 |
|
| 311 |
assertEquals( |
| 312 |
'Pubsub channel must not have any subscribers', 0, pubsub.getCount()); |
| 313 |
|
| 314 |
assertEquals( |
| 315 |
'Topic "X" must not have any subscribers', 0, pubsub.getCount(TOPIC_X)); |
| 316 |
assertEquals( |
| 317 |
'Topic "Y" must not have any subscribers', 0, pubsub.getCount(TOPIC_Y)); |
| 318 |
assertEquals( |
| 319 |
'Topic "Z" must not have any subscribers', 0, pubsub.getCount(TOPIC_Z)); |
| 320 |
|
| 321 |
goog.array.forEach([TOPIC_X, TOPIC_Y, TOPIC_Z], function(topic) { |
| 322 |
pubsub.subscribe(topic, foo); |
| 323 |
}); |
| 324 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount(TOPIC_X)); |
| 325 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount(TOPIC_Y)); |
| 326 |
assertEquals('Topic "Z" must have 1 subscriber', 1, pubsub.getCount(TOPIC_Z)); |
| 327 |
|
| 328 |
goog.array.forEach([TOPIC_X, TOPIC_Y, TOPIC_Z], function(topic) { |
| 329 |
pubsub.subscribe(topic, bar, context); |
| 330 |
}); |
| 331 |
assertEquals( |
| 332 |
'Topic "X" must have 2 subscribers', 2, pubsub.getCount(TOPIC_X)); |
| 333 |
assertEquals( |
| 334 |
'Topic "Y" must have 2 subscribers', 2, pubsub.getCount(TOPIC_Y)); |
| 335 |
assertEquals( |
| 336 |
'Topic "Z" must have 2 subscribers', 2, pubsub.getCount(TOPIC_Z)); |
| 337 |
|
| 338 |
assertEquals( |
| 339 |
'Pubsub channel must have a total of 6 subscribers', 6, |
| 340 |
pubsub.getCount()); |
| 341 |
|
| 342 |
goog.array.forEach([TOPIC_X, TOPIC_Y, TOPIC_Z], function(topic) { |
| 343 |
pubsub.unsubscribe(topic, foo); |
| 344 |
}); |
| 345 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount(TOPIC_X)); |
| 346 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount(TOPIC_Y)); |
| 347 |
assertEquals('Topic "Z" must have 1 subscriber', 1, pubsub.getCount(TOPIC_Z)); |
| 348 |
|
| 349 |
goog.array.forEach([TOPIC_X, TOPIC_Y, TOPIC_Z], function(topic) { |
| 350 |
pubsub.unsubscribe(topic, bar, context); |
| 351 |
}); |
| 352 |
assertEquals( |
| 353 |
'Topic "X" must not have any subscribers', 0, pubsub.getCount(TOPIC_X)); |
| 354 |
assertEquals( |
| 355 |
'Topic "Y" must not have any subscribers', 0, pubsub.getCount(TOPIC_Y)); |
| 356 |
assertEquals( |
| 357 |
'Topic "Z" must not have any subscribers', 0, pubsub.getCount(TOPIC_Z)); |
| 358 |
|
| 359 |
assertEquals( |
| 360 |
'Pubsub channel must not have any subscribers', 0, pubsub.getCount()); |
| 361 |
} |
| 362 |
|
| 363 |
function testPublish() { |
| 364 |
var context = {}; |
| 365 |
var fooCalled = false; |
| 366 |
var barCalled = false; |
| 367 |
|
| 368 |
/** @const */ SOME_TOPIC = new goog.pubsub.TopicId('someTopic'); |
| 369 |
|
| 370 |
function foo(record) { |
| 371 |
fooCalled = true; |
| 372 |
assertEquals('x must have expected value', 'x', record.x); |
| 373 |
assertEquals('y must have expected value', 'y', record.y); |
| 374 |
} |
| 375 |
|
| 376 |
function bar(record) { |
| 377 |
barCalled = true; |
| 378 |
assertEquals('Context must have expected value', context, this); |
| 379 |
assertEquals('x must have expected value', 'x', record.x); |
| 380 |
assertEquals('y must have expected value', 'y', record.y); |
| 381 |
} |
| 382 |
|
| 383 |
pubsub.subscribe(SOME_TOPIC, foo); |
| 384 |
pubsub.subscribe(SOME_TOPIC, bar, context); |
| 385 |
|
| 386 |
assertTrue(pubsub.publish(SOME_TOPIC, {x: 'x', y: 'y'})); |
| 387 |
assertTrue('foo() must have been called', fooCalled); |
| 388 |
assertTrue('bar() must have been called', barCalled); |
| 389 |
|
| 390 |
fooCalled = false; |
| 391 |
barCalled = false; |
| 392 |
assertTrue(pubsub.unsubscribe(SOME_TOPIC, foo)); |
| 393 |
|
| 394 |
assertTrue(pubsub.publish(SOME_TOPIC, {x: 'x', y: 'y'})); |
| 395 |
assertFalse('foo() must not have been called', fooCalled); |
| 396 |
assertTrue('bar() must have been called', barCalled); |
| 397 |
|
| 398 |
fooCalled = false; |
| 399 |
barCalled = false; |
| 400 |
pubsub.subscribe('differentTopic', foo); |
| 401 |
|
| 402 |
assertTrue(pubsub.publish(SOME_TOPIC, {x: 'x', y: 'y'})); |
| 403 |
assertFalse('foo() must not have been called', fooCalled); |
| 404 |
assertTrue('bar() must have been called', barCalled); |
| 405 |
} |
| 406 |
|
| 407 |
function testPublishEmptyTopic() { |
| 408 |
var fooCalled = false; |
| 409 |
function foo() { fooCalled = true; } |
| 410 |
|
| 411 |
/** @const */ SOME_TOPIC = new goog.pubsub.TopicId('someTopic'); |
| 412 |
|
| 413 |
assertFalse( |
| 414 |
'Publishing to nonexistent topic must return false', |
| 415 |
pubsub.publish(SOME_TOPIC)); |
| 416 |
|
| 417 |
pubsub.subscribe(SOME_TOPIC, foo); |
| 418 |
assertTrue( |
| 419 |
'Publishing to topic with subscriber must return true', |
| 420 |
pubsub.publish(SOME_TOPIC)); |
| 421 |
assertTrue('Foo must have been called', fooCalled); |
| 422 |
|
| 423 |
pubsub.unsubscribe(SOME_TOPIC, foo); |
| 424 |
fooCalled = false; |
| 425 |
assertFalse( |
| 426 |
'Publishing to topic without subscribers must return false', |
| 427 |
pubsub.publish(SOME_TOPIC)); |
| 428 |
assertFalse('Foo must nothave been called', fooCalled); |
| 429 |
} |
| 430 |
|
| 431 |
function testSubscribeWhilePublishing() { |
| 432 |
// It's OK for a subscriber to add a new subscriber to its own topic, |
| 433 |
// but the newly added subscriber shouldn't be called until the next |
| 434 |
// publish cycle. |
| 435 |
|
| 436 |
var firstCalled = false; |
| 437 |
var secondCalled = false; |
| 438 |
|
| 439 |
/** @const */ SOME_TOPIC = new goog.pubsub.TopicId('someTopic'); |
| 440 |
|
| 441 |
pubsub.subscribe(SOME_TOPIC, function() { |
| 442 |
pubsub.subscribe(SOME_TOPIC, function() { secondCalled = true; }); |
| 443 |
firstCalled = true; |
| 444 |
}); |
| 445 |
assertEquals( |
| 446 |
'Topic must have one subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 447 |
assertFalse( |
| 448 |
'No subscriber must have been called yet', firstCalled || secondCalled); |
| 449 |
|
| 450 |
pubsub.publish(SOME_TOPIC); |
| 451 |
assertEquals( |
| 452 |
'Topic must have two subscribers', 2, pubsub.getCount(SOME_TOPIC)); |
| 453 |
assertTrue('The first subscriber must have been called', firstCalled); |
| 454 |
assertFalse( |
| 455 |
'The second subscriber must not have been called yet', secondCalled); |
| 456 |
|
| 457 |
pubsub.publish(SOME_TOPIC); |
| 458 |
assertEquals( |
| 459 |
'Topic must have three subscribers', 3, pubsub.getCount(SOME_TOPIC)); |
| 460 |
assertTrue('The first subscriber must have been called', firstCalled); |
| 461 |
assertTrue('The second subscriber must also have been called', secondCalled); |
| 462 |
} |
| 463 |
|
| 464 |
function testUnsubscribeWhilePublishing() { |
| 465 |
// It's OK for a subscriber to unsubscribe another subscriber from its |
| 466 |
// own topic, but the subscriber in question won't actually be removed |
| 467 |
// until after publishing is complete. |
| 468 |
|
| 469 |
var firstCalled = false; |
| 470 |
var secondCalled = false; |
| 471 |
var thirdCalled = false; |
| 472 |
|
| 473 |
/** const */ var TOPIC_X = new goog.pubsub.TopicId('X'); |
| 474 |
|
| 475 |
function first() { |
| 476 |
assertTrue( |
| 477 |
'unsubscribe() must return true when removing a topic', |
| 478 |
pubsub.unsubscribe(TOPIC_X, second)); |
| 479 |
assertEquals( |
| 480 |
'Topic "X" must still have 3 subscribers', 3, pubsub.getCount(TOPIC_X)); |
| 481 |
firstCalled = true; |
| 482 |
} |
| 483 |
pubsub.subscribe(TOPIC_X, first); |
| 484 |
|
| 485 |
function second() { secondCalled = true; } |
| 486 |
pubsub.subscribe(TOPIC_X, second); |
| 487 |
|
| 488 |
function third() { |
| 489 |
assertTrue( |
| 490 |
'unsubscribe() must return true when removing a topic', |
| 491 |
pubsub.unsubscribe(TOPIC_X, first)); |
| 492 |
assertEquals( |
| 493 |
'Topic "X" must still have 3 subscribers', 3, pubsub.getCount(TOPIC_X)); |
| 494 |
thirdCalled = true; |
| 495 |
} |
| 496 |
pubsub.subscribe(TOPIC_X, third); |
| 497 |
|
| 498 |
assertEquals( |
| 499 |
'Topic "X" must have 3 subscribers', 3, pubsub.getCount(TOPIC_X)); |
| 500 |
assertFalse( |
| 501 |
'No subscribers must have been called yet', |
| 502 |
firstCalled || secondCalled || thirdCalled); |
| 503 |
|
| 504 |
assertTrue(pubsub.publish(TOPIC_X)); |
| 505 |
assertTrue('First function must have been called', firstCalled); |
| 506 |
assertFalse('Second function must have been called', secondCalled); |
| 507 |
assertTrue('Third function must have been called', thirdCalled); |
| 508 |
assertEquals( |
| 509 |
'Topic "X" must have 1 subscriber after publishing', 1, |
| 510 |
pubsub.getCount(TOPIC_X)); |
| 511 |
} |
| 512 |
|
| 513 |
function testUnsubscribeSelfWhilePublishing() { |
| 514 |
// It's OK for a subscriber to unsubscribe itself, but it won't actually |
| 515 |
// be removed until after publishing is complete. |
| 516 |
|
| 517 |
var selfDestructCalled = false; |
| 518 |
|
| 519 |
/** @const */ SOME_TOPIC = new goog.pubsub.TopicId('someTopic'); |
| 520 |
|
| 521 |
function selfDestruct() { |
| 522 |
assertTrue( |
| 523 |
'unsubscribe() must return true when removing a topic', |
| 524 |
pubsub.unsubscribe(SOME_TOPIC, arguments.callee)); |
| 525 |
assertEquals( |
| 526 |
'Topic must still have 1 subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 527 |
selfDestructCalled = true; |
| 528 |
} |
| 529 |
|
| 530 |
pubsub.subscribe(SOME_TOPIC, selfDestruct); |
| 531 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount(SOME_TOPIC)); |
| 532 |
assertFalse( |
| 533 |
'selfDestruct() must not have been called yet', selfDestructCalled); |
| 534 |
|
| 535 |
pubsub.publish(SOME_TOPIC); |
| 536 |
assertTrue('selfDestruct() must have been called', selfDestructCalled); |
| 537 |
assertEquals( |
| 538 |
'Topic must have no subscribers after publishing', 0, |
| 539 |
pubsub.getCount(SOME_TOPIC)); |
| 540 |
} |
| 541 |
|
| 542 |
function testPublishReturnValue() { |
| 543 |
/** @const */ SOME_TOPIC = new goog.pubsub.TopicId('someTopic'); |
| 544 |
pubsub.subscribe(SOME_TOPIC, function() { |
| 545 |
pubsub.unsubscribe(SOME_TOPIC, arguments.callee); |
| 546 |
}); |
| 547 |
assertTrue( |
| 548 |
'publish() must return true even if the only subscriber ' + |
| 549 |
'removes itself during publishing', |
| 550 |
pubsub.publish(SOME_TOPIC)); |
| 551 |
} |
| 552 |
|
| 553 |
function testNestedPublish() { |
| 554 |
var x1 = false; |
| 555 |
var x2 = false; |
| 556 |
var y1 = false; |
| 557 |
var y2 = false; |
| 558 |
|
| 559 |
/** @const */ TOPIC_X = new goog.pubsub.TopicId('X'); |
| 560 |
/** @const */ TOPIC_Y = new goog.pubsub.TopicId('Y'); |
| 561 |
|
| 562 |
pubsub.subscribe(TOPIC_X, function() { |
| 563 |
pubsub.publish(TOPIC_Y); |
| 564 |
pubsub.unsubscribe(TOPIC_X, arguments.callee); |
| 565 |
x1 = true; |
| 566 |
}); |
| 567 |
|
| 568 |
pubsub.subscribe(TOPIC_X, function() { x2 = true; }); |
| 569 |
|
| 570 |
pubsub.subscribe(TOPIC_Y, function() { |
| 571 |
pubsub.unsubscribe(TOPIC_Y, arguments.callee); |
| 572 |
y1 = true; |
| 573 |
}); |
| 574 |
|
| 575 |
pubsub.subscribe(TOPIC_Y, function() { y2 = true; }); |
| 576 |
|
| 577 |
pubsub.publish(TOPIC_X); |
| 578 |
|
| 579 |
assertTrue('x1 must be true', x1); |
| 580 |
assertTrue('x2 must be true', x2); |
| 581 |
assertTrue('y1 must be true', y1); |
| 582 |
assertTrue('y2 must be true', y2); |
| 583 |
} |
| 584 |
|
| 585 |
function testClear() { |
| 586 |
function fn() {} |
| 587 |
|
| 588 |
var topics = [ |
| 589 |
new goog.pubsub.TopicId('W'), new goog.pubsub.TopicId('X'), |
| 590 |
new goog.pubsub.TopicId('Y'), new goog.pubsub.TopicId('Z') |
| 591 |
]; |
| 592 |
|
| 593 |
goog.array.forEach(topics, function(topic) { pubsub.subscribe(topic, fn); }); |
| 594 |
assertEquals('Pubsub channel must have 4 subscribers', 4, pubsub.getCount()); |
| 595 |
|
| 596 |
pubsub.clear(topics[0]); |
| 597 |
assertEquals('Pubsub channel must have 3 subscribers', 3, pubsub.getCount()); |
| 598 |
|
| 599 |
pubsub.clear(topics[1]); |
| 600 |
pubsub.clear(topics[2]); |
| 601 |
assertEquals('Pubsub channel must have 1 subscriber', 1, pubsub.getCount()); |
| 602 |
|
| 603 |
pubsub.clear(); |
| 604 |
assertEquals('Pubsub channel must have no subscribers', 0, pubsub.getCount()); |
| 605 |
} |
| 606 |
|
| 607 |
function testNestedSubscribeOnce() { |
| 608 |
var calls = 0; |
| 609 |
|
| 610 |
pubsub.subscribeOnce('X', function() { calls++; }); |
| 611 |
|
| 612 |
pubsub.subscribe('Y', function() { |
| 613 |
pubsub.publish('X'); |
| 614 |
pubsub.publish('X'); |
| 615 |
}); |
| 616 |
|
| 617 |
pubsub.publish('Y'); |
| 618 |
|
| 619 |
assertEquals('X must be called once', 1, calls); |
| 620 |
} |