-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.js
More file actions
896 lines (654 loc) · 20.3 KB
/
test.js
File metadata and controls
896 lines (654 loc) · 20.3 KB
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/* eslint-env mocha */
'use strict';
const assert = require('assert');
const Cache = require('./index');
// Until we get Array.from(iterator) or [...iterator]
function arrayFrom(iterator) {
const array = [];
for (let item of iterator)
array.push(item);
return array;
}
describe('Cache with limit of zero', function() {
let cache;
before(function() {
cache = new Cache(0);
});
it('should report a limit of zero', function() {
assert.equal(cache.limit, 0);
});
it('should report a cost of zero', function() {
assert.equal(cache.cost, 0);
});
it('should report a size of zero', function() {
assert.equal(cache.size, 0);
});
describe('after setting two keys', function() {
before(function() {
cache
.set('x', 'XXX')
.set('y', 'YYY');
});
it('should have a size of zero', function() {
assert.equal(cache.size, 0);
});
it('should report a cost of zero', function() {
assert.equal(cache.cost, 0);
});
it('should have neither of these keys', function() {
assert.equal(cache.get('x'), undefined);
assert.equal(cache.get('y'), undefined);
});
it('should have no entries', function() {
const nextEntry = cache.entries().next();
assert.equal(nextEntry.value, undefined);
assert.equal(nextEntry.done, true);
});
});
});
describe('Cache with negative limit', function() {
let cache;
before(function() {
cache = new Cache(-10);
});
it('should report a limit of zero', function() {
assert.equal(cache.limit, 0);
});
it('should report a cost of zero', function() {
assert.equal(cache.cost, 0);
});
it('should report a size of zero', function() {
assert.equal(cache.size, 0);
});
});
describe('Cache with three keys', function() {
let cache;
before(function() {
cache = new Cache();
cache
.set('x', 'XXX')
.set('y', 'YYY')
.set('z', 'ZZZ');
});
it('should have a size of three', function() {
assert.equal(cache.size, 3);
});
it('should have a cost of three', function() {
assert.equal(cache.cost, 3);
});
describe('get', function() {
it('should return value of key', function() {
assert.equal(cache.get('x'), 'XXX');
assert.equal(cache.get('y'), 'YYY');
assert.equal(cache.get('z'), 'ZZZ');
});
it('should return undefined if key does not exist', function() {
assert.equal(cache.get('X'), undefined);
assert.equal(cache.get('a'), undefined);
assert.equal(cache.get(null), undefined);
});
});
describe('has', function() {
it('should return true if key exists', function() {
assert.equal(cache.has('x'), true);
assert.equal(cache.has('y'), true);
assert.equal(cache.has('z'), true);
});
it('should return false if key does not exist', function() {
assert.equal(cache.has('X'), false);
assert.equal(cache.has('a'), false);
assert.equal(cache.has(null), false);
});
});
describe('iterators', function() {
before(function() {
// This sets the LRU order to y -> z -> x
cache.get('z');
cache.get('y');
});
it('should iterate from most to least recent', function() {
const entries = arrayFrom( cache[Symbol.iterator]() );
assert.deepEqual(entries, [ [ 'y', 'YYY' ], [ 'z', 'ZZZ' ], [ 'x', 'XXX' ] ]);
});
it('should return all entries from most to least recent', function() {
const entries = arrayFrom( cache.entries() );
assert.deepEqual(entries, [ [ 'y', 'YYY' ], [ 'z', 'ZZZ' ], [ 'x', 'XXX' ] ]);
});
it('should return all keys from most to least recent', function() {
const keys = arrayFrom( cache.keys() );
assert.deepEqual(keys, [ 'y', 'z', 'x' ]);
});
it('should return all values from most to least recent', function() {
const values = arrayFrom( cache.values() );
assert.deepEqual(values, [ 'YYY', 'ZZZ', 'XXX' ]);
});
});
describe('forEach', function() {
before(function() {
// This sets the LRU order to y -> z -> x
cache.get('z');
cache.get('y');
});
it('should call with value as first argument (most to least recent)', function() {
const values = [];
cache.forEach(function(value) {
values.push(value);
});
assert.deepEqual(values, [ 'YYY', 'ZZZ', 'XXX' ]);
});
it('should call with key as second argument (most to least recent)', function() {
const keys = [];
cache.forEach(function(value, key) {
keys.push(key);
});
assert.deepEqual(keys, [ 'y', 'z', 'x' ]);
});
it('should call with cache as third argument', function() {
cache.forEach(function(value, key, collection) {
assert.equal(collection, cache);
});
});
it('should call with this = thisArg', function() {
const thisArg = {};
cache.forEach(function() {
assert.equal(this, thisArg);
}, thisArg);
});
it('should call with this = undefined if no thisArg', function() {
cache.forEach(function() {
assert.equal(this, undefined);
});
});
});
describe('set existing key', function() {
before(function() {
cache
.set('x', '<x>')
.set('z', '<z>');
});
it('should not change size', function() {
assert.equal(cache.size, 3);
});
it('should not change cost', function() {
assert.equal(cache.cost, 3);
});
it('should make last change the most recent entry', function() {
const entries = arrayFrom( cache );
assert.deepEqual(entries, [ [ 'z', '<z>' ], [ 'x', '<x>' ], [ 'y', 'YYY' ] ]);
});
it('should return new key value when asked for', function() {
assert.equal(cache.get('x'), '<x>');
});
describe('delete non-existing key', function() {
let returnValue;
before(function() {
cache.get('x');
cache.get('z');
returnValue = cache.delete('a');
});
it('should return true', function() {
assert.equal(returnValue, false);
});
it('should not change size', function() {
assert.equal(cache.size, 3);
});
it('should not change cost', function() {
assert.equal(cache.cost, 3);
});
it('should keep all the same keys', function() {
const entries = arrayFrom( cache.keys() );
assert.deepEqual(entries, [ 'z', 'x', 'y' ]);
});
});
describe('delete existing key', function() {
let returnValue;
before(function() {
cache.get('x');
cache.get('y'); // move to head to list to test conditional when removing
returnValue = cache.delete('y');
});
it('should return true', function() {
assert.equal(returnValue, true);
});
it('should change size', function() {
assert.equal(cache.size, 2);
});
it('should change cost', function() {
assert.equal(cache.cost, 2);
});
it('should not return key when iterating', function() {
const entries = arrayFrom( cache );
assert.deepEqual(entries, [ [ 'x', '<x>' ], [ 'z', '<z>' ] ]);
});
it('should return undefined for key', function() {
assert.equal(cache.get('y'), undefined);
});
it('should return existing key value when asked for', function() {
assert.equal(cache.get('x'), '<x>');
});
});
describe('clear cache', function() {
before(function() {
cache.clear();
});
it('should reset size', function() {
assert.equal(cache.size, 0);
});
it('should reset cost', function() {
assert.equal(cache.cost, 0);
});
it('should have no keys', function() {
assert.equal(cache.has('x'), false);
assert.equal(cache.has('y'), false);
assert.equal(cache.has('z'), false);
});
it('should have no values', function() {
assert.equal(cache.get('x'), undefined);
assert.equal(cache.get('y'), undefined);
assert.equal(cache.get('z'), undefined);
});
it('should have no keys to iterate over', function() {
const keys = arrayFrom( cache.keys() );
assert.deepEqual(keys, []);
});
it('should have no entries to iterate over', function() {
const entries = arrayFrom( cache );
assert.deepEqual(entries, []);
});
});
});
});
describe('Cache with limit of five', function() {
let cache;
before(function() {
cache = new Cache(5);
});
it('should report a limit of five', function() {
assert.equal(cache.limit, 5);
});
describe('after adding six keys', function() {
before(function() {
cache
.set('a', 1)
.set('b', 2)
.set('c', 3)
.set('d', 4)
.set('x', '!')
.set('e', 5)
.set('f', 6);
cache.delete('x'); // exercise some linked list logic
cache.set('g', 7);
});
it('should have size of 5', function() {
assert.equal(cache.size, 5);
});
it('should have cost of 5', function() {
assert.equal(cache.cost, 5);
});
it('should not have first key', function() {
assert.equal(cache.has('a'), false);
});
it('should not have second key', function() {
// Dropped to make room for g
assert.equal(cache.has('b'), false);
});
it('should have third key', function() {
assert.equal(cache.has('c'), true);
});
it('should have seventh key', function() {
assert.equal(cache.has('g'), true);
});
it('should have five entries to iterate over', function() {
const entries = arrayFrom( cache );
assert.deepEqual(entries, [ [ 'g', 7 ], [ 'f', 6 ], [ 'e', 5 ], [ 'd', 4 ], [ 'c', 3 ] ]);
});
describe('set key with cost 3', function() {
before(function() {
cache.set('h', 8, { cost: 3 });
});
it('should have size of 3', function() {
assert.equal(cache.size, 3);
});
it('should still have cost of 5', function() {
assert.equal(cache.cost, 5);
});
it('should drop two oldest keys', function() {
assert.equal(cache.has('c'), false);
assert.equal(cache.has('d'), false);
});
it('should have five entries to iterate over', function() {
const entries = arrayFrom( cache );
assert.deepEqual(entries, [ [ 'h', 8 ], [ 'g', 7 ], [ 'f', 6 ] ]);
});
describe('set key with cost 8', function() {
before(function() {
cache.set('i', 9, { cost: 8 });
});
it('should have size of 3', function() {
assert.equal(cache.size, 3);
});
it('should still have cost of 5', function() {
assert.equal(cache.cost, 5);
});
it('should not add new key', function() {
assert.equal(cache.has('i'), false);
});
it('should keep all the same entries', function() {
const entries = arrayFrom( cache );
assert.deepEqual(entries, [ [ 'h', 8 ], [ 'g', 7 ], [ 'f', 6 ] ]);
});
});
describe('set another key with cost 3', function() {
before(function() {
cache.set('j', 10, { cost: 3 });
});
it('should have size of 1', function() {
assert.equal(cache.size, 1);
});
it('should drop cost to 3', function() {
assert.equal(cache.cost, 3);
});
it('should add new key', function() {
assert.equal(cache.has('j'), true);
});
it('should drop all other keys', function() {
const keys = arrayFrom( cache.keys() );
assert.deepEqual(keys, [ 'j' ]);
});
});
});
});
});
describe('Cache with no limit', function() {
let cache;
before(function() {
cache = new Cache();
});
it('should report a limit of Infinity', function() {
assert.equal(cache.limit, Infinity);
});
describe('after setting two large keys', function() {
before(function() {
cache
.set('x', 'XXX', { cost: Number.MAX_SAFE_INTEGER })
.set('y', 'YYY', { cost: Number.MAX_SAFE_INTEGER });
});
it('should have size of 2', function() {
assert.equal(cache.size, 2);
});
it('should have cost double large', function() {
assert.equal(cache.cost, Number.MAX_SAFE_INTEGER * 2);
});
it('should still have both keys', function() {
assert.equal(cache.get('x'), 'XXX');
assert.equal(cache.get('y'), 'YYY');
});
it('should have both entries', function() {
const entries = arrayFrom( cache );
assert.deepEqual(entries, [ [ 'y', 'YYY' ], [ 'x', 'XXX' ] ]);
});
});
});
describe('Iterate and delete', function() {
let cache;
let keys;
before(function() {
cache = new Cache();
cache.set('x').set('y').set('z');
keys = [];
for (let key of cache.keys()) {
keys.push(key);
cache.delete(key);
}
});
it('should iterate over all keys from most to least recent', function() {
assert.deepEqual(keys, [ 'z', 'y', 'x' ]);
});
it('should delete all keys', function() {
const nextKey = cache.keys().next();
assert( !nextKey.value && nextKey.done );
});
it('should have size of 0', function() {
assert.equal(cache.size, 0);
});
it('should have cost of 0', function() {
assert.equal(cache.cost, 0);
});
});
describe('Cache with expiring entries', function() {
let cache;
before(function() {
cache = new Cache(4);
// `a` expires immediately, never gets stored
cache
.set('a', 1, { ttl: 0 })
.set('b', 2, { ttl: 50 })
.set('c', 3, { ttl: 10 })
.set('d', 4);
});
before(function(done) {
// This will expire `c`
setTimeout(done, 10);
});
it('should not store immediately expired keys', function() {
assert.equal(cache.size, 3);
});
describe('expired key', function() {
it('should not have key', function() {
assert.equal(cache.has('a'), false);
});
it('should not have value', function() {
assert.equal(cache.get('a'), undefined);
});
});
describe('not yet expired key', function() {
it('should have key', function() {
assert.equal(cache.has('b'), true);
});
it('should have value', function() {
assert.equal(cache.get('b'), 2);
});
});
describe('iterators', function() {
before(function() {
assert.equal(cache.size, 3);
});
it('should skip expired keys', function() {
const keys = arrayFrom( cache.keys() );
assert.deepEqual(keys, [ 'b', 'd' ]);
});
it('should skip expired values', function() {
const values = arrayFrom( cache.values() );
assert.deepEqual(values, [ 2, 4 ]);
});
it('should skip expired entries', function() {
const entries = arrayFrom( cache.entries() );
assert.deepEqual(entries, [ [ 'b', 2 ], [ 'd', 4 ] ]);
});
it('should not forEach over expired entries', function() {
const keys = [];
cache.forEach(function(value, key) {
keys.push(key);
});
assert.deepEqual(keys, [ 'b', 'd' ]);
});
it('should also discard expired keys', function() {
assert.equal(cache.size, 2);
});
});
describe('set three keys', function() {
before(function() {
cache.get('d'); // enforce recent order
cache
.set('e', 5)
.set('f', 6)
.set('g', 7, { ttl: 0 });
});
it('should have a size 4', function() {
assert.equal(cache.size, 4);
});
it('should have a cost 4', function() {
assert.equal(cache.cost, 4);
});
it('should keep unexpired keys', function() {
assert.equal(cache.has('b'), true);
assert.equal(cache.has('d'), true);
});
it('should add new unexpired keys', function() {
assert.equal(cache.has('e'), true);
assert.equal(cache.has('f'), true);
});
it('should not add immediately expired keys', function() {
assert.equal(cache.has('g'), false);
});
it('should iterate over recent entries', function() {
const entries = arrayFrom( cache );
assert.deepEqual(entries, [ [ 'f', 6 ], [ 'e', 5 ], [ 'd', 4 ], [ 'b', 2 ] ]);
});
});
});
describe('Clone Map', function() {
let fromMap;
before(function() {
const map = new Map([ [ 'a', 1 ], [ 'b', 2 ] ]);
fromMap = new Cache(2, map);
fromMap.materialize = function() {};
});
it('should have the same keys as the source, but most to least recent', function() {
const keys = arrayFrom( fromMap.keys() );
assert.deepEqual(keys, [ 'b', 'a' ]);
});
it('should have the same values as the source, but most to least recent', function() {
const values = arrayFrom( fromMap.values() );
assert.deepEqual(values, [ 2, 1 ]);
});
it('should have same size as the source', function() {
assert.equal(fromMap.size, 2);
});
it('should have same cost as the source', function() {
assert.equal(fromMap.cost, 2);
});
describe('Clone cache', function() {
let fromCache;
before(function() {
fromCache = new Cache(fromMap);
});
it('should have the same limit as the source', function() {
assert.equal(fromCache.limit, 2);
});
it('should have same keys as the source, most to least recent', function() {
const keys = arrayFrom( fromCache.keys() );
assert.deepEqual(keys, [ 'b', 'a' ]);
});
it('should have same values as the source, most to least recent', function() {
const values = arrayFrom( fromCache.values() );
assert.deepEqual(values, [ 2, 1 ]);
});
it('should have same size as the source', function() {
assert.equal(fromCache.size, 2);
});
it('should have same cost as the source', function() {
assert.equal(fromCache.cost, 2);
});
it('should have same materialize function', function() {
assert.equal(fromCache.materialize, fromMap.materialize);
});
});
});
describe('With materialize function', function() {
let cache;
let resolved;
before(function() {
cache = new Cache();
cache.materialize = function(key) {
const upper = key.toUpperCase();
return `${upper}${upper}${upper}`;
};
});
before(function() {
resolved = cache.get('x');
return resolved;
});
it('should resolve to materialized value', function() {
return resolved
.then(function(value) {
assert.equal(value, 'XXX');
});
});
it('should have size of 1', function() {
assert.equal( cache.size, 1 );
});
it('should have cost of 1', function() {
assert.equal( cache.size, 1 );
});
it('should have new key', function() {
assert(cache.has('x'));
});
describe('get again', function() {
it('should return same value', function() {
const again = cache.get('x');
assert.equal(again, resolved);
});
});
describe('unable to resolve', function() {
let rejected;
before(function() {
cache.clear();
cache.materialize = function() {
throw new Error('fail');
};
rejected = cache.get('y');
});
it('should reject the get', function() {
return rejected.then(function() {
throw new Error('Not expected to arrive here');
}, function() {
// Not an error
});
});
it('should still have size of 0', function() {
assert.equal( cache.size, 0 );
});
it('should still have cost of 0', function() {
assert.equal( cache.size, 0 );
});
it('should not have new key', function() {
assert.equal( cache.has('y'), false);
});
});
describe('materialize and set', function() {
let promise;
before(function() {
cache.clear();
cache.materialize = function(key) {
const lazy = Promise.resolve('ZZZ');
lazy.then(function() {
cache.set(key, lazy, { cost: 5 });
});
return lazy;
};
promise = cache.get('z');
});
it('should retrieve returned value', function() {
return promise
.then(function(value) {
assert.equal(value, 'ZZZ');
});
});
it('should have size of 1', function() {
assert.equal( cache.size, 1 );
});
it('should have cost of 5', function() {
assert.equal( cache.cost, 5 );
});
it('should have key', function() {
assert( cache.get('z') );
});
it('should keep returning value', function() {
return cache.get('z')
.then(function(value) {
assert.equal(value, 'ZZZ');
});
});
});
});