|
1 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions */ |
2 | | -import { describe, it, expect, afterEach } from 'vitest'; |
3 | | -import { signal, watcher, withContexts } from 'signalium'; |
| 2 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 3 | +import { reactive, signal, watcher, withContexts } from 'signalium'; |
4 | 4 | import { MemoryPersistentStore, SyncQueryStore } from '../stores/sync.js'; |
5 | 5 | import { QueryClient, QueryClientContext, DEFAULT_PREFETCH_TTL } from '../QueryClient.js'; |
6 | 6 | import { RESTQuery, RESTQueryAdapter } from '../rest/index.js'; |
7 | 7 | import { t } from '../typeDefs.js'; |
8 | 8 | import { fetchQuery, QueryDefinition } from '../query.js'; |
9 | 9 | import type { MutationEvent } from '../types.js'; |
| 10 | +import { GcManager, type GcKeyType } from '../GcManager.js'; |
| 11 | +import { NetworkManager } from '../NetworkManager.js'; |
10 | 12 | import { createMockFetch, sleep } from './utils.js'; |
11 | 13 |
|
12 | 14 | /** |
@@ -266,21 +268,69 @@ describe('client.prefetch()', () => { |
266 | 268 | unsub(); |
267 | 269 | }); |
268 | 270 |
|
269 | | - it('a ttl that runs out mid-fetch waits for the fetch instead of aborting it', async () => { |
| 271 | + it('a ttl that runs out mid-fetch releases the lease, aborting the fetch with no reader', async () => { |
270 | 272 | const { client, mockFetch } = setup(); |
271 | 273 | mockFetch.reset(); |
272 | 274 | mockFetch.get('/streamed', { n: 9 }, { delay: 40 }); |
273 | 275 | client.prefetch(GetStreamed, undefined, { ttl: 10 }); |
274 | 276 | await sleep(25); |
275 | | - // Past the ttl, before the response (40 ms): still held, request not cancelled. |
276 | | - expect(unsubscribed).toBe(0); |
277 | | - expect(mockFetch.calls[0].options.signal?.aborted).toBe(false); |
| 277 | + // The ttl is an upper bound: past it, before the response, the lease is gone. |
| 278 | + expect(unsubscribed).toBe(1); |
| 279 | + expect(mockFetch.calls[0].options.signal?.aborted).toBe(true); |
| 280 | + await sleep(40); |
| 281 | + }); |
| 282 | + |
| 283 | + it('a ttl that runs out mid-fetch leaves a reader that joined the fetch running', async () => { |
| 284 | + const { client, mockFetch } = setup(); |
| 285 | + mockFetch.reset(); |
| 286 | + mockFetch.get('/streamed', { n: 9 }, { delay: 40 }); |
| 287 | + client.prefetch(GetStreamed, undefined, { ttl: 10 }); |
| 288 | + await sleep(5); |
| 289 | + const { w, unsub } = mountReader(client, () => fetchQuery(GetStreamed).value); |
278 | 290 | await sleep(60); |
279 | 291 | expect(mockFetch.calls).toHaveLength(1); |
280 | | - // The fetch landed (an aborted one applies nothing), then the lease went. |
281 | | - const relay = client.getQuery(QueryDefinition.for(GetStreamed), undefined); |
282 | | - expect(relay.value).toMatchObject({ n: 9 }); |
283 | | - expect(unsubscribed).toBe(1); |
| 292 | + expect(mockFetch.calls[0].options.signal?.aborted).toBe(false); |
| 293 | + expect(w.value).toMatchObject({ n: 9 }); |
| 294 | + unsub(); |
| 295 | + }); |
| 296 | + |
| 297 | + it('a ttl caps a lease whose fetch never settles', async () => { |
| 298 | + vi.useFakeTimers(); |
| 299 | + try { |
| 300 | + const networkManager = new NetworkManager(false); |
| 301 | + const client = new QueryClient({ |
| 302 | + store: new SyncQueryStore(new MemoryPersistentStore()), |
| 303 | + adapters: [new RESTQueryAdapter({ fetch: createMockFetch() as any, baseUrl: 'http://localhost' })], |
| 304 | + networkManager, |
| 305 | + }); |
| 306 | + clients.push(client); |
| 307 | + // Offline: the relay stays pending. |
| 308 | + client.prefetch(GetStreamed, undefined, { ttl: 1_000 }); |
| 309 | + await vi.advanceTimersByTimeAsync(10); |
| 310 | + expect(client.getQuery(QueryDefinition.for(GetStreamed), undefined).isPending).toBe(true); |
| 311 | + await vi.advanceTimersByTimeAsync(2_000); |
| 312 | + expect((client as unknown as { leases: Set<unknown> }).leases.size).toBe(0); |
| 313 | + client.destroy(); |
| 314 | + clients = []; |
| 315 | + await vi.advanceTimersByTimeAsync(1_000); |
| 316 | + } finally { |
| 317 | + vi.useRealTimers(); |
| 318 | + } |
| 319 | + }); |
| 320 | + |
| 321 | + it('destroy() clears the ttl timer', async () => { |
| 322 | + vi.useFakeTimers(); |
| 323 | + try { |
| 324 | + const { client } = setup(); |
| 325 | + client.prefetch(GetItem, undefined, { ttl: 60_000 }); |
| 326 | + await vi.advanceTimersByTimeAsync(20); |
| 327 | + client.destroy(); |
| 328 | + clients = []; |
| 329 | + await vi.advanceTimersByTimeAsync(50); |
| 330 | + expect(vi.getTimerCount()).toBe(0); |
| 331 | + } finally { |
| 332 | + vi.useRealTimers(); |
| 333 | + } |
284 | 334 | }); |
285 | 335 |
|
286 | 336 | it('defaults ttl to DEFAULT_PREFETCH_TTL', () => { |
@@ -441,6 +491,84 @@ describe('client.retain()', () => { |
441 | 491 | expect(unsubscribed).toBe(1); |
442 | 492 | }); |
443 | 493 |
|
| 494 | + it('destroy() leaves no GC timer behind for the queries it releases', async () => { |
| 495 | + const created: unknown[] = []; |
| 496 | + const cleared = new Set<unknown>(); |
| 497 | + const realSetInterval = globalThis.setInterval; |
| 498 | + const realClearInterval = globalThis.clearInterval; |
| 499 | + vi.spyOn(globalThis, 'setInterval').mockImplementation(((fn: () => void, ms?: number) => { |
| 500 | + const id = realSetInterval(fn, ms); |
| 501 | + created.push(id); |
| 502 | + return id; |
| 503 | + }) as typeof setInterval); |
| 504 | + vi.spyOn(globalThis, 'clearInterval').mockImplementation(((id: ReturnType<typeof setInterval>) => { |
| 505 | + cleared.add(id); |
| 506 | + realClearInterval(id); |
| 507 | + }) as typeof clearInterval); |
| 508 | + |
| 509 | + try { |
| 510 | + const { client } = setup(); |
| 511 | + // A real GcManager: the tests' window-less environment gets the no-op one. |
| 512 | + (client as unknown as { gcManager: GcManager }).gcManager = new GcManager( |
| 513 | + (client as unknown as { handleEviction: (key: number, type: GcKeyType) => void }).handleEviction, |
| 514 | + 1, |
| 515 | + ); |
| 516 | + client.retain(() => fetchQuery(GetItem)); |
| 517 | + await sleep(20); |
| 518 | + client.destroy(); |
| 519 | + clients = []; |
| 520 | + // The released query deactivates on Signalium's next flush, after destroy(). |
| 521 | + await sleep(50); |
| 522 | + const leaked = created.filter(id => !cleared.has(id)); |
| 523 | + for (const id of leaked) realClearInterval(id as ReturnType<typeof setInterval>); |
| 524 | + expect(leaked).toHaveLength(0); |
| 525 | + } finally { |
| 526 | + vi.restoreAllMocks(); |
| 527 | + } |
| 528 | + }); |
| 529 | + |
| 530 | + it('warns, and keeps one lease, when called from a reactive computation that reruns', async () => { |
| 531 | + const warn = vi.fn(); |
| 532 | + const mockFetch = createMockFetch(); |
| 533 | + mockFetch.get('/item', () => ({ n: 1 })); |
| 534 | + const client = new QueryClient({ |
| 535 | + store: new SyncQueryStore(new MemoryPersistentStore()), |
| 536 | + adapters: [new RESTQueryAdapter({ fetch: mockFetch as any, baseUrl: 'http://localhost' })], |
| 537 | + log: { warn }, |
| 538 | + }); |
| 539 | + clients.push(client); |
| 540 | + const leases = (client as unknown as { leases: Set<unknown> }).leases; |
| 541 | + |
| 542 | + const tick = signal(0); |
| 543 | + const computation = reactive(() => { |
| 544 | + const value = tick.value; |
| 545 | + client.retain(() => fetchQuery(GetItem)); |
| 546 | + return value; |
| 547 | + }); |
| 548 | + const { w, unsub } = mountReader(client, () => computation()); |
| 549 | + expect(warn).toHaveBeenCalledTimes(1); |
| 550 | + for (let i = 1; i <= 5; i++) { |
| 551 | + tick.value = i; |
| 552 | + w.value; |
| 553 | + await sleep(10); |
| 554 | + } |
| 555 | + expect(leases.size).toBe(1); |
| 556 | + expect(mockFetch.calls).toHaveLength(1); |
| 557 | + unsub(); |
| 558 | + }); |
| 559 | + |
| 560 | + it('does not warn when called outside a reactive computation', () => { |
| 561 | + const warn = vi.fn(); |
| 562 | + const client = new QueryClient({ |
| 563 | + store: new SyncQueryStore(new MemoryPersistentStore()), |
| 564 | + adapters: [new RESTQueryAdapter({ fetch: createMockFetch() as any, baseUrl: 'http://localhost' })], |
| 565 | + log: { warn }, |
| 566 | + }); |
| 567 | + clients.push(client); |
| 568 | + client.retain(() => fetchQuery(GetItem))(); |
| 569 | + expect(warn).not.toHaveBeenCalled(); |
| 570 | + }); |
| 571 | + |
444 | 572 | it('releases and rethrows when the callback throws', () => { |
445 | 573 | const { client } = setup(); |
446 | 574 | expect(() => |
|
0 commit comments