|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import * as rdf from 'rdflib' |
| 3 | +import { shareConcurrentLoads } from '../src/util/shareConcurrentLoads' |
| 4 | + |
| 5 | +function deferred () { |
| 6 | + let resolve!: (value: unknown) => void |
| 7 | + let reject!: (error: unknown) => void |
| 8 | + const promise = new Promise((res, rej) => { resolve = res; reject = rej }) |
| 9 | + return { promise, resolve, reject } |
| 10 | +} |
| 11 | + |
| 12 | +function fetcherWith (load: (...args: unknown[]) => Promise<unknown>) { |
| 13 | + const fetcher = { load } as unknown as rdf.Fetcher |
| 14 | + shareConcurrentLoads(fetcher) |
| 15 | + return fetcher |
| 16 | +} |
| 17 | + |
| 18 | +describe('shareConcurrentLoads', () => { |
| 19 | + const doc = rdf.sym('https://alice.example/profile/card') |
| 20 | + |
| 21 | + it('shares one request between concurrent plain loads of the same document', async () => { |
| 22 | + const response = deferred() |
| 23 | + const load = vi.fn(() => response.promise) |
| 24 | + const fetcher = fetcherWith(load) |
| 25 | + |
| 26 | + const first = fetcher.load(doc) |
| 27 | + const second = fetcher.load(rdf.sym('https://alice.example/profile/card#me').doc()) |
| 28 | + const third = fetcher.load('https://alice.example/profile/card#me', {}) |
| 29 | + response.resolve('ok') |
| 30 | + |
| 31 | + await expect(Promise.all([first, second, third])).resolves.toEqual(['ok', 'ok', 'ok']) |
| 32 | + expect(load).toHaveBeenCalledTimes(1) |
| 33 | + }) |
| 34 | + |
| 35 | + it('loads again once the shared request has settled', async () => { |
| 36 | + const load = vi.fn(async () => 'ok') |
| 37 | + const fetcher = fetcherWith(load) |
| 38 | + |
| 39 | + await fetcher.load(doc) |
| 40 | + await fetcher.load(doc) |
| 41 | + |
| 42 | + expect(load).toHaveBeenCalledTimes(2) |
| 43 | + }) |
| 44 | + |
| 45 | + it('lets a failed load be retried', async () => { |
| 46 | + const load = vi.fn() |
| 47 | + .mockRejectedValueOnce(new Error('offline')) |
| 48 | + .mockResolvedValueOnce('ok') |
| 49 | + const fetcher = fetcherWith(load) |
| 50 | + |
| 51 | + await expect(fetcher.load(doc)).rejects.toThrow('offline') |
| 52 | + await expect(fetcher.load(doc)).resolves.toBe('ok') |
| 53 | + }) |
| 54 | + |
| 55 | + it('does not share forced loads, loads with options, or batches', async () => { |
| 56 | + const response = deferred() |
| 57 | + const load = vi.fn(() => response.promise) |
| 58 | + const fetcher = fetcherWith(load) |
| 59 | + |
| 60 | + const plain = fetcher.load(doc) |
| 61 | + const forced = fetcher.load(doc, { force: true }) |
| 62 | + const withHeaders = fetcher.load(doc, { headers: { accept: 'text/turtle' } } as any) |
| 63 | + const batch = fetcher.load([doc]) |
| 64 | + response.resolve('ok') |
| 65 | + await Promise.all([plain, forced, withHeaders, batch]) |
| 66 | + |
| 67 | + expect(load).toHaveBeenCalledTimes(4) |
| 68 | + }) |
| 69 | + |
| 70 | + it('does not let a batch re-enter the sharing wrapper', async () => { |
| 71 | + const load = vi.fn(async (target: unknown) => { |
| 72 | + if (Array.isArray(target)) { |
| 73 | + return Promise.all(target.map(member => (fetcher.load as any)(member))) |
| 74 | + } |
| 75 | + return target |
| 76 | + }) |
| 77 | + const fetcher = fetcherWith(load) |
| 78 | + |
| 79 | + await expect(fetcher.load([doc, rdf.sym('https://bob.example/profile/card')])).resolves.toHaveLength(2) |
| 80 | + |
| 81 | + expect(load).toHaveBeenCalledTimes(2) |
| 82 | + expect(load.mock.calls.every(([target]) => !Array.isArray(target))).toBe(true) |
| 83 | + }) |
| 84 | + |
| 85 | + it('does not share a pending request across an identity transition', async () => { |
| 86 | + const response = deferred() |
| 87 | + const load = vi.fn(() => response.promise) |
| 88 | + const fetcher = { load } as unknown as rdf.Fetcher |
| 89 | + const invalidate = shareConcurrentLoads(fetcher) |
| 90 | + |
| 91 | + const beforeTransition = fetcher.load(doc) |
| 92 | + invalidate() |
| 93 | + const afterTransition = fetcher.load(doc) |
| 94 | + response.resolve('old identity') |
| 95 | + |
| 96 | + expect(await beforeTransition).toBe('old identity') |
| 97 | + expect(load).toHaveBeenCalledTimes(2) |
| 98 | + expect(await afterTransition).toBe('old identity') |
| 99 | + }) |
| 100 | + |
| 101 | + it('does not share loads of different documents', async () => { |
| 102 | + const load = vi.fn(async () => 'ok') |
| 103 | + const fetcher = fetcherWith(load) |
| 104 | + |
| 105 | + await Promise.all([fetcher.load(doc), fetcher.load(rdf.sym('https://bob.example/profile/card'))]) |
| 106 | + |
| 107 | + expect(load).toHaveBeenCalledTimes(2) |
| 108 | + }) |
| 109 | +}) |
0 commit comments