forked from sindresorhus/quick-lru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
700 lines (602 loc) · 17.2 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
import test from 'ava';
import QuickLRU from './index.js';
const lruWithDuplicates = () => {
const lru = new QuickLRU({maxSize: 2});
lru.set('key', 'value');
lru.set('keyDupe', 1);
lru.set('keyDupe', 2);
return lru;
};
// TODO: Use `import {setTimeout as delay} from 'timers/promises';` when targeting Node.js 16.
const delay = ms =>
new Promise(resolve => {
setTimeout(resolve, ms);
});
test('main', t => {
t.throws(() => {
new QuickLRU(); // eslint-disable-line no-new
}, {message: /maxSize/});
});
test('max age - incorrect value', t => {
t.throws(() => {
new QuickLRU({maxSize: 10, maxAge: 0}); // eslint-disable-line no-new
}, {message: /maxAge/});
});
test('max age - incorrect value', t => {
t.throws(() => {
new QuickLRU({maxSize: 10, maxAge: 0}); // eslint-disable-line no-new
}, /maxAge/);
});
test('.get() / .set()', t => {
const lru = new QuickLRU({maxSize: 100});
lru.set('foo', 1);
lru.set('bar', 2);
t.is(lru.get('foo'), 1);
t.is(lru.size, 2);
});
test('.get() - limit', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
lru.set('2', 2);
t.is(lru.get('1'), 1);
t.is(lru.get('3'), undefined);
lru.set('3', 3);
lru.get('1');
lru.set('4', 4);
lru.get('1');
lru.set('5', 5);
t.true(lru.has('1'));
});
test('.set() - limit', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('foo', 1);
lru.set('bar', 2);
t.is(lru.get('foo'), 1);
t.is(lru.get('bar'), 2);
lru.set('baz', 3);
lru.set('faz', 4);
t.false(lru.has('foo'));
t.false(lru.has('bar'));
t.true(lru.has('baz'));
t.true(lru.has('faz'));
t.is(lru.size, 2);
});
test('.set() - update item', t => {
const lru = new QuickLRU({maxSize: 100});
lru.set('foo', 1);
t.is(lru.get('foo'), 1);
lru.set('foo', 2);
t.is(lru.get('foo'), 2);
t.is(lru.size, 1);
});
test('.has()', t => {
const lru = new QuickLRU({maxSize: 100});
lru.set('foo', 1);
t.true(lru.has('foo'));
});
test('.peek()', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
t.is(lru.peek('1'), 1);
lru.set('2', 2);
t.is(lru.peek('1'), 1);
t.is(lru.peek('3'), undefined);
lru.set('3', 3);
lru.set('4', 4);
t.false(lru.has('1'));
});
test('.delete()', t => {
const lru = new QuickLRU({maxSize: 100});
lru.set('foo', 1);
lru.set('bar', 2);
t.true(lru.delete('foo'));
t.false(lru.has('foo'));
t.true(lru.has('bar'));
t.false(lru.delete('foo'));
t.is(lru.size, 1);
});
test('.delete() - limit', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('foo', 1);
lru.set('bar', 2);
t.is(lru.size, 2);
t.true(lru.delete('foo'));
t.false(lru.has('foo'));
t.true(lru.has('bar'));
lru.delete('bar');
t.is(lru.size, 0);
});
test('.clear()', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('foo', 1);
lru.set('bar', 2);
lru.set('baz', 3);
lru.clear();
t.is(lru.size, 0);
});
test('.keys()', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
lru.set('2', 2);
lru.set('3', 3);
t.deepEqual([...lru.keys()].sort(), ['1', '2', '3']);
});
test('.keys() - accounts for duplicates', t => {
const lru = lruWithDuplicates();
t.deepEqual([...lru.keys()].sort(), ['key', 'keyDupe']);
});
test('.values()', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
lru.set('2', 2);
lru.set('3', 3);
t.deepEqual([...lru.values()].sort(), [1, 2, 3]);
});
test('.values() - accounts for duplicates', t => {
const lru = lruWithDuplicates();
t.deepEqual([...lru.values()].sort(), [2, 'value']);
});
test('.[Symbol.iterator]()', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
lru.set('2', 2);
lru.set('3', 3);
t.deepEqual([...lru].sort(), [
['1', 1],
['2', 2],
['3', 3]
]);
});
test('.[Symbol.iterator]() - accounts for duplicates', t => {
const lru = lruWithDuplicates();
t.deepEqual([...lru].sort(), [
['key', 'value'],
['keyDupe', 2]
]);
});
test('.size', t => {
const lru = new QuickLRU({maxSize: 100});
lru.set('1', 1);
lru.set('2', 2);
t.is(lru.size, 2);
lru.delete('1');
t.is(lru.size, 1);
lru.set('3', 3);
t.is(lru.size, 2);
});
test('.size - accounts for duplicates', t => {
const lru = lruWithDuplicates();
t.is(lru.size, 2);
});
test('max size', t => {
const lru = new QuickLRU({maxSize: 3});
lru.set('1', 1);
lru.set('2', 2);
lru.set('3', 3);
t.is(lru.size, 3);
lru.set('4', 4);
t.is(lru.size, 3);
});
test('checks total cache size does not exceed `maxSize`', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
lru.set('2', 2);
lru.get('1');
t.is(lru.oldCache.has('1'), false);
});
test('`onEviction` option method is called after `maxSize` is exceeded', t => {
const expectKey = '1';
const expectValue = 1;
let isCalled = false;
let actualKey;
let actualValue;
const onEviction = (key, value) => {
actualKey = key;
actualValue = value;
isCalled = true;
};
const lru = new QuickLRU({maxSize: 1, onEviction});
lru.set(expectKey, expectValue);
lru.set('2', 2);
t.is(actualKey, expectKey);
t.is(actualValue, expectValue);
t.true(isCalled);
});
test('set(expiry) - an individual item could have custom expiration', async t => {
const lru = new QuickLRU({maxSize: 10});
lru.set('1', 'test', {maxAge: 100});
await delay(200);
t.false(lru.has('1'));
});
test('set(expiry) - items without expiration will never expired', async t => {
const lru = new QuickLRU({maxSize: 10});
lru.set('1', 'test', {maxAge: 100});
lru.set('2', 'boo');
await delay(200);
t.false(lru.has('1'));
await delay(200);
t.true(lru.has('2'));
});
test('set(expiry) - not a number expires should not be take in account', async t => {
const lru = new QuickLRU({maxSize: 10});
lru.set('1', 'test', 'string');
lru.set('2', 'boo');
await delay(200);
t.true(lru.has('1'));
await delay(200);
t.true(lru.has('2'));
});
test('set(expiry) - local expires prevails over the global maxAge', async t => {
const lru = new QuickLRU({maxSize: 10, maxAge: 1000});
lru.set('1', 'test', {maxAge: 100});
lru.set('2', 'boo');
await delay(300);
t.false(lru.has('1'));
await delay(200);
t.true(lru.has('2'));
});
test('set(expiry) - set the same item should update the expiration time', async t => {
const lru = new QuickLRU({maxSize: 10, maxAge: 150});
lru.set('1', 'test');
await delay(100);
lru.set('1', 'test');
await delay(100);
t.true(lru.has('1'));
});
test('max age - should remove the item if has expired on call `get()` method upon the same key', async t => {
const lru = new QuickLRU({maxSize: 10, maxAge: 90});
lru.set('1', 'test');
await delay(200);
t.is(lru.get('1'), undefined);
});
test('max age - a non-recent item can also expire', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', 'test1');
lru.set('2', 'test2');
lru.set('3', 'test4');
await delay(200);
t.is(lru.get('1'), undefined);
});
test('max age - setting the item again should refresh the expiration time', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', 'test');
await delay(50);
lru.set('1', 'test2');
await delay(50);
t.is(lru.get('1'), 'test2');
});
test('max age - setting an item with a local expiration date', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', 'test');
lru.set('2', 'test2', {maxAge: 500});
await delay(200);
t.true(lru.has('2'));
await delay(300);
t.false(lru.has('2'));
});
test('max age - setting an item with a empty object as options parameter must use the global maxAge', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', 'test');
lru.set('2', 'test2', {});
await delay(200);
t.false(lru.has('2'));
});
test('max age - once an item expires, the eviction function should be called', async t => {
t.timeout(1000);
const expectKey = '1';
const expectValue = 'test';
let isCalled = false;
let actualKey;
let actualValue;
const onEviction = (key, value) => {
isCalled = true;
actualKey = key;
actualValue = value;
};
const lru = new QuickLRU({
maxSize: 2,
maxAge: 100,
onEviction
});
lru.set(expectKey, expectValue);
await delay(200);
t.is(lru.get('1'), undefined);
t.true(isCalled);
t.is(actualKey, expectKey);
t.is(actualValue, expectValue);
});
test('max age - once an non-recent item expires, the eviction function should be called', async t => {
t.timeout(1000);
const expectKeys = ['1', '2'];
const expectValues = ['test', 'test2'];
let isCalled = false;
const actualKeys = [];
const actualValues = [];
const onEviction = (key, value) => {
isCalled = true;
actualKeys.push(key);
actualValues.push(value);
};
const lru = new QuickLRU({
maxSize: 2,
maxAge: 100,
onEviction
});
lru.set('1', 'test');
lru.set('2', 'test2');
lru.set('3', 'test3');
lru.set('4', 'test4');
lru.set('5', 'test5');
await delay(200);
t.is(lru.get('1'), undefined);
t.true(isCalled);
t.deepEqual(actualKeys, expectKeys);
t.deepEqual(actualValues, expectValues);
});
test('max age - on resize, max aged items should also be evicted', async t => {
t.timeout(1000);
const expectKeys = ['1', '2', '3'];
const expectValues = ['test', 'test2', 'test3'];
let isCalled = false;
const actualKeys = [];
const actualValues = [];
const onEviction = (key, value) => {
isCalled = true;
actualKeys.push(key);
actualValues.push(value);
};
const lru = new QuickLRU({
maxSize: 3,
maxAge: 100,
onEviction
});
lru.set('1', 'test');
lru.set('2', 'test2');
lru.set('3', 'test3');
lru.set('4', 'test4');
lru.set('5', 'test5');
lru.resize(2);
await delay(200);
t.false(lru.has('1'));
t.true(isCalled);
t.deepEqual(actualKeys, expectKeys);
t.deepEqual(actualValues, expectValues);
});
test('max age - an item that is not expired can also be peek', async t => {
const lru = new QuickLRU({maxSize: 10, maxAge: 400});
lru.set('1', 'test');
await delay(200);
t.is(lru.peek('1'), 'test');
});
test('max age - peeking the item should also remove the item if it has expired', async t => {
const lru = new QuickLRU({maxSize: 10, maxAge: 100});
lru.set('1', 'test');
await delay(200);
t.is(lru.peek('1'), undefined);
});
test('max age - peeking the item should also remove expired items that are not recent', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', 'test');
lru.set('2', 'test');
lru.set('3', 'test');
await delay(200);
t.is(lru.peek('1'), undefined);
});
test('max age - non-recent items that are not expired are also valid', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 200});
lru.set('1', 'test');
lru.set('2', 'test2');
lru.set('3', 'test4');
await delay(100);
t.is(lru.get('1'), 'test');
});
test('max age - has method should delete the item if expired and return false', async t => {
const lru = new QuickLRU({maxSize: 4, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test');
lru.set('3', 'test');
await delay(200);
t.false(lru.has('1'));
});
test('max age - has method should return the item if is not expired', t => {
const lru = new QuickLRU({maxSize: 4, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test');
lru.set('3', 'test');
t.true(lru.has('1'));
});
test('max age - has method should return true for undefined value that has expiration time', t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test');
lru.set('3', 'test');
t.true(lru.has('1'));
});
test('max age - `.keys()` should return keys that are not expirated', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test2');
lru.set('3', 'test3');
await delay(200);
lru.set('4', 'loco');
t.deepEqual([...lru.keys()].sort(), ['4']);
});
test('max age - `.keys()` should return an empty list if all items has expired', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test2');
lru.set('3', 'test3');
await delay(200);
t.deepEqual([...lru.keys()].sort(), []);
});
test('max age - `.values()` should return an empty if all items has expired', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test2');
lru.set('3', 'test3');
await delay(200);
t.deepEqual([...lru.values()].sort(), []);
});
test('max age - `.values()` should return the values that are not expired', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test2');
lru.set('3', 'test3');
await delay(200);
lru.set('5', 'loco');
t.deepEqual([...lru.values()].sort(), ['loco']);
});
test('max age - `entriesDescending()` should not return expired entries', async t => {
const lru = new QuickLRU({maxSize: 10, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test2');
lru.set('3', 'test3');
await delay(200);
lru.set('4', 'coco');
lru.set('5', 'loco');
t.deepEqual([...lru.entriesDescending()], [['5', 'loco'], ['4', 'coco']]);
});
test('max age - `entriesDescending()` should not return expired entries from old cache', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test2');
lru.set('3', 'test3');
await delay(200);
lru.set('4', 'coco');
lru.set('5', 'loco');
t.deepEqual([...lru.entriesDescending()], [['5', 'loco'], ['4', 'coco']]);
});
test('max age - `entriesDescending()` should return all entries in desc order if are not expired', async t => {
const lru = new QuickLRU({maxSize: 10, maxAge: 5000});
lru.set('1', undefined);
lru.set('2', 'test2');
lru.set('3', 'test3');
await delay(200);
lru.set('4', 'coco');
lru.set('5', 'loco');
t.deepEqual([...lru.entriesDescending()], [['5', 'loco'], ['4', 'coco'], ['3', 'test3'], ['2', 'test2'], ['1', undefined]]);
});
test('max age - `entriesAscending()` should not return expired entries', async t => {
const lru = new QuickLRU({maxSize: 5, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test2');
lru.set('3', 'test3');
await delay(200);
lru.set('4', 'coco');
lru.set('5', 'loco');
t.deepEqual([...lru.entriesAscending()], [['4', 'coco'], ['5', 'loco']]);
});
test('max age - `entriesAscending() should not return expired entries even if are not recent', async t => {
const lru = new QuickLRU({maxSize: 3, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test2');
lru.set('3', 'test3');
await delay(200);
lru.set('4', 'coco');
lru.set('5', 'loco');
t.deepEqual([...lru.entriesAscending()], [['4', 'coco'], ['5', 'loco']]);
});
test('max age - `entriesAscending()` should return the entries that are not expired', async t => {
const lru = new QuickLRU({maxSize: 10, maxAge: 100});
lru.set('1', undefined);
lru.set('2', 'test2');
await delay(200);
lru.set('3', 'test3');
lru.set('4', 'coco');
lru.set('5', 'loco');
t.deepEqual([...lru.entriesAscending()], [['3', 'test3'], ['4', 'coco'], ['5', 'loco']]);
});
test('max age - `.[Symbol.iterator]()` should not return expired items', async t => {
const lru = new QuickLRU({maxSize: 2, maxAge: 100});
lru.set('key', 'value');
lru.set('key3', 1);
await delay(200);
lru.set('key4', 2);
t.deepEqual([...lru].sort(), [['key4', 2]]);
});
test('max age - `.[Symbol.iterator]()` should not return expired items that are old', async t => {
const lru = new QuickLRU({maxSize: 1, maxAge: 100});
lru.set('keyunique', 'value');
lru.set('key3unique', 1);
lru.set('key4unique', 2);
await delay(200);
t.deepEqual([...lru].sort(), []);
});
test('entriesAscending enumerates cache items oldest-first', t => {
const lru = new QuickLRU({maxSize: 3});
lru.set('1', 1);
lru.set('2', 2);
lru.set('3', 3);
lru.set('3', 7);
lru.set('2', 8);
t.deepEqual([...lru.entriesAscending()], [['1', 1], ['3', 7], ['2', 8]]);
});
test('entriesDescending enumerates cache items newest-first', t => {
const lru = new QuickLRU({maxSize: 3});
lru.set('t', 1);
lru.set('q', 2);
lru.set('a', 8);
lru.set('t', 4);
lru.set('v', 3);
t.deepEqual([...lru.entriesDescending()], [['v', 3], ['t', 4], ['a', 8], ['q', 2]]);
});
test('resize removes older items', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
lru.set('2', 2);
lru.set('3', 3);
lru.resize(1);
t.is(lru.peek('1'), undefined);
t.is(lru.peek('3'), 3);
lru.set('3', 4);
t.is(lru.peek('3'), 4);
lru.set('4', 5);
t.is(lru.peek('4'), 5);
t.is(lru.peek('2'), undefined);
});
test('resize omits evictions', t => {
const calls = [];
const onEviction = (...args) => calls.push(args);
const lru = new QuickLRU({maxSize: 2, onEviction});
lru.set('1', 1);
lru.set('2', 2);
lru.set('3', 3);
lru.resize(1);
t.true(calls.length > 0);
t.true(calls.some(([key]) => key === '1'));
});
test('resize increases capacity', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
lru.set('2', 2);
lru.resize(3);
lru.set('3', 3);
lru.set('4', 4);
lru.set('5', 5);
t.deepEqual([...lru.entriesAscending()], [['1', 1], ['2', 2], ['3', 3], ['4', 4], ['5', 5]]);
});
test('resize does not conflict with the same number of items', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
lru.set('2', 2);
lru.set('3', 3);
lru.resize(3);
lru.set('4', 4);
lru.set('5', 5);
t.deepEqual([...lru.entriesAscending()], [['1', 1], ['2', 2], ['3', 3], ['4', 4], ['5', 5]]);
});
test('resize checks parameter bounds', t => {
const lru = new QuickLRU({maxSize: 2});
t.throws(() => {
lru.resize(-1);
}, {message: /maxSize/});
});
test('function value', t => {
const lru = new QuickLRU({maxSize: 1});
let isCalled = false;
lru.set('fn', () => {
isCalled = true;
});
lru.get('fn')();
t.true(isCalled);
});