Open History to see the last commit
1import { expect, test } from 'vitest'
2import { VerifiedChunkCache } from '../../src/chunk-cache.js'
3import type { Chunk } from '../../src/chunk-codec.js'
4
5const chunk: Chunk = { installationId: 'fixture', streamId: 'fixture', chunkId: 'fixture', frames: [] }
6test('concurrent reads coalesce, failed loads retry, and zero budget retains no objects', async () => {
7 const cache = new VerifiedChunkCache(0)
8 let count = 0
9 const load = async () => {
10 count++
11 await Promise.resolve()
12 return chunk
13 }
14 expect(await Promise.all([cache.get('one', load, 10), cache.get('one', load, 10)])).toEqual([chunk, chunk])
15 expect(count).toBe(1)
16 await cache.get('one', load, 10)
17 expect(count).toBe(2)
18 await expect(
19 cache.get(
20 'bad',
21 async () => {
22 throw new Error('temporary')
23 },
24 10,
25 ),
26 ).rejects.toThrow('temporary')
27 expect(await cache.get('bad', load, 10)).toBe(chunk)
28})
29
30test('LRU retention respects its byte budget and evicts old objects', async () => {
31 const cache = new VerifiedChunkCache(50)
32 let count = 0
33 const load = async () => {
34 count++
35 return chunk
36 }
37 await cache.get('a', load, 10)
38 await cache.get('b', load, 10)
39 await cache.get('a', load, 10)
40 expect(count).toBe(2)
41 await cache.get('c', load, 10)
42 await cache.get('a', load, 10)
43 expect(count).toBe(3)
44 await cache.get('b', load, 10)
45 expect(count).toBe(4)
46 cache.close()
47 await expect(cache.get('b', load, 10)).rejects.toThrow('closed')
48 expect(count).toBe(4)
49})
50
51test('canceling one reader preserves a shared read needed by another reader', async () => {
52 const cache = new VerifiedChunkCache(1000)
53 let enter!: () => void, finish!: () => void
54 const entered = new Promise<void>((resolve) => { enter = resolve })
55 const gate = new Promise<void>((resolve) => { finish = resolve })
56 let loadCount = 0, sharedSignal!: AbortSignal
57 const canceled = new AbortController()
58 const first = cache.getMany([{ key: 'a', encodedBytes: 10 }, { key: 'b', encodedBytes: 10 }], async (_missing, signal) => {
59 loadCount++; sharedSignal = signal; enter(); await gate; return [chunk, chunk]
60 }, canceled.signal)
61 const failed = expect(first).rejects.toThrow('reader canceled')
62 await entered
63 const second = cache.get('b', async () => { throw new Error('duplicate request') }, 10)
64 canceled.abort(new Error('reader canceled'))
65 await failed
66 expect(sharedSignal.aborted).toBe(false)
67 finish()
68 expect(await second).toBe(chunk)
69 expect(loadCount).toBe(1)
70 expect(cache.inFlightBytes).toBe(0)
71})
72
73test('the last reader waits for aborted I/O to settle and releases its loading budget', async () => {
74 const cache = new VerifiedChunkCache(1000, 20)
75 const canceled = new AbortController()
76 let enter!: () => void, finish!: () => void
77 const entered = new Promise<void>((resolve) => { enter = resolve })
78 const gate = new Promise<void>((resolve) => { finish = resolve })
79 let signal!: AbortSignal
80 const reading = cache.get('a', async (shared) => { signal = shared; enter(); await gate; return chunk }, 20, canceled.signal)
81 const failed = expect(reading).rejects.toThrow()
82 await entered
83 canceled.abort()
84 expect(signal.aborted).toBe(true)
85 expect(cache.inFlightBytes).toBe(20)
86 await expect(cache.get('b', async () => chunk, 10)).rejects.toThrow('capacity')
87 finish()
88 await failed
89 expect(cache.inFlightBytes).toBe(0)
90 expect(await cache.get('a', async () => chunk, 20)).toBe(chunk)
91})