|
| 1 | +import { deepStrictEqual, ok, strictEqual } from 'node:assert'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { collectAsyncGenerator, createCache } from '../caching.mjs'; |
| 5 | + |
| 6 | +describe('caching', () => { |
| 7 | + describe('collectAsyncGenerator', () => { |
| 8 | + it('should collect all chunks into a flat array', async () => { |
| 9 | + async function* gen() { |
| 10 | + yield [1, 2]; |
| 11 | + yield [3, 4]; |
| 12 | + yield [5]; |
| 13 | + } |
| 14 | + |
| 15 | + const result = await collectAsyncGenerator(gen()); |
| 16 | + |
| 17 | + deepStrictEqual(result, [1, 2, 3, 4, 5]); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should return empty array for empty generator', async () => { |
| 21 | + async function* gen() { |
| 22 | + // empty generator |
| 23 | + } |
| 24 | + |
| 25 | + const result = await collectAsyncGenerator(gen()); |
| 26 | + |
| 27 | + deepStrictEqual(result, []); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should handle empty chunks', async () => { |
| 31 | + async function* gen() { |
| 32 | + yield []; |
| 33 | + yield [1]; |
| 34 | + yield []; |
| 35 | + yield [2, 3]; |
| 36 | + } |
| 37 | + |
| 38 | + const result = await collectAsyncGenerator(gen()); |
| 39 | + |
| 40 | + deepStrictEqual(result, [1, 2, 3]); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('createCache', () => { |
| 45 | + it('should expose the expected methods', () => { |
| 46 | + const cache = createCache(); |
| 47 | + |
| 48 | + ok(cache); |
| 49 | + strictEqual(typeof cache.has, 'function'); |
| 50 | + strictEqual(typeof cache.store, 'function'); |
| 51 | + strictEqual(typeof cache.expectConsumers, 'function'); |
| 52 | + strictEqual(typeof cache.consume, 'function'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should report whether a generator is stored', () => { |
| 56 | + const cache = createCache(); |
| 57 | + |
| 58 | + strictEqual(cache.has('a'), false); |
| 59 | + cache.store('a', Promise.resolve(1)); |
| 60 | + strictEqual(cache.has('a'), true); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should pass through non-streaming results', async () => { |
| 64 | + const cache = createCache(); |
| 65 | + |
| 66 | + cache.store('a', Promise.resolve({ value: 42 })); |
| 67 | + |
| 68 | + deepStrictEqual(await cache.consume('a'), { value: 42 }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should collect a streaming result only once for all consumers', async () => { |
| 72 | + const cache = createCache(); |
| 73 | + |
| 74 | + let iterations = 0; |
| 75 | + |
| 76 | + async function* gen() { |
| 77 | + iterations++; |
| 78 | + yield [1, 2]; |
| 79 | + yield [3]; |
| 80 | + } |
| 81 | + |
| 82 | + cache.store('a', gen()); |
| 83 | + cache.expectConsumers({ a: 2 }); |
| 84 | + |
| 85 | + const first = await cache.consume('a'); |
| 86 | + const second = await cache.consume('a'); |
| 87 | + |
| 88 | + deepStrictEqual(first, [1, 2, 3]); |
| 89 | + // The same collected array is shared, collection happened a single time |
| 90 | + strictEqual(first, second); |
| 91 | + strictEqual(iterations, 1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should evict a result once the final consumer has read it', async () => { |
| 95 | + const cache = createCache(); |
| 96 | + |
| 97 | + cache.store('a', Promise.resolve('value')); |
| 98 | + cache.expectConsumers({ a: 1 }); |
| 99 | + |
| 100 | + strictEqual(await cache.consume('a'), 'value'); |
| 101 | + // After the last consumer, the entry is dropped so it can be GC'd |
| 102 | + strictEqual(cache.has('a'), false); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should keep a result until all expected consumers have read it', async () => { |
| 106 | + const cache = createCache(); |
| 107 | + |
| 108 | + cache.store('a', Promise.resolve('value')); |
| 109 | + cache.expectConsumers({ a: 2 }); |
| 110 | + |
| 111 | + await cache.consume('a'); |
| 112 | + strictEqual(cache.has('a'), true); |
| 113 | + |
| 114 | + await cache.consume('a'); |
| 115 | + strictEqual(cache.has('a'), false); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should count consumers across a dependency closure', async () => { |
| 119 | + const cache = createCache(); |
| 120 | + |
| 121 | + // graph: a -> b -> c and d -> b; requested targets are a and d |
| 122 | + const graph = { a: 'b', b: 'c', d: 'b' }; |
| 123 | + |
| 124 | + cache.populateConsumerCounts(['a', 'd'], name => graph[name]); |
| 125 | + |
| 126 | + // `b` is depended on by both `a` and `d`, so it needs two reads before |
| 127 | + // it is evicted. |
| 128 | + cache.store('b', Promise.resolve('B')); |
| 129 | + await cache.consume('b'); |
| 130 | + strictEqual(cache.has('b'), true); |
| 131 | + await cache.consume('b'); |
| 132 | + strictEqual(cache.has('b'), false); |
| 133 | + |
| 134 | + // `c` is depended on only by `b`, so a single read evicts it. |
| 135 | + cache.store('c', Promise.resolve('C')); |
| 136 | + await cache.consume('c'); |
| 137 | + strictEqual(cache.has('c'), false); |
| 138 | + |
| 139 | + // `a` is a requested target, consumed exactly once by the final read. |
| 140 | + cache.store('a', Promise.resolve('A')); |
| 141 | + await cache.consume('a'); |
| 142 | + strictEqual(cache.has('a'), false); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments