|
| 1 | +import { TLRUCache } from './tlru-cache'; |
| 2 | + |
| 3 | +describe('TLRU Cache', () => { |
| 4 | + let cache: TLRUCache; |
| 5 | + const expectedCacheTimeoutMs = 10; |
| 6 | + |
| 7 | + beforeEach(async () => { |
| 8 | + cache = new TLRUCache(2, expectedCacheTimeoutMs); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(async () => { |
| 12 | + jest.restoreAllMocks(); |
| 13 | + jest.clearAllTimers(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should evict cache after expiration', () => { |
| 17 | + jest.useFakeTimers(); |
| 18 | + |
| 19 | + cache.set('a', 'apple'); |
| 20 | + jest.advanceTimersByTime(expectedCacheTimeoutMs); |
| 21 | + |
| 22 | + expect(cache.get('a')).toBeUndefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not evict cache before expiration', () => { |
| 26 | + jest.useFakeTimers(); |
| 27 | + |
| 28 | + cache.set('a', 'apple'); |
| 29 | + jest.advanceTimersByTime(expectedCacheTimeoutMs - 1); |
| 30 | + expect(cache.get('a')).toBe('apple'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should evict all expired entries on .entries() call', () => { |
| 34 | + jest.useFakeTimers(); |
| 35 | + |
| 36 | + cache.set('a', 'avocado'); |
| 37 | + jest.advanceTimersByTime(expectedCacheTimeoutMs); |
| 38 | + cache.set('b', 'banana'); |
| 39 | + jest.advanceTimersByTime(expectedCacheTimeoutMs); |
| 40 | + |
| 41 | + const cacheEntries = []; |
| 42 | + |
| 43 | + for (const entry of cache.entries()) { |
| 44 | + cacheEntries.push(entry); |
| 45 | + } |
| 46 | + |
| 47 | + expect(cacheEntries.length).toBe(0); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should evict all expired entries on .keys() call', () => { |
| 51 | + jest.useFakeTimers(); |
| 52 | + |
| 53 | + cache = new TLRUCache(3, expectedCacheTimeoutMs); |
| 54 | + cache.set('a', 'avocado'); |
| 55 | + jest.advanceTimersByTime(expectedCacheTimeoutMs); |
| 56 | + cache.set('b', 'banana'); |
| 57 | + jest.advanceTimersByTime(expectedCacheTimeoutMs); |
| 58 | + cache.set('c', 'cherry'); |
| 59 | + |
| 60 | + const cacheKeys = []; |
| 61 | + |
| 62 | + for (const key of cache.keys()) { |
| 63 | + cacheKeys.push(key); |
| 64 | + } |
| 65 | + |
| 66 | + expect(cacheKeys.length).toBe(1); |
| 67 | + expect(cache.get('c')).toBe('cherry'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should evict all expired entries on .values() call', () => { |
| 71 | + jest.useFakeTimers(); |
| 72 | + cache = new TLRUCache(3, expectedCacheTimeoutMs); |
| 73 | + |
| 74 | + cache.set('a', 'avocado'); |
| 75 | + jest.advanceTimersByTime(expectedCacheTimeoutMs); |
| 76 | + cache.set('b', 'banana'); |
| 77 | + jest.advanceTimersByTime(expectedCacheTimeoutMs); |
| 78 | + cache.set('c', 'cherry'); |
| 79 | + |
| 80 | + const cacheValues = []; |
| 81 | + |
| 82 | + for (const value of cache.values()) { |
| 83 | + cacheValues.push(value); |
| 84 | + } |
| 85 | + |
| 86 | + expect(cacheValues.length).toBe(1); |
| 87 | + expect(cache.get('c')).toBe('cherry'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should overwrite existing cache entry', () => { |
| 91 | + jest.useFakeTimers(); |
| 92 | + |
| 93 | + cache.set('a', 'apple'); |
| 94 | + jest.advanceTimersByTime(expectedCacheTimeoutMs - 1); |
| 95 | + cache.set('a', 'avocado'); |
| 96 | + |
| 97 | + // spin the clock by 5sec. After that time cache entry should be still valid. |
| 98 | + jest.advanceTimersByTime(expectedCacheTimeoutMs / 2); |
| 99 | + |
| 100 | + // setting assertion in a weird way because calling cache.get() |
| 101 | + // will reset eviction timer which will mess up next assertion |
| 102 | + let avocadoInCache = false; |
| 103 | + cache.forEach((value, key) => { |
| 104 | + if (key === 'a' && value === 'avocado') { |
| 105 | + avocadoInCache = true; |
| 106 | + } |
| 107 | + }); |
| 108 | + expect(avocadoInCache).toBe(true); |
| 109 | + |
| 110 | + // after another spin of 5 sec, cache entry should evict itself |
| 111 | + jest.advanceTimersByTime(expectedCacheTimeoutMs / 2); |
| 112 | + expect(cache.get('a')).toBeUndefined(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should check if a key exists', () => { |
| 116 | + cache.set('a', 'apple'); |
| 117 | + expect(cache.has('a')).toBeTruthy(); |
| 118 | + expect(cache.has('b')).toBeFalsy(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should handle the cache capacity of zero', () => { |
| 122 | + const zeroCache = new TLRUCache(0, expectedCacheTimeoutMs); |
| 123 | + zeroCache.set('a', 'apple'); |
| 124 | + expect(zeroCache.get('a')).toBeFalsy(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should handle the cache capacity of one', () => { |
| 128 | + jest.useFakeTimers(); |
| 129 | + const oneCache = new TLRUCache(1, expectedCacheTimeoutMs); |
| 130 | + oneCache.set('a', 'apple'); |
| 131 | + jest.advanceTimersByTime(expectedCacheTimeoutMs); |
| 132 | + expect(oneCache.get('a')).toBeUndefined(); |
| 133 | + |
| 134 | + oneCache.set('a', 'avocado'); |
| 135 | + expect(oneCache.get('a')).toBe('avocado'); |
| 136 | + oneCache.set('b', 'banana'); |
| 137 | + expect(oneCache.get('a')).toBeFalsy(); |
| 138 | + expect(oneCache.get('b')).toBe('banana'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should evict oldest entry when capacity limit is reached', () => { |
| 142 | + cache.set('a', 'apple'); |
| 143 | + cache.set('b', 'banana'); |
| 144 | + cache.set('c', 'cherry'); |
| 145 | + |
| 146 | + expect(cache.get('a')).toBeUndefined(); |
| 147 | + expect(cache.has('b')).toBeTruthy(); |
| 148 | + expect(cache.has('c')).toBeTruthy(); |
| 149 | + }); |
| 150 | + |
| 151 | + /** |
| 152 | + This test case might be an overkill but in case Map() changes, |
| 153 | + or we want to ditch it completely this will remind us that insertion |
| 154 | + order is crucial for this cache to work properly |
| 155 | + **/ |
| 156 | + it('should preserve insertion order when inserting on capacity limit', () => { |
| 157 | + cache.set('a', 'apple'); |
| 158 | + cache.set('b', 'banana'); |
| 159 | + cache.set('c', 'cherry'); |
| 160 | + |
| 161 | + let keys = Array.from(cache.keys()); |
| 162 | + expect(keys[0]).toBe('b'); |
| 163 | + expect(keys[1]).toBe('c'); |
| 164 | + |
| 165 | + cache = new TLRUCache(2, expectedCacheTimeoutMs); |
| 166 | + cache.set('a', 'apple'); |
| 167 | + cache.set('b', 'banana'); |
| 168 | + cache.get('a'); |
| 169 | + cache.set('c', 'cherry'); |
| 170 | + |
| 171 | + keys = Array.from(cache.keys()); |
| 172 | + expect(keys[0]).toBe('a'); |
| 173 | + expect(keys[1]).toBe('c'); |
| 174 | + }); |
| 175 | +}); |
0 commit comments