2026-06-08 14:52:56 +08:00
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
|
|
|
import { LruTtlCache } from './lru-ttl-cache'
|
|
|
|
|
|
|
|
|
|
describe('LruTtlCache', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.useFakeTimers()
|
|
|
|
|
// performance.now() is also mocked by vi.useFakeTimers() in newer vitest versions
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.useRealTimers()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should store and retrieve values', () => {
|
|
|
|
|
const cache = new LruTtlCache<string, string>(10)
|
|
|
|
|
cache.set('key1', 'value1', 1000)
|
|
|
|
|
expect(cache.get('key1')).toBe('value1')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should return undefined for missing keys', () => {
|
|
|
|
|
const cache = new LruTtlCache<string, string>(10)
|
|
|
|
|
expect(cache.get('nonexistent')).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should evict items based on LRU policy', () => {
|
|
|
|
|
const cache = new LruTtlCache<string, string>(2)
|
|
|
|
|
cache.set('key1', 'value1', 1000)
|
|
|
|
|
cache.set('key2', 'value2', 1000)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Access key1 to make it most recently used
|
|
|
|
|
cache.get('key1')
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Add key3, which should evict key2 (the least recently used)
|
|
|
|
|
cache.set('key3', 'value3', 1000)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
expect(cache.get('key1')).toBe('value1')
|
|
|
|
|
expect(cache.get('key2')).toBeUndefined()
|
|
|
|
|
expect(cache.get('key3')).toBe('value3')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should return undefined for expired items', () => {
|
|
|
|
|
const cache = new LruTtlCache<string, string>(10)
|
|
|
|
|
cache.set('key1', 'value1', 1000)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
vi.advanceTimersByTime(1001)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
expect(cache.get('key1')).toBeUndefined()
|
|
|
|
|
expect(cache.size).toBe(0)
|
|
|
|
|
})
|
|
|
|
|
|
2026-11-23 13:39:31 +08:00
|
|
|
it('should expire items using wall-clock time even when performance.now pauses during sleep', () => {
|
|
|
|
|
vi.useRealTimers()
|
|
|
|
|
let wallNow = 1_700_000_000_000
|
|
|
|
|
let perfNow = 1_000
|
|
|
|
|
const dateNowSpy = vi.spyOn(Date, 'now').mockImplementation(() => wallNow)
|
|
|
|
|
const perfNowSpy = vi.spyOn(performance, 'now').mockImplementation(() => perfNow)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const cache = new LruTtlCache<string, string>(10)
|
|
|
|
|
cache.set('key1', 'value1', 60_000)
|
|
|
|
|
expect(cache.get('key1')).toBe('value1')
|
|
|
|
|
|
|
|
|
|
// Simulate host sleep: wall clock advances past TTL while monotonic clock barely moves
|
|
|
|
|
wallNow += 120_000
|
|
|
|
|
perfNow += 500
|
|
|
|
|
|
|
|
|
|
expect(cache.get('key1')).toBeUndefined()
|
|
|
|
|
expect(cache.size).toBe(0)
|
|
|
|
|
} finally {
|
|
|
|
|
dateNowSpy.mockRestore()
|
|
|
|
|
perfNowSpy.mockRestore()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
it('should proactively evict expired items on get', () => {
|
|
|
|
|
const sampleSize = 5
|
|
|
|
|
const cache = new LruTtlCache<string, string>(100, sampleSize)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Set 10 items, all will expire
|
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
|
cache.set(`key${i}`, `value${i}`, 1000)
|
|
|
|
|
}
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Add one item that stays valid
|
|
|
|
|
cache.set('valid', 'validValue', 5000)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Advance time so the 10 items expire
|
|
|
|
|
vi.advanceTimersByTime(1001)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Total size should still be 11 (lazy eviction)
|
|
|
|
|
expect(cache.size).toBe(11)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Call get on the valid item once. It should trigger one evictSample call.
|
|
|
|
|
// evictSample will check 5 items.
|
|
|
|
|
cache.get('valid')
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Size should now be 11 - 5 = 6
|
|
|
|
|
expect(cache.size).toBe(6)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Call get again, it should evict the remaining 5 expired items
|
|
|
|
|
cache.get('valid')
|
|
|
|
|
expect(cache.size).toBe(1)
|
|
|
|
|
expect(cache.get('valid')).toBe('validValue')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should loop through the cache with proactive eviction', () => {
|
|
|
|
|
// sampleSize = 3 to make sure we cover all items in one go or skip k3 correctly
|
|
|
|
|
const cache = new LruTtlCache<string, string>(10, 3)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
cache.set('k1', 'v1', 1000)
|
|
|
|
|
cache.set('k2', 'v2', 1000)
|
|
|
|
|
cache.set('k3', 'v3', 5000)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
vi.advanceTimersByTime(1001)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// First get: evicts k1, k2. Checks k3 (not expired).
|
|
|
|
|
cache.get('k3')
|
|
|
|
|
expect(cache.size).toBe(1)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Add more items
|
|
|
|
|
cache.set('k4', 'v4', 1000)
|
|
|
|
|
cache.set('k5', 'v5', 1000)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
vi.advanceTimersByTime(1001)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Second get: evicts k4, k5.
|
|
|
|
|
cache.get('k3')
|
|
|
|
|
expect(cache.size).toBe(1)
|
|
|
|
|
expect(cache.get('k3')).toBe('v3')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should handle capacity <= 0', () => {
|
|
|
|
|
const cache = new LruTtlCache<string, string>(0)
|
|
|
|
|
cache.set('key1', 'value1', 1000)
|
|
|
|
|
expect(cache.size).toBe(0)
|
|
|
|
|
expect(cache.get('key1')).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should handle ttlMs <= 0', () => {
|
|
|
|
|
const cache = new LruTtlCache<string, string>(10)
|
|
|
|
|
cache.set('key1', 'value1', 0)
|
|
|
|
|
expect(cache.size).toBe(0)
|
|
|
|
|
expect(cache.get('key1')).toBeUndefined()
|
|
|
|
|
|
|
|
|
|
cache.set('key2', 'value2', -100)
|
|
|
|
|
expect(cache.size).toBe(0)
|
|
|
|
|
expect(cache.get('key2')).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should handle manual deletions while the iterator is active', () => {
|
|
|
|
|
// This test addresses the user's specific request:
|
|
|
|
|
// "add 100 keys to cache, do a evictSample, and remove 99 keys manually, and do evictSample again, see what happen"
|
|
|
|
|
const cache = new LruTtlCache<number, number>(200, 10)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Add 100 keys
|
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
|
cache.set(i, i, 10000)
|
|
|
|
|
}
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Initial get to trigger evictSample and initialize the iterator
|
2026-06-08 16:08:41 +08:00
|
|
|
cache.get(999)
|
|
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Remove 99 keys manually (leaving key 99)
|
|
|
|
|
for (let i = 0; i < 99; i++) {
|
|
|
|
|
cache.delete(i)
|
|
|
|
|
}
|
|
|
|
|
expect(cache.size).toBe(1)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
2026-06-08 14:52:56 +08:00
|
|
|
// Advance time so the remaining key 99 would expire if it was checked
|
|
|
|
|
vi.advanceTimersByTime(20000)
|
2026-06-08 16:08:41 +08:00
|
|
|
|
|
|
|
|
// Call get. evictSample should run.
|
2026-06-08 14:52:56 +08:00
|
|
|
// The iterator was pointing to something that was deleted.
|
|
|
|
|
// It should proceed to the next available element (key 99) and evict it.
|
|
|
|
|
cache.get(999)
|
|
|
|
|
expect(cache.size).toBe(0)
|
|
|
|
|
})
|
|
|
|
|
})
|