Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80737 views
1
var test = require("tap").test
2
, LRU = require("../")
3
4
test("basic", function (t) {
5
var cache = new LRU({max: 10})
6
cache.set("key", "value")
7
t.equal(cache.get("key"), "value")
8
t.equal(cache.get("nada"), undefined)
9
t.equal(cache.length, 1)
10
t.equal(cache.max, 10)
11
t.end()
12
})
13
14
test("least recently set", function (t) {
15
var cache = new LRU(2)
16
cache.set("a", "A")
17
cache.set("b", "B")
18
cache.set("c", "C")
19
t.equal(cache.get("c"), "C")
20
t.equal(cache.get("b"), "B")
21
t.equal(cache.get("a"), undefined)
22
t.end()
23
})
24
25
test("lru recently gotten", function (t) {
26
var cache = new LRU(2)
27
cache.set("a", "A")
28
cache.set("b", "B")
29
cache.get("a")
30
cache.set("c", "C")
31
t.equal(cache.get("c"), "C")
32
t.equal(cache.get("b"), undefined)
33
t.equal(cache.get("a"), "A")
34
t.end()
35
})
36
37
test("del", function (t) {
38
var cache = new LRU(2)
39
cache.set("a", "A")
40
cache.del("a")
41
t.equal(cache.get("a"), undefined)
42
t.end()
43
})
44
45
test("max", function (t) {
46
var cache = new LRU(3)
47
48
// test changing the max, verify that the LRU items get dropped.
49
cache.max = 100
50
for (var i = 0; i < 100; i ++) cache.set(i, i)
51
t.equal(cache.length, 100)
52
for (var i = 0; i < 100; i ++) {
53
t.equal(cache.get(i), i)
54
}
55
cache.max = 3
56
t.equal(cache.length, 3)
57
for (var i = 0; i < 97; i ++) {
58
t.equal(cache.get(i), undefined)
59
}
60
for (var i = 98; i < 100; i ++) {
61
t.equal(cache.get(i), i)
62
}
63
64
// now remove the max restriction, and try again.
65
cache.max = "hello"
66
for (var i = 0; i < 100; i ++) cache.set(i, i)
67
t.equal(cache.length, 100)
68
for (var i = 0; i < 100; i ++) {
69
t.equal(cache.get(i), i)
70
}
71
// should trigger an immediate resize
72
cache.max = 3
73
t.equal(cache.length, 3)
74
for (var i = 0; i < 97; i ++) {
75
t.equal(cache.get(i), undefined)
76
}
77
for (var i = 98; i < 100; i ++) {
78
t.equal(cache.get(i), i)
79
}
80
t.end()
81
})
82
83
test("reset", function (t) {
84
var cache = new LRU(10)
85
cache.set("a", "A")
86
cache.set("b", "B")
87
cache.reset()
88
t.equal(cache.length, 0)
89
t.equal(cache.max, 10)
90
t.equal(cache.get("a"), undefined)
91
t.equal(cache.get("b"), undefined)
92
t.end()
93
})
94
95
96
// Note: `<cache>.dump()` is a debugging tool only. No guarantees are made
97
// about the format/layout of the response.
98
test("dump", function (t) {
99
var cache = new LRU(10)
100
var d = cache.dump();
101
t.equal(Object.keys(d).length, 0, "nothing in dump for empty cache")
102
cache.set("a", "A")
103
var d = cache.dump() // { a: { key: "a", value: "A", lu: 0 } }
104
t.ok(d.a)
105
t.equal(d.a.key, "a")
106
t.equal(d.a.value, "A")
107
t.equal(d.a.lu, 0)
108
109
cache.set("b", "B")
110
cache.get("b")
111
d = cache.dump()
112
t.ok(d.b)
113
t.equal(d.b.key, "b")
114
t.equal(d.b.value, "B")
115
t.equal(d.b.lu, 2)
116
117
t.end()
118
})
119
120
121
test("basic with weighed length", function (t) {
122
var cache = new LRU({
123
max: 100,
124
length: function (item) { return item.size }
125
})
126
cache.set("key", {val: "value", size: 50})
127
t.equal(cache.get("key").val, "value")
128
t.equal(cache.get("nada"), undefined)
129
t.equal(cache.lengthCalculator(cache.get("key")), 50)
130
t.equal(cache.length, 50)
131
t.equal(cache.max, 100)
132
t.end()
133
})
134
135
136
test("weighed length item too large", function (t) {
137
var cache = new LRU({
138
max: 10,
139
length: function (item) { return item.size }
140
})
141
t.equal(cache.max, 10)
142
143
// should fall out immediately
144
cache.set("key", {val: "value", size: 50})
145
146
t.equal(cache.length, 0)
147
t.equal(cache.get("key"), undefined)
148
t.end()
149
})
150
151
test("least recently set with weighed length", function (t) {
152
var cache = new LRU({
153
max:8,
154
length: function (item) { return item.length }
155
})
156
cache.set("a", "A")
157
cache.set("b", "BB")
158
cache.set("c", "CCC")
159
cache.set("d", "DDDD")
160
t.equal(cache.get("d"), "DDDD")
161
t.equal(cache.get("c"), "CCC")
162
t.equal(cache.get("b"), undefined)
163
t.equal(cache.get("a"), undefined)
164
t.end()
165
})
166
167
test("lru recently gotten with weighed length", function (t) {
168
var cache = new LRU({
169
max: 8,
170
length: function (item) { return item.length }
171
})
172
cache.set("a", "A")
173
cache.set("b", "BB")
174
cache.set("c", "CCC")
175
cache.get("a")
176
cache.get("b")
177
cache.set("d", "DDDD")
178
t.equal(cache.get("c"), undefined)
179
t.equal(cache.get("d"), "DDDD")
180
t.equal(cache.get("b"), "BB")
181
t.equal(cache.get("a"), "A")
182
t.end()
183
})
184
185
test("set returns proper booleans", function(t) {
186
var cache = new LRU({
187
max: 5,
188
length: function (item) { return item.length }
189
})
190
191
t.equal(cache.set("a", "A"), true)
192
193
// should return false for max exceeded
194
t.equal(cache.set("b", "donuts"), false)
195
196
t.equal(cache.set("b", "B"), true)
197
t.equal(cache.set("c", "CCCC"), true)
198
t.end()
199
})
200
201
test("drop the old items", function(t) {
202
var cache = new LRU({
203
max: 5,
204
maxAge: 50
205
})
206
207
cache.set("a", "A")
208
209
setTimeout(function () {
210
cache.set("b", "b")
211
t.equal(cache.get("a"), "A")
212
}, 25)
213
214
setTimeout(function () {
215
cache.set("c", "C")
216
// timed out
217
t.notOk(cache.get("a"))
218
}, 60 + 25)
219
220
setTimeout(function () {
221
t.notOk(cache.get("b"))
222
t.equal(cache.get("c"), "C")
223
}, 90)
224
225
setTimeout(function () {
226
t.notOk(cache.get("c"))
227
t.end()
228
}, 155)
229
})
230
231
test("individual item can have it's own maxAge", function(t) {
232
var cache = new LRU({
233
max: 5,
234
maxAge: 50
235
})
236
237
cache.set("a", "A", 20)
238
setTimeout(function () {
239
t.notOk(cache.get("a"))
240
t.end()
241
}, 25)
242
})
243
244
test("individual item can have it's own maxAge > cache's", function(t) {
245
var cache = new LRU({
246
max: 5,
247
maxAge: 20
248
})
249
250
cache.set("a", "A", 50)
251
setTimeout(function () {
252
t.equal(cache.get("a"), "A")
253
t.end()
254
}, 25)
255
})
256
257
test("disposal function", function(t) {
258
var disposed = false
259
var cache = new LRU({
260
max: 1,
261
dispose: function (k, n) {
262
disposed = n
263
}
264
})
265
266
cache.set(1, 1)
267
cache.set(2, 2)
268
t.equal(disposed, 1)
269
cache.set(3, 3)
270
t.equal(disposed, 2)
271
cache.reset()
272
t.equal(disposed, 3)
273
t.end()
274
})
275
276
test("disposal function on too big of item", function(t) {
277
var disposed = false
278
var cache = new LRU({
279
max: 1,
280
length: function (k) {
281
return k.length
282
},
283
dispose: function (k, n) {
284
disposed = n
285
}
286
})
287
var obj = [ 1, 2 ]
288
289
t.equal(disposed, false)
290
cache.set("obj", obj)
291
t.equal(disposed, obj)
292
t.end()
293
})
294
295
test("has()", function(t) {
296
var cache = new LRU({
297
max: 1,
298
maxAge: 10
299
})
300
301
cache.set('foo', 'bar')
302
t.equal(cache.has('foo'), true)
303
cache.set('blu', 'baz')
304
t.equal(cache.has('foo'), false)
305
t.equal(cache.has('blu'), true)
306
setTimeout(function() {
307
t.equal(cache.has('blu'), false)
308
t.end()
309
}, 15)
310
})
311
312
test("stale", function(t) {
313
var cache = new LRU({
314
maxAge: 10,
315
stale: true
316
})
317
318
cache.set('foo', 'bar')
319
t.equal(cache.get('foo'), 'bar')
320
t.equal(cache.has('foo'), true)
321
setTimeout(function() {
322
t.equal(cache.has('foo'), false)
323
t.equal(cache.get('foo'), 'bar')
324
t.equal(cache.get('foo'), undefined)
325
t.end()
326
}, 15)
327
})
328
329
test("lru update via set", function(t) {
330
var cache = LRU({ max: 2 });
331
332
cache.set('foo', 1);
333
cache.set('bar', 2);
334
cache.del('bar');
335
cache.set('baz', 3);
336
cache.set('qux', 4);
337
338
t.equal(cache.get('foo'), undefined)
339
t.equal(cache.get('bar'), undefined)
340
t.equal(cache.get('baz'), 3)
341
t.equal(cache.get('qux'), 4)
342
t.end()
343
})
344
345
test("least recently set w/ peek", function (t) {
346
var cache = new LRU(2)
347
cache.set("a", "A")
348
cache.set("b", "B")
349
t.equal(cache.peek("a"), "A")
350
cache.set("c", "C")
351
t.equal(cache.get("c"), "C")
352
t.equal(cache.get("b"), "B")
353
t.equal(cache.get("a"), undefined)
354
t.end()
355
})
356
357
test("pop the least used item", function (t) {
358
var cache = new LRU(3)
359
, last
360
361
cache.set("a", "A")
362
cache.set("b", "B")
363
cache.set("c", "C")
364
365
t.equal(cache.length, 3)
366
t.equal(cache.max, 3)
367
368
// Ensure we pop a, c, b
369
cache.get("b", "B")
370
371
last = cache.pop()
372
t.equal(last.key, "a")
373
t.equal(last.value, "A")
374
t.equal(cache.length, 2)
375
t.equal(cache.max, 3)
376
377
last = cache.pop()
378
t.equal(last.key, "c")
379
t.equal(last.value, "C")
380
t.equal(cache.length, 1)
381
t.equal(cache.max, 3)
382
383
last = cache.pop()
384
t.equal(last.key, "b")
385
t.equal(last.value, "B")
386
t.equal(cache.length, 0)
387
t.equal(cache.max, 3)
388
389
last = cache.pop()
390
t.equal(last, null)
391
t.equal(cache.length, 0)
392
t.equal(cache.max, 3)
393
394
t.end()
395
})
396
397