Initial commit
[yaffs-website] / node_modules / lru-cache / test / serialize.js
1 var test = require('tap').test
2 var LRU = require('../')
3
4 test('dump', function (t) {
5   var cache = new LRU()
6
7   t.equal(cache.dump().length, 0, "nothing in dump for empty cache")
8
9   cache.set("a", "A")
10   cache.set("b", "B")
11   t.deepEqual(cache.dump(), [
12     { k: "b", v: "B", e: 0 },
13     { k: "a", v: "A", e: 0 }
14   ])
15
16   cache.set("a", "A");
17   t.deepEqual(cache.dump(), [
18     { k: "a", v: "A", e: 0 },
19     { k: "b", v: "B", e: 0 }
20   ])
21
22   cache.get("b");
23   t.deepEqual(cache.dump(), [
24     { k: "b", v: "B", e: 0 },
25     { k: "a", v: "A", e: 0 }
26   ])
27
28   cache.del("a");
29   t.deepEqual(cache.dump(), [
30     { k: "b", v: "B",  e: 0 }
31   ])
32
33   t.end()
34 })
35
36 test("do not dump stale items", function(t) {
37   var cache = new LRU({
38     max: 5,
39     maxAge: 50
40   })
41
42   //expires at 50
43   cache.set("a", "A")
44
45   setTimeout(function () {
46     //expires at 75
47     cache.set("b", "B")
48     var s = cache.dump()
49     t.equal(s.length, 2)
50     t.equal(s[0].k, "b")
51     t.equal(s[1].k, "a")
52   }, 25)
53
54   setTimeout(function () {
55     //expires at 110
56     cache.set("c", "C")
57     var s = cache.dump()
58     t.equal(s.length, 2)
59     t.equal(s[0].k, "c")
60     t.equal(s[1].k, "b")
61   }, 60)
62
63   setTimeout(function () {
64     //expires at 130
65     cache.set("d", "D", 40)
66     var s = cache.dump()
67     t.equal(s.length, 2)
68     t.equal(s[0].k, "d")
69     t.equal(s[1].k, "c")
70   }, 90)
71
72   setTimeout(function () {
73     var s = cache.dump()
74     t.equal(s.length, 1)
75     t.equal(s[0].k, "d")
76   }, 120)
77
78   setTimeout(function () {
79     var s = cache.dump()
80     t.deepEqual(s, [])
81     t.end()
82   }, 155)
83 })
84
85 test("load basic cache", function(t) {
86   var cache = new LRU(),
87       copy = new LRU()
88
89   cache.set("a", "A")
90   cache.set("b", "B")
91
92   copy.load(cache.dump())
93   t.deepEquals(cache.dump(), copy.dump())
94
95   t.end()
96 })
97
98
99 test("load staled cache", function(t) {
100   var cache = new LRU({maxAge: 50}),
101       copy = new LRU({maxAge: 50}),
102       arr
103
104   //expires at 50
105   cache.set("a", "A")
106   setTimeout(function () {
107     //expires at 80
108     cache.set("b", "B")
109     arr = cache.dump()
110     t.equal(arr.length, 2)
111   }, 30)
112
113   setTimeout(function () {
114     copy.load(arr)
115     t.equal(copy.get("a"), undefined)
116     t.equal(copy.get("b"), "B")
117   }, 60)
118
119   setTimeout(function () {
120     t.equal(copy.get("b"), undefined)
121     t.end()
122   }, 90)
123 })
124
125 test("load to other size cache", function(t) {
126   var cache = new LRU({max: 2}),
127       copy = new LRU({max: 1})
128
129   cache.set("a", "A")
130   cache.set("b", "B")
131
132   copy.load(cache.dump())
133   t.equal(copy.get("a"), undefined)
134   t.equal(copy.get("b"), "B")
135
136   //update the last read from original cache
137   cache.get("a")
138   copy.load(cache.dump())
139   t.equal(copy.get("a"), "A")
140   t.equal(copy.get("b"), undefined)
141
142   t.end()
143 })
144
145
146 test("load to other age cache", function(t) {
147   var cache = new LRU({maxAge: 50}),
148       aged = new LRU({maxAge: 100}),
149       simple = new LRU(),
150       arr,
151       expired
152
153   //created at 0
154   //a would be valid till 0 + 50
155   cache.set("a", "A")
156   setTimeout(function () {
157     //created at 20
158     //b would be valid till 20 + 50
159     cache.set("b", "B")
160     //b would be valid till 20 + 70
161     cache.set("c", "C", 70)
162     arr = cache.dump()
163     t.equal(arr.length, 3)
164   }, 20)
165
166   setTimeout(function () {
167     t.equal(cache.get("a"), undefined)
168     t.equal(cache.get("b"), "B")
169     t.equal(cache.get("c"), "C")
170
171     aged.load(arr)
172     t.equal(aged.get("a"), undefined)
173     t.equal(aged.get("b"), "B")
174     t.equal(aged.get("c"), "C")
175
176     simple.load(arr)
177     t.equal(simple.get("a"), undefined)
178     t.equal(simple.get("b"), "B")
179     t.equal(simple.get("c"), "C")
180   }, 60)
181
182   setTimeout(function () {
183     t.equal(cache.get("a"), undefined)
184     t.equal(cache.get("b"), undefined)
185     t.equal(cache.get("c"), "C")
186
187     aged.load(arr)
188     t.equal(aged.get("a"), undefined)
189     t.equal(aged.get("b"), undefined)
190     t.equal(aged.get("c"), "C")
191
192     simple.load(arr)
193     t.equal(simple.get("a"), undefined)
194     t.equal(simple.get("b"), undefined)
195     t.equal(simple.get("c"), "C")
196   }, 80)
197
198   setTimeout(function () {
199     t.equal(cache.get("a"), undefined)
200     t.equal(cache.get("b"), undefined)
201     t.equal(cache.get("c"), undefined)
202
203     aged.load(arr)
204     t.equal(aged.get("a"), undefined)
205     t.equal(aged.get("b"), undefined)
206     t.equal(aged.get("c"), undefined)
207
208     simple.load(arr)
209     t.equal(simple.get("a"), undefined)
210     t.equal(simple.get("b"), undefined)
211     t.equal(simple.get("c"), undefined)
212     t.end()
213   }, 100)
214
215 })
216