|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { Semaphore, withTimeoutNotify } from "./concurrency-control"; |
| 3 | + |
| 4 | +describe("Semaphore", () => { |
| 5 | + it.concurrent("limit 小于 1 时抛出错误", () => { |
| 6 | + expect(() => new Semaphore(0)).toThrow("limit must be >= 1"); |
| 7 | + expect(() => new Semaphore(-1)).toThrow("limit must be >= 1"); |
| 8 | + }); |
| 9 | + |
| 10 | + it.concurrent("未达到限制时 acquire 立即返回", async () => { |
| 11 | + const sem = new Semaphore(2); |
| 12 | + await sem.acquire(); |
| 13 | + await sem.acquire(); |
| 14 | + // 两次 acquire 都不应阻塞 |
| 15 | + sem.release(); |
| 16 | + sem.release(); |
| 17 | + }); |
| 18 | + |
| 19 | + it.concurrent("达到限制时 acquire 阻塞,release 后恢复", async () => { |
| 20 | + const sem = new Semaphore(1); |
| 21 | + const order: number[] = []; |
| 22 | + |
| 23 | + await sem.acquire(); |
| 24 | + order.push(1); |
| 25 | + |
| 26 | + const blocked = sem.acquire().then(() => { |
| 27 | + order.push(3); |
| 28 | + }); |
| 29 | + |
| 30 | + // 等一个 microtask,确认 blocked 还没执行 |
| 31 | + await Promise.resolve(); |
| 32 | + order.push(2); |
| 33 | + |
| 34 | + sem.release(); |
| 35 | + await blocked; |
| 36 | + |
| 37 | + expect(order).toEqual([1, 2, 3]); |
| 38 | + sem.release(); |
| 39 | + }); |
| 40 | + |
| 41 | + it.concurrent("并发数不超过限制", async () => { |
| 42 | + const limit = 3; |
| 43 | + const sem = new Semaphore(limit); |
| 44 | + let concurrent = 0; |
| 45 | + let maxConcurrent = 0; |
| 46 | + |
| 47 | + const task = async () => { |
| 48 | + await sem.acquire(); |
| 49 | + concurrent++; |
| 50 | + maxConcurrent = Math.max(maxConcurrent, concurrent); |
| 51 | + // 模拟异步操作 |
| 52 | + await new Promise((r) => setTimeout(r, 10)); |
| 53 | + concurrent--; |
| 54 | + sem.release(); |
| 55 | + }; |
| 56 | + |
| 57 | + await Promise.all(Array.from({ length: 10 }, () => task())); |
| 58 | + |
| 59 | + expect(maxConcurrent).toBe(limit); |
| 60 | + }); |
| 61 | + |
| 62 | + it.concurrent("按 FIFO 顺序唤醒等待者", async () => { |
| 63 | + const sem = new Semaphore(1); |
| 64 | + const order: number[] = []; |
| 65 | + |
| 66 | + await sem.acquire(); |
| 67 | + |
| 68 | + const p1 = sem.acquire().then(() => { |
| 69 | + order.push(1); |
| 70 | + sem.release(); |
| 71 | + }); |
| 72 | + const p2 = sem.acquire().then(() => { |
| 73 | + order.push(2); |
| 74 | + sem.release(); |
| 75 | + }); |
| 76 | + const p3 = sem.acquire().then(() => { |
| 77 | + order.push(3); |
| 78 | + sem.release(); |
| 79 | + }); |
| 80 | + |
| 81 | + sem.release(); |
| 82 | + await Promise.all([p1, p2, p3]); |
| 83 | + |
| 84 | + expect(order).toEqual([1, 2, 3]); |
| 85 | + }); |
| 86 | + |
| 87 | + it.concurrent("double release 输出警告", () => { |
| 88 | + const sem = new Semaphore(1); |
| 89 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 90 | + |
| 91 | + sem.release(); |
| 92 | + expect(warnSpy).toHaveBeenCalledWith("Semaphore double release detected"); |
| 93 | + |
| 94 | + warnSpy.mockRestore(); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe("withTimeoutNotify", () => { |
| 99 | + it.concurrent("promise 在超时前完成时,回调收到 done=true", async () => { |
| 100 | + const promise = Promise.resolve("ok"); |
| 101 | + const calls: Array<{ done: boolean; timeouted: boolean }> = []; |
| 102 | + |
| 103 | + const res = await withTimeoutNotify(promise, 1000, (r) => { |
| 104 | + calls.push({ done: r.done, timeouted: r.timeouted }); |
| 105 | + }); |
| 106 | + |
| 107 | + expect(res.result).toBe("ok"); |
| 108 | + expect(res.done).toBe(true); |
| 109 | + expect(res.timeouted).toBe(false); |
| 110 | + expect(res.err).toBeUndefined(); |
| 111 | + // 只调用一次(done),不触发 timeout |
| 112 | + expect(calls).toEqual([{ done: true, timeouted: false }]); |
| 113 | + }); |
| 114 | + |
| 115 | + it.concurrent("promise 在超时前失败时,回调收到 err", async () => { |
| 116 | + const error = new Error("fail"); |
| 117 | + const promise = Promise.reject(error); |
| 118 | + const calls: Array<{ done: boolean; err: Error | undefined }> = []; |
| 119 | + |
| 120 | + const res = await withTimeoutNotify(promise, 1000, (r) => { |
| 121 | + calls.push({ done: r.done, err: r.err }); |
| 122 | + }); |
| 123 | + |
| 124 | + expect(res.err).toBe(error); |
| 125 | + expect(res.done).toBe(true); |
| 126 | + expect(res.result).toBeUndefined(); |
| 127 | + expect(calls).toEqual([{ done: true, err: error }]); |
| 128 | + }); |
| 129 | + |
| 130 | + it.concurrent("超时后回调被调用,promise 完成后再次调用", async () => { |
| 131 | + vi.useFakeTimers(); |
| 132 | + let resolvePromise: (v: string) => void; |
| 133 | + const promise = new Promise<string>((r) => { |
| 134 | + resolvePromise = r; |
| 135 | + }); |
| 136 | + const calls: Array<{ done: boolean; timeouted: boolean }> = []; |
| 137 | + |
| 138 | + const resultPromise = withTimeoutNotify(promise, 100, (r) => { |
| 139 | + calls.push({ done: r.done, timeouted: r.timeouted }); |
| 140 | + }); |
| 141 | + |
| 142 | + // 触发超时 |
| 143 | + vi.advanceTimersByTime(100); |
| 144 | + expect(calls).toEqual([{ done: false, timeouted: true }]); |
| 145 | + |
| 146 | + // promise 完成 |
| 147 | + resolvePromise!("late"); |
| 148 | + const res = await resultPromise; |
| 149 | + |
| 150 | + expect(res.result).toBe("late"); |
| 151 | + expect(res.done).toBe(true); |
| 152 | + expect(res.timeouted).toBe(true); |
| 153 | + // 回调被调用两次:timeout + done |
| 154 | + expect(calls).toHaveLength(2); |
| 155 | + expect(calls[1]).toEqual({ done: true, timeouted: true }); |
| 156 | + |
| 157 | + vi.useRealTimers(); |
| 158 | + }); |
| 159 | + |
| 160 | + it.concurrent("超时后 promise 失败,回调也被调用两次", async () => { |
| 161 | + vi.useFakeTimers(); |
| 162 | + let rejectPromise: (e: Error) => void; |
| 163 | + const promise = new Promise<string>((_, reject) => { |
| 164 | + rejectPromise = reject; |
| 165 | + }); |
| 166 | + const calls: Array<{ timeouted: boolean; err: Error | undefined }> = []; |
| 167 | + |
| 168 | + const resultPromise = withTimeoutNotify(promise, 50, (r) => { |
| 169 | + calls.push({ timeouted: r.timeouted, err: r.err }); |
| 170 | + }); |
| 171 | + |
| 172 | + vi.advanceTimersByTime(50); |
| 173 | + expect(calls).toHaveLength(1); |
| 174 | + |
| 175 | + const error = new Error("network error"); |
| 176 | + rejectPromise!(error); |
| 177 | + const res = await resultPromise; |
| 178 | + |
| 179 | + expect(res.err).toBe(error); |
| 180 | + expect(calls).toHaveLength(2); |
| 181 | + expect(calls[1]).toEqual({ timeouted: true, err: error }); |
| 182 | + |
| 183 | + vi.useRealTimers(); |
| 184 | + }); |
| 185 | +}); |
0 commit comments