| 1 |
// Copyright 2011 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 |
/** |
| 16 |
* @fileoverview Unit tests for the collectable storage interface. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.storage.collectableStorageTester'); |
| 21 |
|
| 22 |
goog.require('goog.testing.asserts'); |
| 23 |
goog.setTestOnly('collectablestorage_test'); |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Tests basic operation: expiration and collection of collectable storage. |
| 28 |
* |
| 29 |
* @param {goog.storage.mechanism.IterableMechanism} mechanism |
| 30 |
* @param {goog.testing.MockClock} clock |
| 31 |
* @param {goog.storage.CollectableStorage} storage |
| 32 |
*/ |
| 33 |
goog.storage.collectableStorageTester.runBasicTests = function( |
| 34 |
mechanism, clock, storage) { |
| 35 |
// No expiration. |
| 36 |
storage.set('first', 'three seconds', 3000); |
| 37 |
storage.set('second', 'one second', 1000); |
| 38 |
storage.set('third', 'permanent'); |
| 39 |
storage.set('fourth', 'two seconds', 2000); |
| 40 |
clock.tick(100); |
| 41 |
storage.collect(); |
| 42 |
assertEquals('three seconds', storage.get('first')); |
| 43 |
assertEquals('one second', storage.get('second')); |
| 44 |
assertEquals('permanent', storage.get('third')); |
| 45 |
assertEquals('two seconds', storage.get('fourth')); |
| 46 |
|
| 47 |
// A key has expired. |
| 48 |
clock.tick(1000); |
| 49 |
storage.collect(); |
| 50 |
assertNull(mechanism.get('second')); |
| 51 |
assertEquals('three seconds', storage.get('first')); |
| 52 |
assertUndefined(storage.get('second')); |
| 53 |
assertEquals('permanent', storage.get('third')); |
| 54 |
assertEquals('two seconds', storage.get('fourth')); |
| 55 |
|
| 56 |
// Another two keys have expired. |
| 57 |
clock.tick(2000); |
| 58 |
storage.collect(); |
| 59 |
assertNull(mechanism.get('first')); |
| 60 |
assertNull(mechanism.get('fourth')); |
| 61 |
assertUndefined(storage.get('first')); |
| 62 |
assertEquals('permanent', storage.get('third')); |
| 63 |
assertUndefined(storage.get('fourth')); |
| 64 |
|
| 65 |
// Clean up. |
| 66 |
storage.remove('third'); |
| 67 |
assertNull(mechanism.get('third')); |
| 68 |
assertUndefined(storage.get('third')); |
| 69 |
storage.collect(); |
| 70 |
clock.uninstall(); |
| 71 |
}; |