|
1 | | -import { deepStrictEqual, strictEqual, throws } from 'node:assert'; |
| 1 | +import { deepStrictEqual, rejects, strictEqual, throws } from 'node:assert'; |
2 | 2 | import { describe, it } from 'node:test'; |
3 | 3 |
|
4 | 4 | import { collectAsyncGenerator, createCache } from '../caching.mjs'; |
@@ -212,6 +212,61 @@ describe('caching', () => { |
212 | 212 | deepStrictEqual(await Array.fromAsync(stream), [[1], [2], [3]]); |
213 | 213 | }); |
214 | 214 |
|
| 215 | + it('should drain a result, alongside its other readers', async () => { |
| 216 | + const cache = createCache(); |
| 217 | + const produced = []; |
| 218 | + |
| 219 | + cache.populateConsumerCounts(['a', 'b'], name => |
| 220 | + name === 'b' ? 'a' : undefined |
| 221 | + ); |
| 222 | + |
| 223 | + async function* gen() { |
| 224 | + for (const value of [1, 2, 3]) { |
| 225 | + produced.push(value); |
| 226 | + |
| 227 | + yield [value]; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + cache.store('a', gen()); |
| 232 | + |
| 233 | + const reader = await cache.stream('a'); |
| 234 | + |
| 235 | + await cache.drain('a'); |
| 236 | + |
| 237 | + strictEqual(cache.has('a'), false); |
| 238 | + deepStrictEqual(produced, [1, 2, 3]); |
| 239 | + |
| 240 | + deepStrictEqual(await Array.fromAsync(reader), [[1], [2], [3]]); |
| 241 | + }); |
| 242 | + |
| 243 | + it('should let go of the chunks of a drained result as they are produced', async () => { |
| 244 | + const cache = createCache(); |
| 245 | + const gate = Promise.withResolvers(); |
| 246 | + const { released, chunkOf } = trackReleases(); |
| 247 | + |
| 248 | + cache.populateConsumerCounts(['a'], () => undefined); |
| 249 | + |
| 250 | + async function* gen() { |
| 251 | + yield chunkOf(1); |
| 252 | + yield chunkOf(2); |
| 253 | + yield chunkOf(3); |
| 254 | + await gate.promise; |
| 255 | + } |
| 256 | + |
| 257 | + cache.store('a', gen()); |
| 258 | + |
| 259 | + const draining = cache.drain('a'); |
| 260 | + |
| 261 | + // The generator waits, past its chunks: the first is no longer held |
| 262 | + await collectGarbage(() => released.has(1)); |
| 263 | + |
| 264 | + strictEqual(released.has(1), true); |
| 265 | + |
| 266 | + gate.resolve(); |
| 267 | + await draining; |
| 268 | + }); |
| 269 | + |
215 | 270 | it('should let go of a chunk once every streaming reader has read it, while it still runs', async () => { |
216 | 271 | const cache = createCache(); |
217 | 272 | const gate = Promise.withResolvers(); |
@@ -252,5 +307,33 @@ describe('caching', () => { |
252 | 307 | await readValues(first); |
253 | 308 | await readValues(second); |
254 | 309 | }); |
| 310 | + |
| 311 | + it('should drain a non-streaming result', async () => { |
| 312 | + const cache = createCache(); |
| 313 | + |
| 314 | + cache.populateConsumerCounts(['a'], () => undefined); |
| 315 | + cache.store('a', Promise.resolve({ value: 1 })); |
| 316 | + |
| 317 | + const drained = await cache.drain('a'); |
| 318 | + |
| 319 | + strictEqual(drained, undefined); |
| 320 | + strictEqual(cache.has('a'), false); |
| 321 | + }); |
| 322 | + |
| 323 | + it('should reject draining a generator that fails', async () => { |
| 324 | + const cache = createCache(); |
| 325 | + |
| 326 | + cache.populateConsumerCounts(['a'], () => undefined); |
| 327 | + |
| 328 | + async function* gen() { |
| 329 | + yield [1]; |
| 330 | + |
| 331 | + throw new Error('drained failure'); |
| 332 | + } |
| 333 | + |
| 334 | + cache.store('a', gen()); |
| 335 | + |
| 336 | + await rejects(cache.drain('a'), /drained failure/); |
| 337 | + }); |
255 | 338 | }); |
256 | 339 | }); |
0 commit comments