| 1 |
// Copyright 2007 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.PubSubTest'); |
| 16 |
goog.setTestOnly('goog.pubsub.PubSubTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.pubsub.PubSub'); |
| 20 |
goog.require('goog.testing.MockClock'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
var pubsub; |
| 24 |
var asyncPubsub; |
| 25 |
var mockClock; |
| 26 |
|
| 27 |
function setUp() { |
| 28 |
pubsub = new goog.pubsub.PubSub(); |
| 29 |
asyncPubsub = new goog.pubsub.PubSub(true); |
| 30 |
mockClock = new goog.testing.MockClock(true); |
| 31 |
} |
| 32 |
|
| 33 |
function tearDown() { |
| 34 |
mockClock.uninstall(); |
| 35 |
asyncPubsub.dispose(); |
| 36 |
pubsub.dispose(); |
| 37 |
} |
| 38 |
|
| 39 |
function testConstructor() { |
| 40 |
assertNotNull('PubSub instance must not be null', pubsub); |
| 41 |
assertTrue( |
| 42 |
'PubSub instance must have the expected type', |
| 43 |
pubsub instanceof goog.pubsub.PubSub); |
| 44 |
} |
| 45 |
|
| 46 |
function testDispose() { |
| 47 |
assertFalse( |
| 48 |
'PubSub instance must not have been disposed of', pubsub.isDisposed()); |
| 49 |
pubsub.dispose(); |
| 50 |
assertTrue('PubSub instance must have been disposed of', pubsub.isDisposed()); |
| 51 |
} |
| 52 |
|
| 53 |
function testSubscribeUnsubscribe() { |
| 54 |
function foo1() {} |
| 55 |
function bar1() {} |
| 56 |
function foo2() {} |
| 57 |
function bar2() {} |
| 58 |
|
| 59 |
assertEquals( |
| 60 |
'Topic "foo" must not have any subscribers', 0, pubsub.getCount('foo')); |
| 61 |
assertEquals( |
| 62 |
'Topic "bar" must not have any subscribers', 0, pubsub.getCount('bar')); |
| 63 |
|
| 64 |
pubsub.subscribe('foo', foo1); |
| 65 |
assertEquals('Topic "foo" must have 1 subscriber', 1, pubsub.getCount('foo')); |
| 66 |
assertEquals( |
| 67 |
'Topic "bar" must not have any subscribers', 0, pubsub.getCount('bar')); |
| 68 |
|
| 69 |
pubsub.subscribe('bar', bar1); |
| 70 |
assertEquals('Topic "foo" must have 1 subscriber', 1, pubsub.getCount('foo')); |
| 71 |
assertEquals('Topic "bar" must have 1 subscriber', 1, pubsub.getCount('bar')); |
| 72 |
|
| 73 |
pubsub.subscribe('foo', foo2); |
| 74 |
assertEquals( |
| 75 |
'Topic "foo" must have 2 subscribers', 2, pubsub.getCount('foo')); |
| 76 |
assertEquals('Topic "bar" must have 1 subscriber', 1, pubsub.getCount('bar')); |
| 77 |
|
| 78 |
pubsub.subscribe('bar', bar2); |
| 79 |
assertEquals( |
| 80 |
'Topic "foo" must have 2 subscribers', 2, pubsub.getCount('foo')); |
| 81 |
assertEquals( |
| 82 |
'Topic "bar" must have 2 subscribers', 2, pubsub.getCount('bar')); |
| 83 |
|
| 84 |
assertTrue(pubsub.unsubscribe('foo', foo1)); |
| 85 |
assertEquals('Topic "foo" must have 1 subscriber', 1, pubsub.getCount('foo')); |
| 86 |
assertEquals( |
| 87 |
'Topic "bar" must have 2 subscribers', 2, pubsub.getCount('bar')); |
| 88 |
|
| 89 |
assertTrue(pubsub.unsubscribe('foo', foo2)); |
| 90 |
assertEquals( |
| 91 |
'Topic "foo" must have no subscribers', 0, pubsub.getCount('foo')); |
| 92 |
assertEquals( |
| 93 |
'Topic "bar" must have 2 subscribers', 2, pubsub.getCount('bar')); |
| 94 |
|
| 95 |
assertTrue(pubsub.unsubscribe('bar', bar1)); |
| 96 |
assertEquals( |
| 97 |
'Topic "foo" must have no subscribers', 0, pubsub.getCount('foo')); |
| 98 |
assertEquals('Topic "bar" must have 1 subscriber', 1, pubsub.getCount('bar')); |
| 99 |
|
| 100 |
assertTrue(pubsub.unsubscribe('bar', bar2)); |
| 101 |
assertEquals( |
| 102 |
'Topic "foo" must have no subscribers', 0, pubsub.getCount('foo')); |
| 103 |
assertEquals( |
| 104 |
'Topic "bar" must have no subscribers', 0, pubsub.getCount('bar')); |
| 105 |
|
| 106 |
assertFalse( |
| 107 |
'Unsubscribing a nonexistent topic must return false', |
| 108 |
pubsub.unsubscribe('baz', foo1)); |
| 109 |
|
| 110 |
assertFalse( |
| 111 |
'Unsubscribing a nonexistent function must return false', |
| 112 |
pubsub.unsubscribe('foo', function() {})); |
| 113 |
} |
| 114 |
|
| 115 |
function testSubscribeUnsubscribeWithContext() { |
| 116 |
function foo() {} |
| 117 |
function bar() {} |
| 118 |
|
| 119 |
var contextA = {}; |
| 120 |
var contextB = {}; |
| 121 |
|
| 122 |
assertEquals( |
| 123 |
'Topic "X" must not have any subscribers', 0, pubsub.getCount('X')); |
| 124 |
|
| 125 |
pubsub.subscribe('X', foo, contextA); |
| 126 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount('X')); |
| 127 |
|
| 128 |
pubsub.subscribe('X', bar); |
| 129 |
assertEquals('Topic "X" must have 2 subscribers', 2, pubsub.getCount('X')); |
| 130 |
|
| 131 |
pubsub.subscribe('X', bar, contextB); |
| 132 |
assertEquals('Topic "X" must have 3 subscribers', 3, pubsub.getCount('X')); |
| 133 |
|
| 134 |
assertFalse( |
| 135 |
'Unknown function/context combination return false', |
| 136 |
pubsub.unsubscribe('X', foo, contextB)); |
| 137 |
|
| 138 |
assertTrue(pubsub.unsubscribe('X', foo, contextA)); |
| 139 |
assertEquals('Topic "X" must have 2 subscribers', 2, pubsub.getCount('X')); |
| 140 |
|
| 141 |
assertTrue(pubsub.unsubscribe('X', bar)); |
| 142 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount('X')); |
| 143 |
|
| 144 |
assertTrue(pubsub.unsubscribe('X', bar, contextB)); |
| 145 |
assertEquals('Topic "X" must have no subscribers', 0, pubsub.getCount('X')); |
| 146 |
} |
| 147 |
|
| 148 |
function testSubscribeOnce() { |
| 149 |
var called, context; |
| 150 |
|
| 151 |
called = false; |
| 152 |
pubsub.subscribeOnce('someTopic', function() { called = true; }); |
| 153 |
assertEquals( |
| 154 |
'Topic must have one subscriber', 1, pubsub.getCount('someTopic')); |
| 155 |
assertFalse('Subscriber must not have been called yet', called); |
| 156 |
|
| 157 |
pubsub.publish('someTopic'); |
| 158 |
assertEquals( |
| 159 |
'Topic must have no subscribers', 0, pubsub.getCount('someTopic')); |
| 160 |
assertTrue('Subscriber must have been called', called); |
| 161 |
|
| 162 |
context = {called: false}; |
| 163 |
pubsub.subscribeOnce( |
| 164 |
'someTopic', function() { this.called = true; }, context); |
| 165 |
assertEquals( |
| 166 |
'Topic must have one subscriber', 1, pubsub.getCount('someTopic')); |
| 167 |
assertFalse('Subscriber must not have been called yet', context.called); |
| 168 |
|
| 169 |
pubsub.publish('someTopic'); |
| 170 |
assertEquals( |
| 171 |
'Topic must have no subscribers', 0, pubsub.getCount('someTopic')); |
| 172 |
assertTrue('Subscriber must have been called', context.called); |
| 173 |
|
| 174 |
context = {called: false, value: 0}; |
| 175 |
pubsub.subscribeOnce('someTopic', function(value) { |
| 176 |
this.called = true; |
| 177 |
this.value = value; |
| 178 |
}, context); |
| 179 |
assertEquals( |
| 180 |
'Topic must have one subscriber', 1, pubsub.getCount('someTopic')); |
| 181 |
assertFalse('Subscriber must not have been called yet', context.called); |
| 182 |
assertEquals('Value must have expected value', 0, context.value); |
| 183 |
|
| 184 |
pubsub.publish('someTopic', 17); |
| 185 |
assertEquals( |
| 186 |
'Topic must have no subscribers', 0, pubsub.getCount('someTopic')); |
| 187 |
assertTrue('Subscriber must have been called', context.called); |
| 188 |
assertEquals('Value must have been updated', 17, context.value); |
| 189 |
} |
| 190 |
|
| 191 |
function testAsyncSubscribeOnce() { |
| 192 |
var callCount = 0; |
| 193 |
asyncPubsub.subscribeOnce('someTopic', function() { callCount++; }); |
| 194 |
assertEquals( |
| 195 |
'Topic must have one subscriber', 1, asyncPubsub.getCount('someTopic')); |
| 196 |
mockClock.tick(); |
| 197 |
assertEquals('Subscriber must not have been called yet', 0, callCount); |
| 198 |
|
| 199 |
asyncPubsub.publish('someTopic'); |
| 200 |
asyncPubsub.publish('someTopic'); |
| 201 |
mockClock.tick(); |
| 202 |
assertEquals( |
| 203 |
'Topic must have no subscribers', 0, asyncPubsub.getCount('someTopic')); |
| 204 |
assertEquals('Subscriber must have been called once', 1, callCount); |
| 205 |
} |
| 206 |
|
| 207 |
function testAsyncSubscribeOnceWithContext() { |
| 208 |
var context = {callCount: 0}; |
| 209 |
asyncPubsub.subscribeOnce( |
| 210 |
'someTopic', function() { this.callCount++; }, context); |
| 211 |
assertEquals( |
| 212 |
'Topic must have one subscriber', 1, asyncPubsub.getCount('someTopic')); |
| 213 |
mockClock.tick(); |
| 214 |
assertEquals( |
| 215 |
'Subscriber must not have been called yet', 0, context.callCount); |
| 216 |
|
| 217 |
asyncPubsub.publish('someTopic'); |
| 218 |
asyncPubsub.publish('someTopic'); |
| 219 |
mockClock.tick(); |
| 220 |
assertEquals( |
| 221 |
'Topic must have no subscribers', 0, asyncPubsub.getCount('someTopic')); |
| 222 |
assertEquals('Subscriber must have been called once', 1, context.callCount); |
| 223 |
} |
| 224 |
|
| 225 |
function testAsyncSubscribeOnceWithContextAndValue() { |
| 226 |
var context = {callCount: 0, value: 0}; |
| 227 |
asyncPubsub.subscribeOnce('someTopic', function(value) { |
| 228 |
this.callCount++; |
| 229 |
this.value = value; |
| 230 |
}, context); |
| 231 |
assertEquals( |
| 232 |
'Topic must have one subscriber', 1, asyncPubsub.getCount('someTopic')); |
| 233 |
mockClock.tick(); |
| 234 |
assertEquals( |
| 235 |
'Subscriber must not have been called yet', 0, context.callCount); |
| 236 |
assertEquals('Value must have expected value', 0, context.value); |
| 237 |
|
| 238 |
asyncPubsub.publish('someTopic', 17); |
| 239 |
asyncPubsub.publish('someTopic', 42); |
| 240 |
mockClock.tick(); |
| 241 |
assertEquals( |
| 242 |
'Topic must have no subscribers', 0, asyncPubsub.getCount('someTopic')); |
| 243 |
assertEquals('Subscriber must have been called once', 1, context.callCount); |
| 244 |
assertEquals('Value must have been updated', 17, context.value); |
| 245 |
} |
| 246 |
|
| 247 |
function testSubscribeOnce_boundFn() { |
| 248 |
var context = {called: false, value: 0}; |
| 249 |
|
| 250 |
function subscriber(value) { |
| 251 |
this.called = true; |
| 252 |
this.value = value; |
| 253 |
} |
| 254 |
|
| 255 |
pubsub.subscribeOnce('someTopic', goog.bind(subscriber, context)); |
| 256 |
assertEquals( |
| 257 |
'Topic must have one subscriber', 1, pubsub.getCount('someTopic')); |
| 258 |
assertFalse('Subscriber must not have been called yet', context.called); |
| 259 |
assertEquals('Value must have expected value', 0, context.value); |
| 260 |
|
| 261 |
pubsub.publish('someTopic', 17); |
| 262 |
assertEquals( |
| 263 |
'Topic must have no subscribers', 0, pubsub.getCount('someTopic')); |
| 264 |
assertTrue('Subscriber must have been called', context.called); |
| 265 |
assertEquals('Value must have been updated', 17, context.value); |
| 266 |
} |
| 267 |
|
| 268 |
function testSubscribeOnce_partialFn() { |
| 269 |
var called = false; |
| 270 |
var value = 0; |
| 271 |
|
| 272 |
function subscriber(hasBeenCalled, newValue) { |
| 273 |
called = hasBeenCalled; |
| 274 |
value = newValue; |
| 275 |
} |
| 276 |
|
| 277 |
pubsub.subscribeOnce('someTopic', goog.partial(subscriber, true)); |
| 278 |
assertEquals( |
| 279 |
'Topic must have one subscriber', 1, pubsub.getCount('someTopic')); |
| 280 |
assertFalse('Subscriber must not have been called yet', called); |
| 281 |
assertEquals('Value must have expected value', 0, value); |
| 282 |
|
| 283 |
pubsub.publish('someTopic', 17); |
| 284 |
assertEquals( |
| 285 |
'Topic must have no subscribers', 0, pubsub.getCount('someTopic')); |
| 286 |
assertTrue('Subscriber must have been called', called); |
| 287 |
assertEquals('Value must have been updated', 17, value); |
| 288 |
} |
| 289 |
|
| 290 |
function testSelfResubscribe() { |
| 291 |
var value = null; |
| 292 |
|
| 293 |
function resubscribe(iteration, newValue) { |
| 294 |
pubsub.subscribeOnce('someTopic', goog.partial(resubscribe, iteration + 1)); |
| 295 |
value = newValue + ':' + iteration; |
| 296 |
} |
| 297 |
|
| 298 |
pubsub.subscribeOnce('someTopic', goog.partial(resubscribe, 0)); |
| 299 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount('someTopic')); |
| 300 |
assertNull('Value must be null', value); |
| 301 |
|
| 302 |
pubsub.publish('someTopic', 'foo'); |
| 303 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount('someTopic')); |
| 304 |
assertEquals( |
| 305 |
'Pubsub must not have any pending unsubscribe keys', 0, |
| 306 |
pubsub.pendingKeys_.length); |
| 307 |
assertEquals('Value be as expected', 'foo:0', value); |
| 308 |
|
| 309 |
pubsub.publish('someTopic', 'bar'); |
| 310 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount('someTopic')); |
| 311 |
assertEquals( |
| 312 |
'Pubsub must not have any pending unsubscribe keys', 0, |
| 313 |
pubsub.pendingKeys_.length); |
| 314 |
assertEquals('Value be as expected', 'bar:1', value); |
| 315 |
|
| 316 |
pubsub.publish('someTopic', 'baz'); |
| 317 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount('someTopic')); |
| 318 |
assertEquals( |
| 319 |
'Pubsub must not have any pending unsubscribe keys', 0, |
| 320 |
pubsub.pendingKeys_.length); |
| 321 |
assertEquals('Value be as expected', 'baz:2', value); |
| 322 |
} |
| 323 |
|
| 324 |
function testUnsubscribeByKey() { |
| 325 |
var key1, key2, key3; |
| 326 |
|
| 327 |
key1 = pubsub.subscribe('X', function() {}); |
| 328 |
key2 = pubsub.subscribe('Y', function() {}); |
| 329 |
|
| 330 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount('X')); |
| 331 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount('Y')); |
| 332 |
assertNotEquals('Subscription keys must be distinct', key1, key2); |
| 333 |
|
| 334 |
pubsub.unsubscribeByKey(key1); |
| 335 |
assertEquals('Topic "X" must have no subscribers', 0, pubsub.getCount('X')); |
| 336 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount('Y')); |
| 337 |
|
| 338 |
key3 = pubsub.subscribe('X', function() {}); |
| 339 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount('X')); |
| 340 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount('Y')); |
| 341 |
assertNotEquals('Subscription keys must be distinct', key1, key3); |
| 342 |
assertNotEquals('Subscription keys must be distinct', key2, key3); |
| 343 |
|
| 344 |
pubsub.unsubscribeByKey(key1); // Obsolete key; should be no-op. |
| 345 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount('X')); |
| 346 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount('Y')); |
| 347 |
|
| 348 |
pubsub.unsubscribeByKey(key2); |
| 349 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount('X')); |
| 350 |
assertEquals('Topic "Y" must have no subscribers', 0, pubsub.getCount('Y')); |
| 351 |
|
| 352 |
pubsub.unsubscribeByKey(key3); |
| 353 |
assertEquals('Topic "X" must have no subscribers', 0, pubsub.getCount('X')); |
| 354 |
assertEquals('Topic "Y" must have no subscribers', 0, pubsub.getCount('Y')); |
| 355 |
} |
| 356 |
|
| 357 |
function testSubscribeUnsubscribeMultiple() { |
| 358 |
function foo() {} |
| 359 |
function bar() {} |
| 360 |
|
| 361 |
var context = {}; |
| 362 |
|
| 363 |
assertEquals( |
| 364 |
'Pubsub channel must not have any subscribers', 0, pubsub.getCount()); |
| 365 |
|
| 366 |
assertEquals( |
| 367 |
'Topic "X" must not have any subscribers', 0, pubsub.getCount('X')); |
| 368 |
assertEquals( |
| 369 |
'Topic "Y" must not have any subscribers', 0, pubsub.getCount('Y')); |
| 370 |
assertEquals( |
| 371 |
'Topic "Z" must not have any subscribers', 0, pubsub.getCount('Z')); |
| 372 |
|
| 373 |
goog.array.forEach( |
| 374 |
['X', 'Y', 'Z'], function(topic) { pubsub.subscribe(topic, foo); }); |
| 375 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount('X')); |
| 376 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount('Y')); |
| 377 |
assertEquals('Topic "Z" must have 1 subscriber', 1, pubsub.getCount('Z')); |
| 378 |
|
| 379 |
goog.array.forEach(['X', 'Y', 'Z'], function(topic) { |
| 380 |
pubsub.subscribe(topic, bar, context); |
| 381 |
}); |
| 382 |
assertEquals('Topic "X" must have 2 subscribers', 2, pubsub.getCount('X')); |
| 383 |
assertEquals('Topic "Y" must have 2 subscribers', 2, pubsub.getCount('Y')); |
| 384 |
assertEquals('Topic "Z" must have 2 subscribers', 2, pubsub.getCount('Z')); |
| 385 |
|
| 386 |
assertEquals( |
| 387 |
'Pubsub channel must have a total of 6 subscribers', 6, |
| 388 |
pubsub.getCount()); |
| 389 |
|
| 390 |
goog.array.forEach( |
| 391 |
['X', 'Y', 'Z'], function(topic) { pubsub.unsubscribe(topic, foo); }); |
| 392 |
assertEquals('Topic "X" must have 1 subscriber', 1, pubsub.getCount('X')); |
| 393 |
assertEquals('Topic "Y" must have 1 subscriber', 1, pubsub.getCount('Y')); |
| 394 |
assertEquals('Topic "Z" must have 1 subscriber', 1, pubsub.getCount('Z')); |
| 395 |
|
| 396 |
goog.array.forEach(['X', 'Y', 'Z'], function(topic) { |
| 397 |
pubsub.unsubscribe(topic, bar, context); |
| 398 |
}); |
| 399 |
assertEquals( |
| 400 |
'Topic "X" must not have any subscribers', 0, pubsub.getCount('X')); |
| 401 |
assertEquals( |
| 402 |
'Topic "Y" must not have any subscribers', 0, pubsub.getCount('Y')); |
| 403 |
assertEquals( |
| 404 |
'Topic "Z" must not have any subscribers', 0, pubsub.getCount('Z')); |
| 405 |
|
| 406 |
assertEquals( |
| 407 |
'Pubsub channel must not have any subscribers', 0, pubsub.getCount()); |
| 408 |
} |
| 409 |
|
| 410 |
function testPublish() { |
| 411 |
var context = {}; |
| 412 |
var fooCalled = false; |
| 413 |
var barCalled = false; |
| 414 |
|
| 415 |
function foo(x, y) { |
| 416 |
fooCalled = true; |
| 417 |
assertEquals('x must have expected value', 'x', x); |
| 418 |
assertEquals('y must have expected value', 'y', y); |
| 419 |
} |
| 420 |
|
| 421 |
function bar(x, y) { |
| 422 |
barCalled = true; |
| 423 |
assertEquals('Context must have expected value', context, this); |
| 424 |
assertEquals('x must have expected value', 'x', x); |
| 425 |
assertEquals('y must have expected value', 'y', y); |
| 426 |
} |
| 427 |
|
| 428 |
pubsub.subscribe('someTopic', foo); |
| 429 |
pubsub.subscribe('someTopic', bar, context); |
| 430 |
|
| 431 |
assertTrue(pubsub.publish('someTopic', 'x', 'y')); |
| 432 |
assertTrue('foo() must have been called', fooCalled); |
| 433 |
assertTrue('bar() must have been called', barCalled); |
| 434 |
|
| 435 |
fooCalled = false; |
| 436 |
barCalled = false; |
| 437 |
assertTrue(pubsub.unsubscribe('someTopic', foo)); |
| 438 |
|
| 439 |
assertTrue(pubsub.publish('someTopic', 'x', 'y')); |
| 440 |
assertFalse('foo() must not have been called', fooCalled); |
| 441 |
assertTrue('bar() must have been called', barCalled); |
| 442 |
|
| 443 |
fooCalled = false; |
| 444 |
barCalled = false; |
| 445 |
pubsub.subscribe('differentTopic', foo); |
| 446 |
|
| 447 |
assertTrue(pubsub.publish('someTopic', 'x', 'y')); |
| 448 |
assertFalse('foo() must not have been called', fooCalled); |
| 449 |
assertTrue('bar() must have been called', barCalled); |
| 450 |
} |
| 451 |
|
| 452 |
function testAsyncPublish() { |
| 453 |
var context = {}; |
| 454 |
var fooCallCount = 0; |
| 455 |
var barCallCount = 0; |
| 456 |
|
| 457 |
function foo(x, y) { |
| 458 |
fooCallCount++; |
| 459 |
assertEquals('x must have expected value', 'x', x); |
| 460 |
assertEquals('y must have expected value', 'y', y); |
| 461 |
} |
| 462 |
|
| 463 |
function bar(x, y) { |
| 464 |
barCallCount++; |
| 465 |
assertEquals('Context must have expected value', context, this); |
| 466 |
assertEquals('x must have expected value', 'x', x); |
| 467 |
assertEquals('y must have expected value', 'y', y); |
| 468 |
} |
| 469 |
|
| 470 |
asyncPubsub.subscribe('someTopic', foo); |
| 471 |
asyncPubsub.subscribe('someTopic', bar, context); |
| 472 |
|
| 473 |
assertTrue(asyncPubsub.publish('someTopic', 'x', 'y')); |
| 474 |
assertEquals('foo() must not have been called', 0, fooCallCount); |
| 475 |
assertEquals('bar() must not have been called', 0, barCallCount); |
| 476 |
mockClock.tick(); |
| 477 |
assertEquals('foo() must have been called once', 1, fooCallCount); |
| 478 |
assertEquals('bar() must have been called once', 1, barCallCount); |
| 479 |
|
| 480 |
fooCallCount = 0; |
| 481 |
barCallCount = 0; |
| 482 |
assertTrue(asyncPubsub.unsubscribe('someTopic', foo)); |
| 483 |
|
| 484 |
assertTrue(asyncPubsub.publish('someTopic', 'x', 'y')); |
| 485 |
assertEquals('foo() must not have been called', 0, fooCallCount); |
| 486 |
assertEquals('bar() must not have been called', 0, barCallCount); |
| 487 |
mockClock.tick(); |
| 488 |
assertEquals('foo() must not have been called', 0, fooCallCount); |
| 489 |
assertEquals('bar() must have been called once', 1, barCallCount); |
| 490 |
|
| 491 |
fooCallCount = 0; |
| 492 |
barCallCount = 0; |
| 493 |
asyncPubsub.subscribe('differentTopic', foo); |
| 494 |
assertTrue(asyncPubsub.publish('someTopic', 'x', 'y')); |
| 495 |
assertEquals('foo() must not have been called', 0, fooCallCount); |
| 496 |
assertEquals('bar() must not have been called', 0, barCallCount); |
| 497 |
mockClock.tick(); |
| 498 |
assertEquals('foo() must not have been called', 0, fooCallCount); |
| 499 |
assertEquals('bar() must have been called once', 1, barCallCount); |
| 500 |
} |
| 501 |
|
| 502 |
function testPublishEmptyTopic() { |
| 503 |
var fooCalled = false; |
| 504 |
function foo() { fooCalled = true; } |
| 505 |
|
| 506 |
assertFalse( |
| 507 |
'Publishing to nonexistent topic must return false', |
| 508 |
pubsub.publish('someTopic')); |
| 509 |
|
| 510 |
pubsub.subscribe('someTopic', foo); |
| 511 |
assertTrue( |
| 512 |
'Publishing to topic with subscriber must return true', |
| 513 |
pubsub.publish('someTopic')); |
| 514 |
assertTrue('Foo must have been called', fooCalled); |
| 515 |
|
| 516 |
pubsub.unsubscribe('someTopic', foo); |
| 517 |
fooCalled = false; |
| 518 |
assertFalse( |
| 519 |
'Publishing to topic without subscribers must return false', |
| 520 |
pubsub.publish('someTopic')); |
| 521 |
assertFalse('Foo must nothave been called', fooCalled); |
| 522 |
} |
| 523 |
|
| 524 |
function testSubscribeWhilePublishing() { |
| 525 |
// It's OK for a subscriber to add a new subscriber to its own topic, |
| 526 |
// but the newly added subscriber shouldn't be called until the next |
| 527 |
// publish cycle. |
| 528 |
|
| 529 |
var firstCalled = false; |
| 530 |
var secondCalled = false; |
| 531 |
|
| 532 |
pubsub.subscribe('someTopic', function() { |
| 533 |
pubsub.subscribe('someTopic', function() { secondCalled = true; }); |
| 534 |
firstCalled = true; |
| 535 |
}); |
| 536 |
assertEquals( |
| 537 |
'Topic must have one subscriber', 1, pubsub.getCount('someTopic')); |
| 538 |
assertFalse( |
| 539 |
'No subscriber must have been called yet', firstCalled || secondCalled); |
| 540 |
|
| 541 |
pubsub.publish('someTopic'); |
| 542 |
assertEquals( |
| 543 |
'Topic must have two subscribers', 2, pubsub.getCount('someTopic')); |
| 544 |
assertTrue('The first subscriber must have been called', firstCalled); |
| 545 |
assertFalse( |
| 546 |
'The second subscriber must not have been called yet', secondCalled); |
| 547 |
|
| 548 |
pubsub.publish('someTopic'); |
| 549 |
assertEquals( |
| 550 |
'Topic must have three subscribers', 3, pubsub.getCount('someTopic')); |
| 551 |
assertTrue('The first subscriber must have been called', firstCalled); |
| 552 |
assertTrue('The second subscriber must also have been called', secondCalled); |
| 553 |
} |
| 554 |
|
| 555 |
function testUnsubscribeWhilePublishing() { |
| 556 |
// It's OK for a subscriber to unsubscribe another subscriber from its |
| 557 |
// own topic, but the subscriber in question won't actually be removed |
| 558 |
// until after publishing is complete. |
| 559 |
|
| 560 |
var firstCalled = false; |
| 561 |
var secondCalled = false; |
| 562 |
var thirdCalled = false; |
| 563 |
|
| 564 |
function first() { |
| 565 |
assertTrue( |
| 566 |
'unsubscribe() must return true when unsubscribing', |
| 567 |
pubsub.unsubscribe('X', second)); |
| 568 |
assertEquals( |
| 569 |
'Topic "X" must still have 3 subscribers', 3, pubsub.getCount('X')); |
| 570 |
firstCalled = true; |
| 571 |
} |
| 572 |
pubsub.subscribe('X', first); |
| 573 |
|
| 574 |
function second() { secondCalled = true; } |
| 575 |
pubsub.subscribe('X', second); |
| 576 |
|
| 577 |
function third() { |
| 578 |
assertTrue( |
| 579 |
'unsubscribe() must return true when unsubscribing', |
| 580 |
pubsub.unsubscribe('X', first)); |
| 581 |
assertEquals( |
| 582 |
'Topic "X" must still have 3 subscribers', 3, pubsub.getCount('X')); |
| 583 |
thirdCalled = true; |
| 584 |
} |
| 585 |
pubsub.subscribe('X', third); |
| 586 |
|
| 587 |
assertEquals('Topic "X" must have 3 subscribers', 3, pubsub.getCount('X')); |
| 588 |
assertFalse( |
| 589 |
'No subscribers must have been called yet', |
| 590 |
firstCalled || secondCalled || thirdCalled); |
| 591 |
|
| 592 |
assertTrue(pubsub.publish('X')); |
| 593 |
assertTrue('First function must have been called', firstCalled); |
| 594 |
assertFalse('Second function must not have been called', secondCalled); |
| 595 |
assertTrue('Third function must have been called', thirdCalled); |
| 596 |
assertEquals( |
| 597 |
'Topic "X" must have 1 subscriber after publishing', 1, |
| 598 |
pubsub.getCount('X')); |
| 599 |
assertEquals( |
| 600 |
'PubSub must not have any subscriptions pending removal', 0, |
| 601 |
pubsub.pendingKeys_.length); |
| 602 |
} |
| 603 |
|
| 604 |
function testUnsubscribeSelfWhilePublishing() { |
| 605 |
// It's OK for a subscriber to unsubscribe itself, but it won't actually |
| 606 |
// be removed until after publishing is complete. |
| 607 |
|
| 608 |
var selfDestructCalled = false; |
| 609 |
|
| 610 |
function selfDestruct() { |
| 611 |
assertTrue( |
| 612 |
'unsubscribe() must return true when unsubscribing', |
| 613 |
pubsub.unsubscribe('someTopic', arguments.callee)); |
| 614 |
assertEquals( |
| 615 |
'Topic must still have 1 subscriber', 1, pubsub.getCount('someTopic')); |
| 616 |
selfDestructCalled = true; |
| 617 |
} |
| 618 |
|
| 619 |
pubsub.subscribe('someTopic', selfDestruct); |
| 620 |
assertEquals('Topic must have 1 subscriber', 1, pubsub.getCount('someTopic')); |
| 621 |
assertFalse( |
| 622 |
'selfDestruct() must not have been called yet', selfDestructCalled); |
| 623 |
|
| 624 |
pubsub.publish('someTopic'); |
| 625 |
assertTrue('selfDestruct() must have been called', selfDestructCalled); |
| 626 |
assertEquals( |
| 627 |
'Topic must have no subscribers after publishing', 0, |
| 628 |
pubsub.getCount('someTopic')); |
| 629 |
assertEquals( |
| 630 |
'PubSub must not have any subscriptions pending removal', 0, |
| 631 |
pubsub.pendingKeys_.length); |
| 632 |
} |
| 633 |
|
| 634 |
function testPublishReturnValue() { |
| 635 |
pubsub.subscribe( |
| 636 |
'X', function() { pubsub.unsubscribe('X', arguments.callee); }); |
| 637 |
assertTrue( |
| 638 |
'publish() must return true even if the only subscriber ' + |
| 639 |
'removes itself during publishing', |
| 640 |
pubsub.publish('X')); |
| 641 |
} |
| 642 |
|
| 643 |
function testNestedPublish() { |
| 644 |
var x1 = false; |
| 645 |
var x2 = false; |
| 646 |
var y1 = false; |
| 647 |
var y2 = false; |
| 648 |
|
| 649 |
pubsub.subscribe('X', function() { |
| 650 |
pubsub.publish('Y'); |
| 651 |
pubsub.unsubscribe('X', arguments.callee); |
| 652 |
x1 = true; |
| 653 |
}); |
| 654 |
|
| 655 |
pubsub.subscribe('X', function() { x2 = true; }); |
| 656 |
|
| 657 |
pubsub.subscribe('Y', function() { |
| 658 |
pubsub.unsubscribe('Y', arguments.callee); |
| 659 |
y1 = true; |
| 660 |
}); |
| 661 |
|
| 662 |
pubsub.subscribe('Y', function() { y2 = true; }); |
| 663 |
|
| 664 |
pubsub.publish('X'); |
| 665 |
|
| 666 |
assertTrue('x1 must be true', x1); |
| 667 |
assertTrue('x2 must be true', x2); |
| 668 |
assertTrue('y1 must be true', y1); |
| 669 |
assertTrue('y2 must be true', y2); |
| 670 |
} |
| 671 |
|
| 672 |
function testClear() { |
| 673 |
function fn() {} |
| 674 |
|
| 675 |
goog.array.forEach( |
| 676 |
['W', 'X', 'Y', 'Z'], function(topic) { pubsub.subscribe(topic, fn); }); |
| 677 |
assertEquals('Pubsub channel must have 4 subscribers', 4, pubsub.getCount()); |
| 678 |
|
| 679 |
pubsub.clear('W'); |
| 680 |
assertEquals('Pubsub channel must have 3 subscribers', 3, pubsub.getCount()); |
| 681 |
|
| 682 |
goog.array.forEach(['X', 'Y'], function(topic) { pubsub.clear(topic); }); |
| 683 |
assertEquals('Pubsub channel must have 1 subscriber', 1, pubsub.getCount()); |
| 684 |
|
| 685 |
pubsub.clear(); |
| 686 |
assertEquals('Pubsub channel must have no subscribers', 0, pubsub.getCount()); |
| 687 |
} |
| 688 |
|
| 689 |
function testSubscriberExceptionUnlocksSubscriptions() { |
| 690 |
var key1 = pubsub.subscribe('X', function() {}); |
| 691 |
|
| 692 |
pubsub.subscribe('X', function() { |
| 693 |
// Pushes "key1" onto queue to be unsubscribed after subscriptions are |
| 694 |
// processed. |
| 695 |
pubsub.unsubscribeByKey(key1); |
| 696 |
}); |
| 697 |
|
| 698 |
pubsub.subscribe('X', function() { throw 'Oh no!'; }); |
| 699 |
|
| 700 |
var key2 = pubsub.subscribe('X', function() {}); |
| 701 |
|
| 702 |
assertThrows(function() { pubsub.publish('X'); }); |
| 703 |
|
| 704 |
assertTrue(pubsub.unsubscribeByKey(key2)); |
| 705 |
// "key1" should've been successfully removed already; |
| 706 |
assertFalse(pubsub.unsubscribeByKey(key1)); |
| 707 |
} |
| 708 |
|
| 709 |
function testNestedSubscribeOnce() { |
| 710 |
var calls = 0; |
| 711 |
|
| 712 |
pubsub.subscribeOnce('X', function() { calls++; }); |
| 713 |
|
| 714 |
pubsub.subscribe('Y', function() { |
| 715 |
pubsub.publish('X'); |
| 716 |
pubsub.publish('X'); |
| 717 |
}); |
| 718 |
|
| 719 |
pubsub.publish('Y'); |
| 720 |
|
| 721 |
assertEquals('X must be called once', 1, calls); |
| 722 |
} |