| 1 |
// Copyright 2006 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.structs.PriorityPoolTest'); |
| 16 |
goog.setTestOnly('goog.structs.PriorityPoolTest'); |
| 17 |
|
| 18 |
goog.require('goog.structs.PriorityPool'); |
| 19 |
goog.require('goog.testing.MockClock'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
// Implementation of the Pool class with isObjectDead() always returning TRUE, |
| 23 |
// so that the the Pool will not reuse any objects. |
| 24 |
function NoObjectReusePriorityPool(opt_min, opt_max) { |
| 25 |
goog.structs.PriorityPool.call(this, opt_min, opt_max); |
| 26 |
} |
| 27 |
goog.inherits(NoObjectReusePriorityPool, goog.structs.PriorityPool); |
| 28 |
|
| 29 |
NoObjectReusePriorityPool.prototype.objectCanBeReused = function(obj) { |
| 30 |
return false; |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
function testExceedMax1() { |
| 35 |
var p = new goog.structs.PriorityPool(0, 3); |
| 36 |
|
| 37 |
var getCount1 = 0; |
| 38 |
var callback1 = function(obj) { |
| 39 |
assertNotNull(obj); |
| 40 |
getCount1++; |
| 41 |
}; |
| 42 |
|
| 43 |
var getCount2 = 0; |
| 44 |
var callback2 = function(obj) { getCount2++; }; |
| 45 |
|
| 46 |
p.getObject(callback1); |
| 47 |
p.getObject(callback1); |
| 48 |
p.getObject(callback1); |
| 49 |
p.getObject(callback2); |
| 50 |
p.getObject(callback2); |
| 51 |
p.getObject(callback2); |
| 52 |
|
| 53 |
assertEquals('getCount for allocated, Should be 3', getCount1, 3); |
| 54 |
assertEquals('getCount for unallocated, Should be 0', getCount2, 0); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
function testExceedMax2() { |
| 59 |
var p = new goog.structs.PriorityPool(0, 1); |
| 60 |
|
| 61 |
var getCount1 = 0; |
| 62 |
var callback1 = function(obj) { |
| 63 |
assertNotNull(obj); |
| 64 |
getCount1++; |
| 65 |
}; |
| 66 |
|
| 67 |
var getCount2 = 0; |
| 68 |
var callback2 = function(obj) { getCount2++; }; |
| 69 |
|
| 70 |
p.getObject(callback1); |
| 71 |
p.getObject(callback2); |
| 72 |
p.getObject(callback2); |
| 73 |
p.getObject(callback2); |
| 74 |
p.getObject(callback2); |
| 75 |
p.getObject(callback2); |
| 76 |
|
| 77 |
assertEquals('getCount for allocated, Should be 1', getCount1, 1); |
| 78 |
assertEquals('getCount for unallocated, Should be 0', getCount2, 0); |
| 79 |
} |
| 80 |
|
| 81 |
function testExceedMax3() { |
| 82 |
var p = new goog.structs.PriorityPool(0, 2); |
| 83 |
|
| 84 |
var obj1 = null; |
| 85 |
var callback1 = function(obj) { obj1 = obj; }; |
| 86 |
|
| 87 |
var obj2 = null; |
| 88 |
var callback2 = function(obj) { obj2 = obj; }; |
| 89 |
|
| 90 |
var obj3 = null; |
| 91 |
var callback3 = function(obj) { obj3 = obj; }; |
| 92 |
|
| 93 |
p.getObject(callback1); |
| 94 |
p.getObject(callback2); |
| 95 |
p.getObject(callback3); |
| 96 |
|
| 97 |
assertNotNull(obj1); |
| 98 |
assertNotNull(obj2); |
| 99 |
assertNull(obj3); |
| 100 |
} |
| 101 |
|
| 102 |
function testExceedMax4() { |
| 103 |
var p = new goog.structs.PriorityPool(); // default: 10 |
| 104 |
var objs = []; |
| 105 |
|
| 106 |
var getCount1 = 0; |
| 107 |
var callback1 = function(obj) { |
| 108 |
assertNotNull(obj); |
| 109 |
getCount1++; |
| 110 |
}; |
| 111 |
|
| 112 |
var getCount2 = 0; |
| 113 |
var callback2 = function(obj) { getCount2++; }; |
| 114 |
|
| 115 |
for (var i = 0; i < 12; i++) { |
| 116 |
p.getObject(i < 10 ? callback1 : callback2); |
| 117 |
} |
| 118 |
|
| 119 |
assertEquals('getCount for allocated, Should be 10', getCount1, 10); |
| 120 |
assertEquals('getCount for unallocated, Should be 0', getCount2, 0); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
function testReleaseAndGet1() { |
| 125 |
var p = new goog.structs.PriorityPool(0, 10); |
| 126 |
|
| 127 |
var o = null; |
| 128 |
var callback = function(obj) { o = obj; }; |
| 129 |
|
| 130 |
p.getObject(callback); |
| 131 |
assertEquals(1, p.getCount()); |
| 132 |
assertEquals(1, p.getInUseCount()); |
| 133 |
assertEquals(0, p.getFreeCount()); |
| 134 |
assertTrue('Result should be true', p.releaseObject(o)); |
| 135 |
assertEquals(1, p.getCount()); |
| 136 |
assertEquals(0, p.getInUseCount()); |
| 137 |
assertEquals(1, p.getFreeCount()); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
function testReleaseAndGet2() { |
| 142 |
var p = new NoObjectReusePriorityPool(0, 10); |
| 143 |
|
| 144 |
var o = null; |
| 145 |
var callback = function(obj) { o = obj; }; |
| 146 |
|
| 147 |
p.getObject(callback); |
| 148 |
assertEquals(1, p.getCount()); |
| 149 |
assertEquals(1, p.getInUseCount()); |
| 150 |
assertEquals(0, p.getFreeCount()); |
| 151 |
assertTrue('Result should be true', p.releaseObject(o)); |
| 152 |
assertEquals(0, p.getCount()); |
| 153 |
assertEquals(0, p.getInUseCount()); |
| 154 |
assertEquals(0, p.getFreeCount()); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
function testReleaseAndGet3() { |
| 159 |
var p = new goog.structs.PriorityPool(0, 10); |
| 160 |
var o1 = null; |
| 161 |
var callback1 = function(obj) { o1 = obj; }; |
| 162 |
|
| 163 |
var o2 = null; |
| 164 |
var callback2 = function(obj) { o2 = obj; }; |
| 165 |
|
| 166 |
var o3 = null; |
| 167 |
var callback3 = function(obj) { o3 = obj; }; |
| 168 |
|
| 169 |
var o4 = {}; |
| 170 |
|
| 171 |
p.getObject(callback1); |
| 172 |
p.getObject(callback2); |
| 173 |
p.getObject(callback3); |
| 174 |
|
| 175 |
assertEquals(3, p.getCount()); |
| 176 |
assertEquals(3, p.getInUseCount()); |
| 177 |
assertEquals(0, p.getFreeCount()); |
| 178 |
assertTrue('Result should be true', p.releaseObject(o1)); |
| 179 |
assertTrue('Result should be true', p.releaseObject(o2)); |
| 180 |
assertFalse('Result should be false', p.releaseObject(o4)); |
| 181 |
assertEquals(3, p.getCount()); |
| 182 |
assertEquals(1, p.getInUseCount()); |
| 183 |
assertEquals(2, p.getFreeCount()); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
function testReleaseAndGet4() { |
| 188 |
var p = new NoObjectReusePriorityPool(0, 10); |
| 189 |
|
| 190 |
var o1 = null; |
| 191 |
var callback1 = function(obj) { o1 = obj; }; |
| 192 |
|
| 193 |
var o2 = null; |
| 194 |
var callback2 = function(obj) { o2 = obj; }; |
| 195 |
|
| 196 |
var o3 = null; |
| 197 |
var callback3 = function(obj) { o3 = obj; }; |
| 198 |
|
| 199 |
var o4 = {}; |
| 200 |
|
| 201 |
p.getObject(callback1); |
| 202 |
p.getObject(callback2); |
| 203 |
p.getObject(callback3); |
| 204 |
assertEquals(3, p.getCount()); |
| 205 |
assertEquals(3, p.getInUseCount()); |
| 206 |
assertEquals(0, p.getFreeCount()); |
| 207 |
assertTrue('Result should be true', p.releaseObject(o1)); |
| 208 |
assertTrue('Result should be true', p.releaseObject(o2)); |
| 209 |
assertFalse('Result should be false', p.releaseObject(o4)); |
| 210 |
assertEquals(1, p.getCount()); |
| 211 |
assertEquals(1, p.getInUseCount()); |
| 212 |
assertEquals(0, p.getFreeCount()); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
function testIsInPool1() { |
| 217 |
var p = new goog.structs.PriorityPool(); |
| 218 |
var o1 = null; |
| 219 |
var callback1 = function(obj) { o1 = obj; }; |
| 220 |
|
| 221 |
var o2 = null; |
| 222 |
var callback2 = function(obj) { o2 = obj; }; |
| 223 |
|
| 224 |
var o3 = null; |
| 225 |
var callback3 = function(obj) { o3 = obj; }; |
| 226 |
|
| 227 |
var o4 = {}; |
| 228 |
var o5 = {}; |
| 229 |
|
| 230 |
p.getObject(callback1); |
| 231 |
p.getObject(callback2); |
| 232 |
p.getObject(callback3); |
| 233 |
var o6 = o1; |
| 234 |
|
| 235 |
assertTrue(p.contains(o1)); |
| 236 |
assertTrue(p.contains(o2)); |
| 237 |
assertTrue(p.contains(o3)); |
| 238 |
assertFalse(p.contains(o4)); |
| 239 |
assertFalse(p.contains(o5)); |
| 240 |
assertTrue(p.contains(o6)); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
function testSetMin1() { |
| 245 |
var p = new goog.structs.PriorityPool(0, 10); |
| 246 |
|
| 247 |
assertEquals(0, p.getCount()); |
| 248 |
assertEquals(0, p.getInUseCount()); |
| 249 |
assertEquals(0, p.getFreeCount()); |
| 250 |
|
| 251 |
p.setMinimumCount(10); |
| 252 |
|
| 253 |
assertEquals(10, p.getCount()); |
| 254 |
assertEquals(0, p.getInUseCount()); |
| 255 |
assertEquals(10, p.getFreeCount()); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
function testSetMin2() { |
| 260 |
var p = new goog.structs.PriorityPool(0, 10); |
| 261 |
|
| 262 |
assertEquals(0, p.getCount()); |
| 263 |
assertEquals(0, p.getInUseCount()); |
| 264 |
assertEquals(0, p.getFreeCount()); |
| 265 |
|
| 266 |
var o1 = null; |
| 267 |
var callback1 = function(obj) { o1 = obj; }; |
| 268 |
p.getObject(callback1); |
| 269 |
|
| 270 |
assertEquals(1, p.getCount()); |
| 271 |
assertEquals(1, p.getInUseCount()); |
| 272 |
assertEquals(0, p.getFreeCount()); |
| 273 |
|
| 274 |
p.setMinimumCount(10); |
| 275 |
|
| 276 |
assertEquals(10, p.getCount()); |
| 277 |
assertEquals(1, p.getInUseCount()); |
| 278 |
assertEquals(9, p.getFreeCount()); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
function testSetMax1() { |
| 283 |
var p = new goog.structs.PriorityPool(0, 10); |
| 284 |
|
| 285 |
assertEquals(0, p.getCount()); |
| 286 |
assertEquals(0, p.getInUseCount()); |
| 287 |
assertEquals(0, p.getFreeCount()); |
| 288 |
|
| 289 |
var o1 = null; |
| 290 |
var callback1 = function(obj) { o1 = obj; }; |
| 291 |
|
| 292 |
var o2 = null; |
| 293 |
var callback2 = function(obj) { o2 = obj; }; |
| 294 |
|
| 295 |
var o3 = null; |
| 296 |
var callback3 = function(obj) { o3 = obj; }; |
| 297 |
|
| 298 |
var o4 = null; |
| 299 |
var callback4 = function(obj) { o4 = obj; }; |
| 300 |
|
| 301 |
var o5 = null; |
| 302 |
var callback5 = function(obj) { o5 = obj; }; |
| 303 |
|
| 304 |
p.getObject(callback1); |
| 305 |
p.getObject(callback2); |
| 306 |
p.getObject(callback3); |
| 307 |
p.getObject(callback4); |
| 308 |
p.getObject(callback5); |
| 309 |
|
| 310 |
assertEquals(5, p.getCount()); |
| 311 |
assertEquals(5, p.getInUseCount()); |
| 312 |
assertEquals(0, p.getFreeCount()); |
| 313 |
|
| 314 |
assertTrue('Result should be true', p.releaseObject(o5)); |
| 315 |
|
| 316 |
assertEquals(5, p.getCount()); |
| 317 |
assertEquals(4, p.getInUseCount()); |
| 318 |
assertEquals(1, p.getFreeCount()); |
| 319 |
|
| 320 |
p.setMaximumCount(4); |
| 321 |
|
| 322 |
assertEquals(4, p.getCount()); |
| 323 |
assertEquals(4, p.getInUseCount()); |
| 324 |
assertEquals(0, p.getFreeCount()); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
function testInvalidMinMax1() { |
| 329 |
var p = new goog.structs.PriorityPool(0, 10); |
| 330 |
|
| 331 |
assertEquals(0, p.getCount()); |
| 332 |
assertEquals(0, p.getInUseCount()); |
| 333 |
assertEquals(0, p.getFreeCount()); |
| 334 |
|
| 335 |
assertThrows(function() { p.setMinimumCount(11); }); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
function testInvalidMinMax2() { |
| 340 |
var p = new goog.structs.PriorityPool(5, 10); |
| 341 |
|
| 342 |
assertEquals(5, p.getCount()); |
| 343 |
assertEquals(0, p.getInUseCount()); |
| 344 |
assertEquals(5, p.getFreeCount()); |
| 345 |
|
| 346 |
assertThrows(function() { p.setMaximumCount(4); }); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
function testInvalidMinMax3() { |
| 351 |
assertThrows(function() { new goog.structs.PriorityPool(10, 1); }); |
| 352 |
} |
| 353 |
|
| 354 |
function testQueue1() { |
| 355 |
var p = new goog.structs.PriorityPool(0, 2); |
| 356 |
|
| 357 |
var o1 = null; |
| 358 |
var callback1 = function(obj) { o1 = obj; }; |
| 359 |
|
| 360 |
var o2 = null; |
| 361 |
var callback2 = function(obj) { o2 = obj; }; |
| 362 |
|
| 363 |
var o3 = null; |
| 364 |
var callback3 = function(obj) { o3 = obj; }; |
| 365 |
|
| 366 |
p.getObject(callback1); |
| 367 |
p.getObject(callback2); |
| 368 |
p.getObject(callback3); |
| 369 |
|
| 370 |
assertNotNull(o1); |
| 371 |
assertNotNull(o2); |
| 372 |
assertNull(o3); |
| 373 |
|
| 374 |
p.releaseObject(o1); |
| 375 |
assertNotNull(o3); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
function testPriority1() { |
| 380 |
var p = new goog.structs.PriorityPool(0, 2); |
| 381 |
|
| 382 |
var o1 = null; |
| 383 |
var callback1 = function(obj) { o1 = obj; }; |
| 384 |
|
| 385 |
var o2 = null; |
| 386 |
var callback2 = function(obj) { o2 = obj; }; |
| 387 |
|
| 388 |
var o3 = null; |
| 389 |
var callback3 = function(obj) { o3 = obj; }; |
| 390 |
|
| 391 |
var o4 = null; |
| 392 |
var callback4 = function(obj) { o4 = obj; }; |
| 393 |
|
| 394 |
var o5 = null; |
| 395 |
var callback5 = function(obj) { o5 = obj; }; |
| 396 |
|
| 397 |
var o6 = null; |
| 398 |
var callback6 = function(obj) { o6 = obj; }; |
| 399 |
|
| 400 |
p.getObject(callback1); // Initially seeded requests. |
| 401 |
p.getObject(callback2); |
| 402 |
|
| 403 |
p.getObject(callback3, 101); // Lowest priority. |
| 404 |
p.getObject(callback4); // Second lowest priority (default is 100). |
| 405 |
p.getObject(callback5, 99); // Second highest priority. |
| 406 |
p.getObject(callback6, 0); // Highest priority. |
| 407 |
|
| 408 |
assertNotNull(o1); |
| 409 |
assertNotNull(o2); |
| 410 |
assertNull(o3); |
| 411 |
assertNull(o4); |
| 412 |
assertNull(o5); |
| 413 |
assertNull(o6); |
| 414 |
|
| 415 |
p.releaseObject(o1); // Release the first initially seeded request (o1). |
| 416 |
assertNotNull(o6); // Make sure the highest priority request (o6) started. |
| 417 |
assertNull(o3); |
| 418 |
assertNull(o4); |
| 419 |
assertNull(o5); |
| 420 |
|
| 421 |
p.releaseObject(o2); // Release the second, initially seeded request (o2). |
| 422 |
assertNotNull(o5); // The second highest priority request starts (o5). |
| 423 |
assertNull(o3); |
| 424 |
assertNull(o4); |
| 425 |
|
| 426 |
p.releaseObject(o6); |
| 427 |
assertNotNull(o4); |
| 428 |
assertNull(o3); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
function testRateLimiting() { |
| 433 |
var clock = new goog.testing.MockClock(); |
| 434 |
clock.install(); |
| 435 |
|
| 436 |
var p = new goog.structs.PriorityPool(0, 4); |
| 437 |
p.setDelay(100); |
| 438 |
|
| 439 |
var getCount = 0; |
| 440 |
var callback = function(obj) { |
| 441 |
assertNotNull(obj); |
| 442 |
getCount++; |
| 443 |
}; |
| 444 |
|
| 445 |
p.getObject(callback); |
| 446 |
assertEquals(1, getCount); |
| 447 |
|
| 448 |
p.getObject(callback); |
| 449 |
assertEquals(1, getCount); |
| 450 |
|
| 451 |
clock.tick(100); |
| 452 |
assertEquals(2, getCount); |
| 453 |
|
| 454 |
p.getObject(callback); |
| 455 |
p.getObject(callback); |
| 456 |
assertEquals(2, getCount); |
| 457 |
|
| 458 |
clock.tick(100); |
| 459 |
assertEquals(3, getCount); |
| 460 |
|
| 461 |
clock.tick(100); |
| 462 |
assertEquals(4, getCount); |
| 463 |
|
| 464 |
p.getObject(callback); |
| 465 |
assertEquals(4, getCount); |
| 466 |
|
| 467 |
clock.tick(100); |
| 468 |
assertEquals(4, getCount); |
| 469 |
|
| 470 |
goog.dispose(clock); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
function testRateLimitingWithChangingDelay() { |
| 475 |
var clock = new goog.testing.MockClock(); |
| 476 |
clock.install(); |
| 477 |
|
| 478 |
var p = new goog.structs.PriorityPool(0, 3); |
| 479 |
p.setDelay(100); |
| 480 |
|
| 481 |
var getCount = 0; |
| 482 |
var callback = function(obj) { |
| 483 |
assertNotNull(obj); |
| 484 |
getCount++; |
| 485 |
}; |
| 486 |
|
| 487 |
p.getObject(callback); |
| 488 |
assertEquals(1, getCount); |
| 489 |
|
| 490 |
p.getObject(callback); |
| 491 |
assertEquals(1, getCount); |
| 492 |
|
| 493 |
clock.tick(50); |
| 494 |
assertEquals(1, getCount); |
| 495 |
|
| 496 |
p.setDelay(50); |
| 497 |
assertEquals(2, getCount); |
| 498 |
|
| 499 |
p.getObject(callback); |
| 500 |
assertEquals(2, getCount); |
| 501 |
|
| 502 |
clock.tick(20); |
| 503 |
assertEquals(2, getCount); |
| 504 |
|
| 505 |
p.setDelay(40); |
| 506 |
assertEquals(2, getCount); |
| 507 |
|
| 508 |
clock.tick(20); |
| 509 |
assertEquals(3, getCount); |
| 510 |
|
| 511 |
goog.dispose(clock); |
| 512 |
} |