|
| 1 | +import { sleep } from "@App/pkg/utils/utils"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { |
| 4 | + getSessionRuleIds, |
| 5 | + LIMIT_SESSION_RULES, |
| 6 | + nextSessionRuleId, |
| 7 | + removeSessionRuleIdEntry, |
| 8 | +} from "./dnr_id_controller"; |
| 9 | + |
| 10 | +describe("getSessionRuleIds", () => { |
| 11 | + it("从现有 chrome session rules 初始化", async () => { |
| 12 | + const ids = await getSessionRuleIds(); |
| 13 | + expect(ids.size).lessThan(100); |
| 14 | + await nextSessionRuleId(); |
| 15 | + expect(ids.size).greaterThanOrEqual(1); |
| 16 | + await nextSessionRuleId(); |
| 17 | + expect(ids.size).greaterThanOrEqual(2); |
| 18 | + }); |
| 19 | +}); |
| 20 | + |
| 21 | +describe("nextSessionRuleId", () => { |
| 22 | + it("每次调用返回唯一递增的 id", async () => { |
| 23 | + const id1 = await nextSessionRuleId(); |
| 24 | + const id2 = await nextSessionRuleId(); |
| 25 | + const id3 = await nextSessionRuleId(); |
| 26 | + |
| 27 | + expect(id2).toBeGreaterThan(id1); |
| 28 | + expect(id3).toBeGreaterThan(id2); |
| 29 | + }); |
| 30 | + |
| 31 | + it("跳过已存在于 session rules 中的 id", async () => { |
| 32 | + const ids = await getSessionRuleIds(); |
| 33 | + const next = await nextSessionRuleId(); |
| 34 | + |
| 35 | + ids.add(next + 1); |
| 36 | + const skipped = await nextSessionRuleId(); |
| 37 | + |
| 38 | + expect(skipped).toBeGreaterThan(next + 1); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe("removeSessionRuleIdEntry", () => { |
| 43 | + it("从追踪集合中移除指定 id", async () => { |
| 44 | + const ids = await getSessionRuleIds(); |
| 45 | + const id = await nextSessionRuleId(); |
| 46 | + |
| 47 | + ids.add(id); |
| 48 | + removeSessionRuleIdEntry(id); |
| 49 | + |
| 50 | + expect(ids.has(id)).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it("sessionRuleIds 未初始化时为 no-op", () => { |
| 54 | + expect(() => removeSessionRuleIdEntry(10404)).not.toThrow(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("ruleId <= 10000 时抛错", () => { |
| 58 | + expect(() => removeSessionRuleIdEntry(10000)).toThrow(); |
| 59 | + expect(() => removeSessionRuleIdEntry(1)).toThrow(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("回退 SESSION_RULE_ID_BEGIN 使被移除的 id 下次被复用", async () => { |
| 63 | + const ids = await getSessionRuleIds(); |
| 64 | + |
| 65 | + const id1 = await nextSessionRuleId(); |
| 66 | + const id2 = await nextSessionRuleId(); |
| 67 | + const id3 = await nextSessionRuleId(); |
| 68 | + |
| 69 | + ids.add(id1); |
| 70 | + ids.add(id2); |
| 71 | + ids.add(id3); |
| 72 | + |
| 73 | + // 移除最新的 id,counter 回退并复用该 id |
| 74 | + removeSessionRuleIdEntry(id3); |
| 75 | + const reused = await nextSessionRuleId(); |
| 76 | + expect(reused).toBe(id3); |
| 77 | + }); |
| 78 | + |
| 79 | + it("移除的 id 在 counter 之前时仍会回退", async () => { |
| 80 | + const ids = await getSessionRuleIds(); |
| 81 | + |
| 82 | + const id1 = await nextSessionRuleId(); |
| 83 | + const id2 = await nextSessionRuleId(); |
| 84 | + const id3 = await nextSessionRuleId(); |
| 85 | + |
| 86 | + ids.add(id1); |
| 87 | + ids.add(id2); |
| 88 | + ids.add(id3); |
| 89 | + |
| 90 | + // 移除较早的 id,counter 回退到 id1 - 1,下次分配会得到 id1 |
| 91 | + removeSessionRuleIdEntry(id1); |
| 92 | + const reused = await nextSessionRuleId(); |
| 93 | + expect(reused).toBe(id1); |
| 94 | + }); |
| 95 | + |
| 96 | + it("移除 counter 之后的 id 不回退 counter", async () => { |
| 97 | + const id1 = await nextSessionRuleId(); |
| 98 | + const id2 = await nextSessionRuleId(); |
| 99 | + const id3 = await nextSessionRuleId(); |
| 100 | + const _id4 = await nextSessionRuleId(); |
| 101 | + const id5 = await nextSessionRuleId(); |
| 102 | + const id6 = await nextSessionRuleId(); |
| 103 | + |
| 104 | + removeSessionRuleIdEntry(id1); |
| 105 | + removeSessionRuleIdEntry(id5); |
| 106 | + removeSessionRuleIdEntry(id3); |
| 107 | + // removeSessionRuleIdEntry(id4); |
| 108 | + removeSessionRuleIdEntry(id2); |
| 109 | + const nextBefore = await nextSessionRuleId(); // e.g. 10001 |
| 110 | + removeSessionRuleIdEntry(id6); |
| 111 | + |
| 112 | + expect(await nextSessionRuleId()).toBe(nextBefore + 1); // counter 未变,正常递增 |
| 113 | + expect(await nextSessionRuleId()).toBe(nextBefore + 2); |
| 114 | + // expect(await nextSessionRuleId()).toBe(nextBefore + 3); |
| 115 | + expect(await nextSessionRuleId()).toBe(nextBefore + 4); |
| 116 | + expect(await nextSessionRuleId()).toBe(nextBefore + 5); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe("nextSessionRuleId limit control", () => { |
| 121 | + it("达到上限时锁定,移除条目后解锁", async () => { |
| 122 | + const ids = await getSessionRuleIds(); |
| 123 | + expect(ids.size).toBeLessThan(100); |
| 124 | + |
| 125 | + const added = []; |
| 126 | + for (let w = ids.size; w < LIMIT_SESSION_RULES; w++) { |
| 127 | + const j = await nextSessionRuleId(); |
| 128 | + added.push(j); |
| 129 | + ids.add(j); |
| 130 | + } |
| 131 | + expect(ids.size).toBeGreaterThan(1000); |
| 132 | + |
| 133 | + const lockedPromise = nextSessionRuleId(); |
| 134 | + |
| 135 | + const raceResult1 = await Promise.race([lockedPromise.then(() => "resolved"), sleep(5).then(() => "pending")]); |
| 136 | + expect(raceResult1).toBe("pending"); |
| 137 | + |
| 138 | + // 使用固定索引而非随机,保证测试可重复 |
| 139 | + const p1 = added[0]; |
| 140 | + const p2 = added[6]; |
| 141 | + removeSessionRuleIdEntry(p1); |
| 142 | + removeSessionRuleIdEntry(p2); |
| 143 | + |
| 144 | + const raceResult2 = await Promise.race([lockedPromise.then(() => "resolved"), sleep(5).then(() => "pending")]); |
| 145 | + expect(raceResult2).toBe("resolved"); |
| 146 | + |
| 147 | + const id1 = await lockedPromise; |
| 148 | + const id2 = await nextSessionRuleId(); |
| 149 | + expect(id1).toBe(p1); |
| 150 | + expect(id2).toBe(p2); |
| 151 | + |
| 152 | + for (const k of added) { |
| 153 | + removeSessionRuleIdEntry(k); |
| 154 | + } |
| 155 | + |
| 156 | + const res = await getSessionRuleIds(); |
| 157 | + expect(res).toBe(ids); |
| 158 | + expect(res.size).toBeLessThan(100); |
| 159 | + }); |
| 160 | + |
| 161 | + it("单次移除仅放行 1 个 waiter,其余继续等待", async () => { |
| 162 | + const ids = await getSessionRuleIds(); |
| 163 | + expect(ids.size).toBeLessThan(100); |
| 164 | + |
| 165 | + const added = []; |
| 166 | + for (let w = ids.size; w < LIMIT_SESSION_RULES; w++) { |
| 167 | + const j = await nextSessionRuleId(); |
| 168 | + added.push(j); |
| 169 | + ids.add(j); |
| 170 | + } |
| 171 | + expect(ids.size).toBeGreaterThan(1000); |
| 172 | + |
| 173 | + // 在已达上限时发起多个并发调用 |
| 174 | + const p1 = nextSessionRuleId(); |
| 175 | + const p2 = nextSessionRuleId(); |
| 176 | + const p3 = nextSessionRuleId(); |
| 177 | + |
| 178 | + const raceResult = await Promise.race([ |
| 179 | + Promise.all([p1, p2, p3]).then(() => "resolved"), |
| 180 | + sleep(5).then(() => "pending"), |
| 181 | + ]); |
| 182 | + expect(raceResult).toBe("pending"); |
| 183 | + |
| 184 | + // 单次释放 1 个 slot: 只应放行 1 个 waiter,剩余仍挂起 |
| 185 | + removeSessionRuleIdEntry(added[0]); |
| 186 | + const firstResolved = await Promise.race([p1.then(() => "resolved"), sleep(50).then(() => "pending")]); |
| 187 | + expect(firstResolved).toBe("resolved"); |
| 188 | + |
| 189 | + const remainingStillPending = await Promise.race([ |
| 190 | + Promise.all([p2, p3]).then(() => "resolved"), |
| 191 | + sleep(5).then(() => "pending"), |
| 192 | + ]); |
| 193 | + expect(remainingStillPending).toBe("pending"); |
| 194 | + |
| 195 | + // 继续释放 2 个 slot,剩余 waiter 才能继续完成 |
| 196 | + removeSessionRuleIdEntry(added[1]); |
| 197 | + removeSessionRuleIdEntry(added[2]); |
| 198 | + const results = await Promise.race([Promise.all([p2, p3]).then((vals) => vals), sleep(50).then(() => null)]); |
| 199 | + expect(results).not.toBeNull(); |
| 200 | + |
| 201 | + // 任何时候 size 都不应超过 LIMIT_SESSION_RULES |
| 202 | + expect(ids.size).toBeLessThanOrEqual(LIMIT_SESSION_RULES); |
| 203 | + |
| 204 | + for (const k of added) { |
| 205 | + if (ids.has(k)) removeSessionRuleIdEntry(k); |
| 206 | + } |
| 207 | + // p1/p2/p3 分配的 id 也要清理 |
| 208 | + const allocated = [await p1, ...(results as number[])]; |
| 209 | + for (const k of allocated) { |
| 210 | + if (ids.has(k)) removeSessionRuleIdEntry(k); |
| 211 | + } |
| 212 | + |
| 213 | + const res = await getSessionRuleIds(); |
| 214 | + expect(res).toBe(ids); |
| 215 | + expect(res.size).toBeLessThan(100); |
| 216 | + }); |
| 217 | +}); |
0 commit comments