|
| 1 | +/** |
| 2 | + * 同一模型 AI 调用并发限制单元测试 |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ config: { aiConcurrencyLimit: 2 } as { aiConcurrencyLimit?: number } })); |
| 7 | +vi.mock('@/lib/config', () => ({ getAppConfig: () => mocks.config })); |
| 8 | + |
| 9 | +import { getModelConcurrencyState, limitModelConcurrency, runWithModelConcurrency } from '@/lib/ai/concurrency'; |
| 10 | + |
| 11 | +/** 返回一个可手动完成的任务 */ |
| 12 | +function deferred() { |
| 13 | + let resolve!: () => void; |
| 14 | + const promise = new Promise<void>((r) => { resolve = r; }); |
| 15 | + return { promise, resolve }; |
| 16 | +} |
| 17 | + |
| 18 | +const flush = () => new Promise((r) => setTimeout(r, 0)); |
| 19 | + |
| 20 | +describe('runWithModelConcurrency', () => { |
| 21 | + beforeEach(() => { |
| 22 | + mocks.config = { aiConcurrencyLimit: 2 }; |
| 23 | + }); |
| 24 | + |
| 25 | + it('超过上限的调用排队,名额释放后按顺序执行', async () => { |
| 26 | + const tasks = [deferred(), deferred(), deferred()]; |
| 27 | + const started: number[] = []; |
| 28 | + const runs = tasks.map((task, index) => runWithModelConcurrency('m1', async () => { |
| 29 | + started.push(index); |
| 30 | + await task.promise; |
| 31 | + return index; |
| 32 | + })); |
| 33 | + |
| 34 | + await flush(); |
| 35 | + expect(started).toEqual([0, 1]); |
| 36 | + expect(getModelConcurrencyState('m1')).toEqual({ active: 2, waiting: 1 }); |
| 37 | + |
| 38 | + tasks[0].resolve(); |
| 39 | + await flush(); |
| 40 | + expect(started).toEqual([0, 1, 2]); |
| 41 | + |
| 42 | + tasks[1].resolve(); |
| 43 | + tasks[2].resolve(); |
| 44 | + expect(await Promise.all(runs)).toEqual([0, 1, 2]); |
| 45 | + expect(getModelConcurrencyState('m1')).toEqual({ active: 0, waiting: 0 }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('不同模型各自计数', async () => { |
| 49 | + const block = deferred(); |
| 50 | + const a = [runWithModelConcurrency('a', () => block.promise), runWithModelConcurrency('a', () => block.promise)]; |
| 51 | + const started = vi.fn(); |
| 52 | + const b = runWithModelConcurrency('b', async () => started()); |
| 53 | + |
| 54 | + await b; |
| 55 | + expect(started).toHaveBeenCalled(); |
| 56 | + block.resolve(); |
| 57 | + await Promise.all(a); |
| 58 | + }); |
| 59 | + |
| 60 | + it('调用失败也会释放名额', async () => { |
| 61 | + mocks.config = { aiConcurrencyLimit: 1 }; |
| 62 | + await expect(runWithModelConcurrency('err', async () => { throw new Error('429'); })).rejects.toThrow('429'); |
| 63 | + expect(await runWithModelConcurrency('err', async () => 'ok')).toBe('ok'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('调高上限后排队的调用随下一次释放一并放行', async () => { |
| 67 | + mocks.config = { aiConcurrencyLimit: 1 }; |
| 68 | + const tasks = [deferred(), deferred(), deferred()]; |
| 69 | + const started: number[] = []; |
| 70 | + const runs = tasks.map((task, index) => runWithModelConcurrency('raise', async () => { |
| 71 | + started.push(index); |
| 72 | + await task.promise; |
| 73 | + })); |
| 74 | + await flush(); |
| 75 | + expect(started).toEqual([0]); |
| 76 | + |
| 77 | + mocks.config = { aiConcurrencyLimit: 3 }; |
| 78 | + tasks[0].resolve(); |
| 79 | + await flush(); |
| 80 | + expect(started).toEqual([0, 1, 2]); |
| 81 | + tasks[1].resolve(); |
| 82 | + tasks[2].resolve(); |
| 83 | + await Promise.all(runs); |
| 84 | + }); |
| 85 | + |
| 86 | + it('配置无效时使用默认上限 5', async () => { |
| 87 | + mocks.config = { aiConcurrencyLimit: 0 }; |
| 88 | + const block = deferred(); |
| 89 | + const started: number[] = []; |
| 90 | + const runs = Array.from({ length: 6 }, (_, index) => runWithModelConcurrency('default', async () => { |
| 91 | + started.push(index); |
| 92 | + await block.promise; |
| 93 | + })); |
| 94 | + await flush(); |
| 95 | + expect(started).toHaveLength(5); |
| 96 | + block.resolve(); |
| 97 | + await Promise.all(runs); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('limitModelConcurrency', () => { |
| 102 | + it('包装后的方法受并发限制,内部互相调用不重复排队', async () => { |
| 103 | + mocks.config = { aiConcurrencyLimit: 1 }; |
| 104 | + const service = { |
| 105 | + async outer() { return this.inner(); }, |
| 106 | + async inner() { return 'done'; }, |
| 107 | + }; |
| 108 | + const limited = limitModelConcurrency(service, 'svc'); |
| 109 | + |
| 110 | + expect(await limited.outer()).toBe('done'); |
| 111 | + expect(getModelConcurrencyState('svc')).toEqual({ active: 0, waiting: 0 }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments