Skip to content

Commit 91330a1

Browse files
authored
test(cache): verify TTLCache exact TTL boundary behavior (JhaSourav07#1127)
* test(cache): verify near-zero positive TTL values do not throw * test(cache): verify TTLCache exact TTL boundary behavior
1 parent 4b20ff0 commit 91330a1

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

lib/cache.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,26 @@ describe('TTLCache', () => {
226226

227227
cache.destroy();
228228
});
229+
it('returns correct values around the exact TTL boundary', () => {
230+
vi.useFakeTimers();
231+
232+
const cache = new TTLCache<string>();
233+
cache.set('key', 'value', 1000);
234+
235+
// 999ms -> still valid
236+
vi.advanceTimersByTime(999);
237+
expect(cache.get('key')).toBe('value');
238+
239+
// 1000ms exact boundary -> still valid
240+
vi.advanceTimersByTime(1);
241+
expect(cache.get('key')).toBe('value');
242+
243+
// 1001ms -> expired
244+
vi.advanceTimersByTime(1);
245+
expect(cache.get('key')).toBeNull();
246+
247+
cache.destroy();
248+
});
229249

230250
it('returns null after passing TTL expiry', () => {
231251
vi.useFakeTimers();

0 commit comments

Comments
 (0)