|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { createBatchRunner } from '../../src/utils/batch' |
| 3 | + |
| 4 | +describe('createBatchRunner', () => { |
| 5 | + beforeEach(() => { |
| 6 | + vi.clearAllMocks() |
| 7 | + }) |
| 8 | + |
| 9 | + it('should batch different tasks in the same tick', async () => { |
| 10 | + const runBatch = vi.fn(async (tasks: string[]) => { |
| 11 | + const values = new Map<string, string>() |
| 12 | + tasks.forEach((task) => { |
| 13 | + values.set(task, task.toUpperCase()) |
| 14 | + }) |
| 15 | + return { values, errors: new Map<string, unknown>() } |
| 16 | + }) |
| 17 | + |
| 18 | + const run = createBatchRunner<string, string>({ |
| 19 | + runBatch, |
| 20 | + }) |
| 21 | + |
| 22 | + const [a, b] = await Promise.all([ |
| 23 | + run('a'), |
| 24 | + run('b'), |
| 25 | + ]) |
| 26 | + |
| 27 | + expect(a).toBe('A') |
| 28 | + expect(b).toBe('B') |
| 29 | + expect(runBatch).toHaveBeenCalledTimes(1) |
| 30 | + expect(runBatch).toHaveBeenCalledWith(['a', 'b']) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should deduplicate same-key requests in one batch', async () => { |
| 34 | + const runBatch = vi.fn(async (tasks: string[]) => { |
| 35 | + const values = new Map<string, string>() |
| 36 | + tasks.forEach((task) => { |
| 37 | + values.set(task, `${task}-resolved`) |
| 38 | + }) |
| 39 | + return { values, errors: new Map<string, unknown>() } |
| 40 | + }) |
| 41 | + |
| 42 | + const run = createBatchRunner<string, string>({ |
| 43 | + runBatch, |
| 44 | + }) |
| 45 | + |
| 46 | + const [first, second] = await Promise.all([ |
| 47 | + run('pkg'), |
| 48 | + run('pkg'), |
| 49 | + ]) |
| 50 | + |
| 51 | + expect(first).toBe('pkg-resolved') |
| 52 | + expect(second).toBe('pkg-resolved') |
| 53 | + expect(runBatch).toHaveBeenCalledTimes(1) |
| 54 | + expect(runBatch).toHaveBeenCalledWith(['pkg']) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should reject all tasks when runBatch throws', async () => { |
| 58 | + const runBatch = vi.fn(async () => { |
| 59 | + throw new Error('batch failed') |
| 60 | + }) |
| 61 | + |
| 62 | + const run = createBatchRunner<string, string>({ |
| 63 | + runBatch, |
| 64 | + }) |
| 65 | + |
| 66 | + const [a, b] = await Promise.allSettled([ |
| 67 | + run('a'), |
| 68 | + run('b'), |
| 69 | + ]) |
| 70 | + |
| 71 | + expect(a.status).toBe('rejected') |
| 72 | + expect(b.status).toBe('rejected') |
| 73 | + expect(runBatch).toHaveBeenCalledTimes(1) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should flush immediately when maxSize is reached and split into multiple batches', async () => { |
| 77 | + const runBatch = vi.fn(async (tasks: string[]) => { |
| 78 | + const values = new Map<string, string>() |
| 79 | + tasks.forEach((task) => { |
| 80 | + values.set(task, task.toUpperCase()) |
| 81 | + }) |
| 82 | + return { values, errors: new Map<string, unknown>() } |
| 83 | + }) |
| 84 | + |
| 85 | + const run = createBatchRunner<string, string>({ |
| 86 | + maxSize: 2, |
| 87 | + runBatch, |
| 88 | + }) |
| 89 | + |
| 90 | + const a = run('a') |
| 91 | + expect(runBatch).not.toHaveBeenCalled() |
| 92 | + |
| 93 | + const [, b, c] = await Promise.all([ |
| 94 | + a, |
| 95 | + run('b'), |
| 96 | + run('c'), |
| 97 | + ]) |
| 98 | + |
| 99 | + expect(await a).toBe('A') |
| 100 | + expect(b).toBe('B') |
| 101 | + expect(c).toBe('C') |
| 102 | + expect(runBatch).toHaveBeenCalledTimes(2) |
| 103 | + expect(runBatch).toHaveBeenNthCalledWith(1, ['a', 'b']) |
| 104 | + expect(runBatch).toHaveBeenNthCalledWith(2, ['c']) |
| 105 | + }) |
| 106 | + |
| 107 | + it('should reject errored or missing outcomes', async () => { |
| 108 | + const runBatch = vi.fn(async () => ({ |
| 109 | + values: new Map<string, string>([['a', 'A']]), |
| 110 | + errors: new Map<string, unknown>([['b', new Error('b failed')]]), |
| 111 | + })) |
| 112 | + |
| 113 | + const run = createBatchRunner<string, string>({ |
| 114 | + runBatch, |
| 115 | + }) |
| 116 | + |
| 117 | + const [a, b, c] = await Promise.allSettled([ |
| 118 | + run('a'), |
| 119 | + run('b'), |
| 120 | + run('c'), |
| 121 | + ]) |
| 122 | + |
| 123 | + expect(a).toEqual({ status: 'fulfilled', value: 'A' }) |
| 124 | + expect(b.status).toBe('rejected') |
| 125 | + expect(c.status).toBe('rejected') |
| 126 | + expect(runBatch).toHaveBeenCalledTimes(1) |
| 127 | + }) |
| 128 | +}) |
0 commit comments