prioritypool_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. goog.provide('goog.structs.PriorityPoolTest');
  15. goog.setTestOnly('goog.structs.PriorityPoolTest');
  16. goog.require('goog.structs.PriorityPool');
  17. goog.require('goog.testing.MockClock');
  18. goog.require('goog.testing.jsunit');
  19. // Implementation of the Pool class with isObjectDead() always returning TRUE,
  20. // so that the the Pool will not reuse any objects.
  21. function NoObjectReusePriorityPool(opt_min, opt_max) {
  22. goog.structs.PriorityPool.call(this, opt_min, opt_max);
  23. }
  24. goog.inherits(NoObjectReusePriorityPool, goog.structs.PriorityPool);
  25. NoObjectReusePriorityPool.prototype.objectCanBeReused = function(obj) {
  26. return false;
  27. };
  28. function testExceedMax1() {
  29. var p = new goog.structs.PriorityPool(0, 3);
  30. var getCount1 = 0;
  31. var callback1 = function(obj) {
  32. assertNotNull(obj);
  33. getCount1++;
  34. };
  35. var getCount2 = 0;
  36. var callback2 = function(obj) { getCount2++; };
  37. p.getObject(callback1);
  38. p.getObject(callback1);
  39. p.getObject(callback1);
  40. p.getObject(callback2);
  41. p.getObject(callback2);
  42. p.getObject(callback2);
  43. assertEquals('getCount for allocated, Should be 3', getCount1, 3);
  44. assertEquals('getCount for unallocated, Should be 0', getCount2, 0);
  45. }
  46. function testExceedMax2() {
  47. var p = new goog.structs.PriorityPool(0, 1);
  48. var getCount1 = 0;
  49. var callback1 = function(obj) {
  50. assertNotNull(obj);
  51. getCount1++;
  52. };
  53. var getCount2 = 0;
  54. var callback2 = function(obj) { getCount2++; };
  55. p.getObject(callback1);
  56. p.getObject(callback2);
  57. p.getObject(callback2);
  58. p.getObject(callback2);
  59. p.getObject(callback2);
  60. p.getObject(callback2);
  61. assertEquals('getCount for allocated, Should be 1', getCount1, 1);
  62. assertEquals('getCount for unallocated, Should be 0', getCount2, 0);
  63. }
  64. function testExceedMax3() {
  65. var p = new goog.structs.PriorityPool(0, 2);
  66. var obj1 = null;
  67. var callback1 = function(obj) { obj1 = obj; };
  68. var obj2 = null;
  69. var callback2 = function(obj) { obj2 = obj; };
  70. var obj3 = null;
  71. var callback3 = function(obj) { obj3 = obj; };
  72. p.getObject(callback1);
  73. p.getObject(callback2);
  74. p.getObject(callback3);
  75. assertNotNull(obj1);
  76. assertNotNull(obj2);
  77. assertNull(obj3);
  78. }
  79. function testExceedMax4() {
  80. var p = new goog.structs.PriorityPool(); // default: 10
  81. var objs = [];
  82. var getCount1 = 0;
  83. var callback1 = function(obj) {
  84. assertNotNull(obj);
  85. getCount1++;
  86. };
  87. var getCount2 = 0;
  88. var callback2 = function(obj) { getCount2++; };
  89. for (var i = 0; i < 12; i++) {
  90. p.getObject(i < 10 ? callback1 : callback2);
  91. }
  92. assertEquals('getCount for allocated, Should be 10', getCount1, 10);
  93. assertEquals('getCount for unallocated, Should be 0', getCount2, 0);
  94. }
  95. function testReleaseAndGet1() {
  96. var p = new goog.structs.PriorityPool(0, 10);
  97. var o = null;
  98. var callback = function(obj) { o = obj; };
  99. p.getObject(callback);
  100. assertEquals(1, p.getCount());
  101. assertEquals(1, p.getInUseCount());
  102. assertEquals(0, p.getFreeCount());
  103. assertTrue('Result should be true', p.releaseObject(o));
  104. assertEquals(1, p.getCount());
  105. assertEquals(0, p.getInUseCount());
  106. assertEquals(1, p.getFreeCount());
  107. }
  108. function testReleaseAndGet2() {
  109. var p = new NoObjectReusePriorityPool(0, 10);
  110. var o = null;
  111. var callback = function(obj) { o = obj; };
  112. p.getObject(callback);
  113. assertEquals(1, p.getCount());
  114. assertEquals(1, p.getInUseCount());
  115. assertEquals(0, p.getFreeCount());
  116. assertTrue('Result should be true', p.releaseObject(o));
  117. assertEquals(0, p.getCount());
  118. assertEquals(0, p.getInUseCount());
  119. assertEquals(0, p.getFreeCount());
  120. }
  121. function testReleaseAndGet3() {
  122. var p = new goog.structs.PriorityPool(0, 10);
  123. var o1 = null;
  124. var callback1 = function(obj) { o1 = obj; };
  125. var o2 = null;
  126. var callback2 = function(obj) { o2 = obj; };
  127. var o3 = null;
  128. var callback3 = function(obj) { o3 = obj; };
  129. var o4 = {};
  130. p.getObject(callback1);
  131. p.getObject(callback2);
  132. p.getObject(callback3);
  133. assertEquals(3, p.getCount());
  134. assertEquals(3, p.getInUseCount());
  135. assertEquals(0, p.getFreeCount());
  136. assertTrue('Result should be true', p.releaseObject(o1));
  137. assertTrue('Result should be true', p.releaseObject(o2));
  138. assertFalse('Result should be false', p.releaseObject(o4));
  139. assertEquals(3, p.getCount());
  140. assertEquals(1, p.getInUseCount());
  141. assertEquals(2, p.getFreeCount());
  142. }
  143. function testReleaseAndGet4() {
  144. var p = new NoObjectReusePriorityPool(0, 10);
  145. var o1 = null;
  146. var callback1 = function(obj) { o1 = obj; };
  147. var o2 = null;
  148. var callback2 = function(obj) { o2 = obj; };
  149. var o3 = null;
  150. var callback3 = function(obj) { o3 = obj; };
  151. var o4 = {};
  152. p.getObject(callback1);
  153. p.getObject(callback2);
  154. p.getObject(callback3);
  155. assertEquals(3, p.getCount());
  156. assertEquals(3, p.getInUseCount());
  157. assertEquals(0, p.getFreeCount());
  158. assertTrue('Result should be true', p.releaseObject(o1));
  159. assertTrue('Result should be true', p.releaseObject(o2));
  160. assertFalse('Result should be false', p.releaseObject(o4));
  161. assertEquals(1, p.getCount());
  162. assertEquals(1, p.getInUseCount());
  163. assertEquals(0, p.getFreeCount());
  164. }
  165. function testIsInPool1() {
  166. var p = new goog.structs.PriorityPool();
  167. var o1 = null;
  168. var callback1 = function(obj) { o1 = obj; };
  169. var o2 = null;
  170. var callback2 = function(obj) { o2 = obj; };
  171. var o3 = null;
  172. var callback3 = function(obj) { o3 = obj; };
  173. var o4 = {};
  174. var o5 = {};
  175. p.getObject(callback1);
  176. p.getObject(callback2);
  177. p.getObject(callback3);
  178. var o6 = o1;
  179. assertTrue(p.contains(o1));
  180. assertTrue(p.contains(o2));
  181. assertTrue(p.contains(o3));
  182. assertFalse(p.contains(o4));
  183. assertFalse(p.contains(o5));
  184. assertTrue(p.contains(o6));
  185. }
  186. function testSetMin1() {
  187. var p = new goog.structs.PriorityPool(0, 10);
  188. assertEquals(0, p.getCount());
  189. assertEquals(0, p.getInUseCount());
  190. assertEquals(0, p.getFreeCount());
  191. p.setMinimumCount(10);
  192. assertEquals(10, p.getCount());
  193. assertEquals(0, p.getInUseCount());
  194. assertEquals(10, p.getFreeCount());
  195. }
  196. function testSetMin2() {
  197. var p = new goog.structs.PriorityPool(0, 10);
  198. assertEquals(0, p.getCount());
  199. assertEquals(0, p.getInUseCount());
  200. assertEquals(0, p.getFreeCount());
  201. var o1 = null;
  202. var callback1 = function(obj) { o1 = obj; };
  203. p.getObject(callback1);
  204. assertEquals(1, p.getCount());
  205. assertEquals(1, p.getInUseCount());
  206. assertEquals(0, p.getFreeCount());
  207. p.setMinimumCount(10);
  208. assertEquals(10, p.getCount());
  209. assertEquals(1, p.getInUseCount());
  210. assertEquals(9, p.getFreeCount());
  211. }
  212. function testSetMax1() {
  213. var p = new goog.structs.PriorityPool(0, 10);
  214. assertEquals(0, p.getCount());
  215. assertEquals(0, p.getInUseCount());
  216. assertEquals(0, p.getFreeCount());
  217. var o1 = null;
  218. var callback1 = function(obj) { o1 = obj; };
  219. var o2 = null;
  220. var callback2 = function(obj) { o2 = obj; };
  221. var o3 = null;
  222. var callback3 = function(obj) { o3 = obj; };
  223. var o4 = null;
  224. var callback4 = function(obj) { o4 = obj; };
  225. var o5 = null;
  226. var callback5 = function(obj) { o5 = obj; };
  227. p.getObject(callback1);
  228. p.getObject(callback2);
  229. p.getObject(callback3);
  230. p.getObject(callback4);
  231. p.getObject(callback5);
  232. assertEquals(5, p.getCount());
  233. assertEquals(5, p.getInUseCount());
  234. assertEquals(0, p.getFreeCount());
  235. assertTrue('Result should be true', p.releaseObject(o5));
  236. assertEquals(5, p.getCount());
  237. assertEquals(4, p.getInUseCount());
  238. assertEquals(1, p.getFreeCount());
  239. p.setMaximumCount(4);
  240. assertEquals(4, p.getCount());
  241. assertEquals(4, p.getInUseCount());
  242. assertEquals(0, p.getFreeCount());
  243. }
  244. function testInvalidMinMax1() {
  245. var p = new goog.structs.PriorityPool(0, 10);
  246. assertEquals(0, p.getCount());
  247. assertEquals(0, p.getInUseCount());
  248. assertEquals(0, p.getFreeCount());
  249. assertThrows(function() { p.setMinimumCount(11); });
  250. }
  251. function testInvalidMinMax2() {
  252. var p = new goog.structs.PriorityPool(5, 10);
  253. assertEquals(5, p.getCount());
  254. assertEquals(0, p.getInUseCount());
  255. assertEquals(5, p.getFreeCount());
  256. assertThrows(function() { p.setMaximumCount(4); });
  257. }
  258. function testInvalidMinMax3() {
  259. assertThrows(function() { new goog.structs.PriorityPool(10, 1); });
  260. }
  261. function testQueue1() {
  262. var p = new goog.structs.PriorityPool(0, 2);
  263. var o1 = null;
  264. var callback1 = function(obj) { o1 = obj; };
  265. var o2 = null;
  266. var callback2 = function(obj) { o2 = obj; };
  267. var o3 = null;
  268. var callback3 = function(obj) { o3 = obj; };
  269. p.getObject(callback1);
  270. p.getObject(callback2);
  271. p.getObject(callback3);
  272. assertNotNull(o1);
  273. assertNotNull(o2);
  274. assertNull(o3);
  275. p.releaseObject(o1);
  276. assertNotNull(o3);
  277. }
  278. function testPriority1() {
  279. var p = new goog.structs.PriorityPool(0, 2);
  280. var o1 = null;
  281. var callback1 = function(obj) { o1 = obj; };
  282. var o2 = null;
  283. var callback2 = function(obj) { o2 = obj; };
  284. var o3 = null;
  285. var callback3 = function(obj) { o3 = obj; };
  286. var o4 = null;
  287. var callback4 = function(obj) { o4 = obj; };
  288. var o5 = null;
  289. var callback5 = function(obj) { o5 = obj; };
  290. var o6 = null;
  291. var callback6 = function(obj) { o6 = obj; };
  292. p.getObject(callback1); // Initially seeded requests.
  293. p.getObject(callback2);
  294. p.getObject(callback3, 101); // Lowest priority.
  295. p.getObject(callback4); // Second lowest priority (default is 100).
  296. p.getObject(callback5, 99); // Second highest priority.
  297. p.getObject(callback6, 0); // Highest priority.
  298. assertNotNull(o1);
  299. assertNotNull(o2);
  300. assertNull(o3);
  301. assertNull(o4);
  302. assertNull(o5);
  303. assertNull(o6);
  304. p.releaseObject(o1); // Release the first initially seeded request (o1).
  305. assertNotNull(o6); // Make sure the highest priority request (o6) started.
  306. assertNull(o3);
  307. assertNull(o4);
  308. assertNull(o5);
  309. p.releaseObject(o2); // Release the second, initially seeded request (o2).
  310. assertNotNull(o5); // The second highest priority request starts (o5).
  311. assertNull(o3);
  312. assertNull(o4);
  313. p.releaseObject(o6);
  314. assertNotNull(o4);
  315. assertNull(o3);
  316. }
  317. function testRateLimiting() {
  318. var clock = new goog.testing.MockClock();
  319. clock.install();
  320. var p = new goog.structs.PriorityPool(0, 4);
  321. p.setDelay(100);
  322. var getCount = 0;
  323. var callback = function(obj) {
  324. assertNotNull(obj);
  325. getCount++;
  326. };
  327. p.getObject(callback);
  328. assertEquals(1, getCount);
  329. p.getObject(callback);
  330. assertEquals(1, getCount);
  331. clock.tick(100);
  332. assertEquals(2, getCount);
  333. p.getObject(callback);
  334. p.getObject(callback);
  335. assertEquals(2, getCount);
  336. clock.tick(100);
  337. assertEquals(3, getCount);
  338. clock.tick(100);
  339. assertEquals(4, getCount);
  340. p.getObject(callback);
  341. assertEquals(4, getCount);
  342. clock.tick(100);
  343. assertEquals(4, getCount);
  344. goog.dispose(clock);
  345. }
  346. function testRateLimitingWithChangingDelay() {
  347. var clock = new goog.testing.MockClock();
  348. clock.install();
  349. var p = new goog.structs.PriorityPool(0, 3);
  350. p.setDelay(100);
  351. var getCount = 0;
  352. var callback = function(obj) {
  353. assertNotNull(obj);
  354. getCount++;
  355. };
  356. p.getObject(callback);
  357. assertEquals(1, getCount);
  358. p.getObject(callback);
  359. assertEquals(1, getCount);
  360. clock.tick(50);
  361. assertEquals(1, getCount);
  362. p.setDelay(50);
  363. assertEquals(2, getCount);
  364. p.getObject(callback);
  365. assertEquals(2, getCount);
  366. clock.tick(20);
  367. assertEquals(2, getCount);
  368. p.setDelay(40);
  369. assertEquals(2, getCount);
  370. clock.tick(20);
  371. assertEquals(3, getCount);
  372. goog.dispose(clock);
  373. }