Debian Codegraph @

closure-library/closure/goog/storage/expiringstorage_test.js

raw
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 goog.provide('goog.storage.ExpiringStorageTest');
16 goog.setTestOnly('goog.storage.ExpiringStorageTest');
17
18 goog.require('goog.storage.ExpiringStorage');
19 goog.require('goog.storage.storageTester');
20 goog.require('goog.testing.MockClock');
21 goog.require('goog.testing.jsunit');
22 goog.require('goog.testing.storage.FakeMechanism');
23
24 function testBasicOperations() {
25 var mechanism = new goog.testing.storage.FakeMechanism();
26 var storage = new goog.storage.ExpiringStorage(mechanism);
27 goog.storage.storageTester.runBasicTests(storage);
28 }
29
30 function testExpiration() {
31 var mechanism = new goog.testing.storage.FakeMechanism();
32 var clock = new goog.testing.MockClock(true);
33 var storage = new goog.storage.ExpiringStorage(mechanism);
34
35 // No expiration.
36 storage.set('first', 'one second', 1000);
37 storage.set('second', 'permanent');
38 storage.set('third', 'two seconds', 2000);
39 storage.set('fourth', 'permanent');
40 clock.tick(100);
41 assertEquals('one second', storage.get('first'));
42 assertEquals('permanent', storage.get('second'));
43 assertEquals('two seconds', storage.get('third'));
44 assertEquals('permanent', storage.get('fourth'));
45
46 // A key has expired.
47 clock.tick(1000);
48 assertUndefined(storage.get('first'));
49 assertEquals('permanent', storage.get('second'));
50 assertEquals('two seconds', storage.get('third'));
51 assertEquals('permanent', storage.get('fourth'));
52 assertNull(mechanism.get('first'));
53
54 // Add an already expired key.
55 storage.set('fourth', 'one second again', 1000);
56 assertNull(mechanism.get('fourth'));
57 assertUndefined(storage.get('fourth'));
58
59 // Another key has expired.
60 clock.tick(1000);
61 assertEquals('permanent', storage.get('second'));
62 assertUndefined(storage.get('third'));
63 assertNull(mechanism.get('third'));
64
65 // Clean up.
66 storage.remove('second');
67 assertNull(mechanism.get('second'));
68 assertUndefined(storage.get('second'));
69 clock.uninstall();
70 }
71
72 function testClockSkew() {
73 var mechanism = new goog.testing.storage.FakeMechanism();
74 var storage = new goog.storage.ExpiringStorage(mechanism);
75 var clock = new goog.testing.MockClock(true);
76
77 // Simulate clock skew.
78 clock.tick(100);
79 storage.set('first', 'one second', 1000);
80 clock.reset();
81 assertUndefined(storage.get('first'));
82 assertNull(mechanism.get('first'));
83
84 clock.uninstall();
85 }