pool_test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.PoolTest');
  15. goog.setTestOnly('goog.structs.PoolTest');
  16. goog.require('goog.structs.Pool');
  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 NoObjectReusePool(opt_min, opt_max) {
  22. goog.structs.Pool.call(this, opt_min, opt_max);
  23. }
  24. goog.inherits(NoObjectReusePool, goog.structs.Pool);
  25. NoObjectReusePool.prototype.objectCanBeReused = function(obj) {
  26. return false;
  27. };
  28. function testExceedMax1() {
  29. var p = new goog.structs.Pool(0, 3);
  30. var obj1 = p.getObject();
  31. var obj2 = p.getObject();
  32. var obj3 = p.getObject();
  33. var obj4 = p.getObject();
  34. var obj5 = p.getObject();
  35. assertNotUndefined(obj1);
  36. assertNotUndefined(obj2);
  37. assertNotUndefined(obj3);
  38. assertUndefined(obj4);
  39. assertUndefined(obj5);
  40. }
  41. function testExceedMax2() {
  42. var p = new goog.structs.Pool(0, 1);
  43. var obj1 = p.getObject();
  44. var obj2 = p.getObject();
  45. var obj3 = p.getObject();
  46. var obj4 = p.getObject();
  47. var obj5 = p.getObject();
  48. assertNotUndefined(obj1);
  49. assertUndefined(obj2);
  50. assertUndefined(obj3);
  51. assertUndefined(obj4);
  52. assertUndefined(obj5);
  53. }
  54. function testExceedMax3() {
  55. var p = new goog.structs.Pool(); // default: 10
  56. var objs = [];
  57. for (var i = 0; i < 12; i++) {
  58. objs[i] = p.getObject();
  59. }
  60. for (var i = 0; i < 10; i++) {
  61. assertNotNull('First 10 should be not null', objs[i]);
  62. }
  63. assertUndefined(objs[10]);
  64. assertUndefined(objs[11]);
  65. }
  66. function testReleaseAndGet1() {
  67. var p = new goog.structs.Pool(0, 10);
  68. var o = p.getObject();
  69. assertEquals(1, p.getCount());
  70. assertEquals(1, p.getInUseCount());
  71. assertEquals(0, p.getFreeCount());
  72. assertTrue('Result should be true', p.releaseObject(o));
  73. assertEquals(1, p.getCount());
  74. assertEquals(0, p.getInUseCount());
  75. assertEquals(1, p.getFreeCount());
  76. }
  77. function testReleaseAndGet2() {
  78. var p = new NoObjectReusePool(0, 10);
  79. var o = p.getObject();
  80. assertEquals(1, p.getCount());
  81. assertEquals(1, p.getInUseCount());
  82. assertEquals(0, p.getFreeCount());
  83. assertTrue('Result should be true', p.releaseObject(o));
  84. assertEquals(0, p.getCount());
  85. assertEquals(0, p.getInUseCount());
  86. assertEquals(0, p.getFreeCount());
  87. }
  88. function testReleaseAndGet3() {
  89. var p = new goog.structs.Pool(0, 10);
  90. var o1 = p.getObject();
  91. var o2 = p.getObject();
  92. var o3 = p.getObject();
  93. var o4 = {};
  94. assertEquals(3, p.getCount());
  95. assertEquals(3, p.getInUseCount());
  96. assertEquals(0, p.getFreeCount());
  97. assertTrue('Result should be true', p.releaseObject(o1));
  98. assertTrue('Result should be true', p.releaseObject(o2));
  99. assertFalse('Result should be false', p.releaseObject(o4));
  100. assertEquals(3, p.getCount());
  101. assertEquals(1, p.getInUseCount());
  102. assertEquals(2, p.getFreeCount());
  103. }
  104. function testReleaseAndGet4() {
  105. var p = new NoObjectReusePool(0, 10);
  106. var o1 = p.getObject();
  107. var o2 = p.getObject();
  108. var o3 = p.getObject();
  109. var o4 = {};
  110. assertEquals(3, p.getCount());
  111. assertEquals(3, p.getInUseCount());
  112. assertEquals(0, p.getFreeCount());
  113. assertTrue('Result should be true', p.releaseObject(o1));
  114. assertTrue('Result should be true', p.releaseObject(o2));
  115. assertFalse('Result should be false', p.releaseObject(o4));
  116. assertEquals(1, p.getCount());
  117. assertEquals(1, p.getInUseCount());
  118. assertEquals(0, p.getFreeCount());
  119. }
  120. function testIsInPool1() {
  121. var p = new goog.structs.Pool();
  122. var o1 = p.getObject();
  123. var o2 = p.getObject();
  124. var o3 = p.getObject();
  125. var o4 = {};
  126. var o5 = {};
  127. var o6 = o1;
  128. assertTrue(p.contains(o1));
  129. assertTrue(p.contains(o2));
  130. assertTrue(p.contains(o3));
  131. assertFalse(p.contains(o4));
  132. assertFalse(p.contains(o5));
  133. assertTrue(p.contains(o6));
  134. }
  135. function testSetMin1() {
  136. var p = new goog.structs.Pool(0, 10);
  137. assertEquals(0, p.getCount());
  138. assertEquals(0, p.getInUseCount());
  139. assertEquals(0, p.getFreeCount());
  140. p.setMinimumCount(10);
  141. assertEquals(10, p.getCount());
  142. assertEquals(0, p.getInUseCount());
  143. assertEquals(10, p.getFreeCount());
  144. }
  145. function testSetMin2() {
  146. var p = new goog.structs.Pool(0, 10);
  147. assertEquals(0, p.getCount());
  148. assertEquals(0, p.getInUseCount());
  149. assertEquals(0, p.getFreeCount());
  150. var o1 = p.getObject();
  151. assertEquals(1, p.getCount());
  152. assertEquals(1, p.getInUseCount());
  153. assertEquals(0, p.getFreeCount());
  154. p.setMinimumCount(10);
  155. assertEquals(10, p.getCount());
  156. assertEquals(1, p.getInUseCount());
  157. assertEquals(9, p.getFreeCount());
  158. }
  159. function testSetMax1() {
  160. var p = new goog.structs.Pool(0, 10);
  161. assertEquals(0, p.getCount());
  162. assertEquals(0, p.getInUseCount());
  163. assertEquals(0, p.getFreeCount());
  164. var o1 = p.getObject();
  165. var o2 = p.getObject();
  166. var o3 = p.getObject();
  167. var o4 = p.getObject();
  168. var o5 = p.getObject();
  169. assertEquals(5, p.getCount());
  170. assertEquals(5, p.getInUseCount());
  171. assertEquals(0, p.getFreeCount());
  172. assertTrue('Result should be true', p.releaseObject(o5));
  173. assertEquals(5, p.getCount());
  174. assertEquals(4, p.getInUseCount());
  175. assertEquals(1, p.getFreeCount());
  176. p.setMaximumCount(4);
  177. assertEquals(4, p.getCount());
  178. assertEquals(4, p.getInUseCount());
  179. assertEquals(0, p.getFreeCount());
  180. }
  181. function testInvalidMinMax1() {
  182. var p = new goog.structs.Pool(0, 10);
  183. assertEquals(0, p.getCount());
  184. assertEquals(0, p.getInUseCount());
  185. assertEquals(0, p.getFreeCount());
  186. assertThrows(function() { p.setMinimumCount(11); });
  187. }
  188. function testInvalidMinMax2() {
  189. var p = new goog.structs.Pool(5, 10);
  190. assertEquals(5, p.getCount());
  191. assertEquals(0, p.getInUseCount());
  192. assertEquals(5, p.getFreeCount());
  193. assertThrows(function() { p.setMaximumCount(4); });
  194. }
  195. function testInvalidMinMax3() {
  196. assertThrows(function() { new goog.structs.Pool(10, 1); });
  197. }
  198. function testRateLimiting() {
  199. var clock = new goog.testing.MockClock();
  200. clock.install();
  201. var p = new goog.structs.Pool(0, 3);
  202. p.setDelay(100);
  203. assertNotUndefined(p.getObject());
  204. assertUndefined(p.getObject());
  205. clock.tick(100);
  206. assertNotUndefined(p.getObject());
  207. assertUndefined(p.getObject());
  208. clock.tick(100);
  209. assertNotUndefined(p.getObject());
  210. assertUndefined(p.getObject());
  211. clock.tick(100);
  212. assertUndefined(p.getObject());
  213. goog.dispose(clock);
  214. }