fix: 限定捞瓶查询与重试成本

This commit is contained in:
root
2026-09-15 21:13:28 +08:00
parent 0c5cbd75d9
commit c94d7b1af0
7 changed files with 425 additions and 49 deletions
+53 -1
View File
@@ -285,7 +285,9 @@ describe("match API with real PostgreSQL", () => {
const results = await Promise.all(
Array.from({ length: 8 }, () => pick(picker.authorization, key)),
);
expect(results.every((x) => x.status === 201)).toBe(true);
expect(results.map((x) => [x.status, x.body.code])).toEqual(
Array.from({ length: 8 }, () => [201, "OK"]),
);
expect(new Set(results.map((x) => x.body.data.lease.id))).toHaveLength(1);
expect(new Set(results.map((x) => x.body.data.lease.token))).toHaveLength(
1,
@@ -451,6 +453,56 @@ describe("match API with real PostgreSQL", () => {
await pick(picker.authorization).expect(201);
}, 15_000);
it("sustains twenty rounds of 21 concurrent distinct requests without picker-level serialization", async () => {
for (let round = 0; round < 20; round += 1) {
await prisma.$executeRawUnsafe(`TRUNCATE TABLE "accounts" CASCADE`);
const author = await actor(`stress-author-${round}`);
const picker = await actor(`stress-picker-${round}`);
await prisma.bottle.createMany({
data: Array.from({ length: 96 }, (_, index) => ({
authorId: author.id,
clientRequestId: `stress-${round}-${index}`,
contentText: `stress-${round}-${index}`,
reviewStatus: "APPROVED" as const,
poolStatus: "IN_POOL" as const,
approvedAt: new Date(),
})),
});
const results = await Promise.all(
Array.from({ length: 21 }, (_, index) =>
pick(picker.authorization, `stress-key-${round}-${index}`),
),
);
expect(results.filter(({ status }) => status === 201)).toHaveLength(20);
expect(
results.filter(
({ status, body }) =>
status === 429 && body.code === "BOTTLE_DAILY_LIMIT",
),
).toHaveLength(1);
expect(results.filter(({ status }) => status >= 500)).toHaveLength(0);
expect(
await prisma.dailyUsage.findFirstOrThrow({
where: { accountId: picker.id },
}),
).toMatchObject({ bottlesPicked: 20 });
expect(
await prisma.bottlePickLease.count({ where: { pickerId: picker.id } }),
).toBe(20);
expect(
await prisma.bottlePickHistory.count({
where: { pickerId: picker.id },
}),
).toBe(20);
expect(
await prisma.bottlePickRequest.count({
where: { pickerId: picker.id },
}),
).toBe(20);
}
}, 120_000);
it.each(["SUSPENSION", "BAN"] as const)(
"rejects active %s sanction",
async (type) => {