workqueue_test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 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.async.WorkQueueTest');
  15. goog.setTestOnly('goog.async.WorkQueueTest');
  16. goog.require('goog.async.WorkQueue');
  17. goog.require('goog.testing.jsunit');
  18. var id = 0;
  19. var queue = null;
  20. function setUp() {
  21. queue = new goog.async.WorkQueue();
  22. }
  23. function tearDown() {
  24. queue = null;
  25. }
  26. function testEntriesReturnedInOrder() {
  27. var fn1 = function one() {};
  28. var scope1 = {};
  29. var fn2 = function two() {};
  30. var scope2 = {};
  31. queue.add(fn1, scope1);
  32. queue.add(fn2, scope2);
  33. var item = queue.remove();
  34. assertEquals(fn1, item.fn);
  35. assertEquals(scope1, item.scope);
  36. assertNull(item.next);
  37. item = queue.remove();
  38. assertEquals(fn2, item.fn);
  39. assertEquals(scope2, item.scope);
  40. assertNull(item.next);
  41. item = queue.remove();
  42. assertNull(item);
  43. }
  44. function testReturnedItemReused() {
  45. var fn1 = function() {};
  46. var scope1 = {};
  47. var fn2 = function() {};
  48. var scope2 = {};
  49. assertEquals(0, goog.async.WorkQueue.freelist_.occupants());
  50. queue.add(fn1, scope1);
  51. var item1 = queue.remove();
  52. assertEquals(0, goog.async.WorkQueue.freelist_.occupants());
  53. queue.returnUnused(item1);
  54. assertEquals(1, goog.async.WorkQueue.freelist_.occupants());
  55. queue.add(fn2, scope2);
  56. assertEquals(0, goog.async.WorkQueue.freelist_.occupants());
  57. var item2 = queue.remove();
  58. assertEquals(item1, item2);
  59. }
  60. function testEmptyQueueReturnNull() {
  61. var item1 = queue.remove();
  62. assertNull(item1);
  63. }