| 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 |
|
| 15 |
goog.provide('goog.async.WorkQueueTest'); |
| 16 |
goog.setTestOnly('goog.async.WorkQueueTest'); |
| 17 |
|
| 18 |
goog.require('goog.async.WorkQueue'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
|
| 21 |
|
| 22 |
var id = 0; |
| 23 |
var queue = null; |
| 24 |
|
| 25 |
|
| 26 |
function setUp() { |
| 27 |
queue = new goog.async.WorkQueue(); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
function tearDown() { |
| 32 |
queue = null; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
function testEntriesReturnedInOrder() { |
| 37 |
var fn1 = function one() {}; |
| 38 |
var scope1 = {}; |
| 39 |
var fn2 = function two() {}; |
| 40 |
var scope2 = {}; |
| 41 |
queue.add(fn1, scope1); |
| 42 |
queue.add(fn2, scope2); |
| 43 |
|
| 44 |
var item = queue.remove(); |
| 45 |
assertEquals(fn1, item.fn); |
| 46 |
assertEquals(scope1, item.scope); |
| 47 |
assertNull(item.next); |
| 48 |
|
| 49 |
item = queue.remove(); |
| 50 |
assertEquals(fn2, item.fn); |
| 51 |
assertEquals(scope2, item.scope); |
| 52 |
assertNull(item.next); |
| 53 |
|
| 54 |
item = queue.remove(); |
| 55 |
assertNull(item); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
function testReturnedItemReused() { |
| 60 |
var fn1 = function() {}; |
| 61 |
var scope1 = {}; |
| 62 |
|
| 63 |
var fn2 = function() {}; |
| 64 |
var scope2 = {}; |
| 65 |
|
| 66 |
assertEquals(0, goog.async.WorkQueue.freelist_.occupants()); |
| 67 |
|
| 68 |
queue.add(fn1, scope1); |
| 69 |
var item1 = queue.remove(); |
| 70 |
|
| 71 |
assertEquals(0, goog.async.WorkQueue.freelist_.occupants()); |
| 72 |
|
| 73 |
queue.returnUnused(item1); |
| 74 |
|
| 75 |
assertEquals(1, goog.async.WorkQueue.freelist_.occupants()); |
| 76 |
|
| 77 |
queue.add(fn2, scope2); |
| 78 |
|
| 79 |
assertEquals(0, goog.async.WorkQueue.freelist_.occupants()); |
| 80 |
|
| 81 |
var item2 = queue.remove(); |
| 82 |
|
| 83 |
assertEquals(item1, item2); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
function testEmptyQueueReturnNull() { |
| 88 |
var item1 = queue.remove(); |
| 89 |
assertNull(item1); |
| 90 |
} |