|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { Prisma } from "../generated/prisma"; |
| 3 | +import { type RetryBudget } from "./transaction"; |
| 4 | +import { withInfraRetry } from "./infraRetry"; |
| 5 | + |
| 6 | +const options = { enabled: true, maxAttempts: 4, backoffMinMs: 0, backoffMaxMs: 0 }; |
| 7 | +const noSleep = () => Promise.resolve(); |
| 8 | +const retryable = () => true; |
| 9 | + |
| 10 | +// A thunk that throws `error` for its first `failTimes` calls, then resolves. |
| 11 | +function makeThunk(failTimes: number, error: unknown = new Error("infra"), value = "ok") { |
| 12 | + let calls = 0; |
| 13 | + return { |
| 14 | + run: async () => { |
| 15 | + calls++; |
| 16 | + if (calls <= failTimes) throw error; |
| 17 | + return value; |
| 18 | + }, |
| 19 | + calls: () => calls, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +describe("withInfraRetry", () => { |
| 24 | + it("runs the thunk exactly once when disabled", async () => { |
| 25 | + const t = makeThunk(Infinity); |
| 26 | + await expect( |
| 27 | + withInfraRetry(t.run, { options: { ...options, enabled: false }, isRetryable: retryable }) |
| 28 | + ).rejects.toThrow("infra"); |
| 29 | + expect(t.calls()).toBe(1); |
| 30 | + }); |
| 31 | + |
| 32 | + it("retries a retryable error until it succeeds", async () => { |
| 33 | + const t = makeThunk(2); |
| 34 | + const result = await withInfraRetry(t.run, { options, isRetryable: retryable, sleep: noSleep }); |
| 35 | + expect(result).toBe("ok"); |
| 36 | + expect(t.calls()).toBe(3); |
| 37 | + }); |
| 38 | + |
| 39 | + it("does not retry a non-retryable error", async () => { |
| 40 | + const t = makeThunk(Infinity, new Error("query bug")); |
| 41 | + await expect( |
| 42 | + withInfraRetry(t.run, { options, isRetryable: () => false, sleep: noSleep }) |
| 43 | + ).rejects.toThrow("query bug"); |
| 44 | + expect(t.calls()).toBe(1); |
| 45 | + }); |
| 46 | + |
| 47 | + it("gives up after maxAttempts", async () => { |
| 48 | + const t = makeThunk(Infinity); |
| 49 | + await expect( |
| 50 | + withInfraRetry(t.run, { options, isRetryable: retryable, sleep: noSleep }) |
| 51 | + ).rejects.toThrow("infra"); |
| 52 | + expect(t.calls()).toBe(4); |
| 53 | + }); |
| 54 | + |
| 55 | + it("stops retrying once the shared budget is exhausted", async () => { |
| 56 | + const t = makeThunk(Infinity); |
| 57 | + let tokens = 1; |
| 58 | + const budget: RetryBudget = { |
| 59 | + tryConsume: () => { |
| 60 | + if (tokens > 0) { |
| 61 | + tokens--; |
| 62 | + return true; |
| 63 | + } |
| 64 | + return false; |
| 65 | + }, |
| 66 | + }; |
| 67 | + await expect( |
| 68 | + withInfraRetry(t.run, { options, isRetryable: retryable, budget, sleep: noSleep }) |
| 69 | + ).rejects.toThrow("infra"); |
| 70 | + // first attempt + one budgeted retry, then the budget denies the next |
| 71 | + expect(t.calls()).toBe(2); |
| 72 | + }); |
| 73 | + |
| 74 | + it("runs the thunk exactly once when maxAttempts <= 1", async () => { |
| 75 | + const t = makeThunk(Infinity); |
| 76 | + await expect( |
| 77 | + withInfraRetry(t.run, { |
| 78 | + options: { ...options, maxAttempts: 1 }, |
| 79 | + isRetryable: retryable, |
| 80 | + sleep: noSleep, |
| 81 | + }) |
| 82 | + ).rejects.toThrow("infra"); |
| 83 | + expect(t.calls()).toBe(1); |
| 84 | + }); |
| 85 | + |
| 86 | + it("defaults to isInfrastructureError when no isRetryable is provided", async () => { |
| 87 | + const infra = new Prisma.PrismaClientKnownRequestError("boom", { |
| 88 | + code: "P1001", |
| 89 | + clientVersion: "6.14.0", |
| 90 | + }); |
| 91 | + const t = makeThunk(1, infra); |
| 92 | + // no isRetryable in the config — the module must default to the classifier |
| 93 | + const result = await withInfraRetry(t.run, { options, sleep: noSleep }); |
| 94 | + expect(result).toBe("ok"); |
| 95 | + expect(t.calls()).toBe(2); |
| 96 | + }); |
| 97 | + |
| 98 | + it("with the default classifier, does not retry a real query error", async () => { |
| 99 | + const queryError = new Prisma.PrismaClientKnownRequestError("not found", { |
| 100 | + code: "P2025", |
| 101 | + clientVersion: "6.14.0", |
| 102 | + }); |
| 103 | + const t = makeThunk(Infinity, queryError); |
| 104 | + await expect(withInfraRetry(t.run, { options, sleep: noSleep })).rejects.toBe(queryError); |
| 105 | + expect(t.calls()).toBe(1); |
| 106 | + }); |
| 107 | + |
| 108 | + it("clamps a swapped min/max backoff", async () => { |
| 109 | + const t = makeThunk(1); |
| 110 | + const delays: number[] = []; |
| 111 | + await withInfraRetry(t.run, { |
| 112 | + options: { enabled: true, maxAttempts: 2, backoffMinMs: 300, backoffMaxMs: 100 }, |
| 113 | + isRetryable: retryable, |
| 114 | + sleep: noSleep, |
| 115 | + random: () => 0.9, |
| 116 | + onRetry: ({ delayMs }) => delays.push(delayMs), |
| 117 | + }); |
| 118 | + // min > max collapses to [100, 100], so the delay is 100 regardless of random |
| 119 | + expect(delays).toEqual([100]); |
| 120 | + }); |
| 121 | + |
| 122 | + it("computes a jittered delay within bounds and reports it", async () => { |
| 123 | + const t = makeThunk(1); |
| 124 | + const delays: number[] = []; |
| 125 | + await withInfraRetry(t.run, { |
| 126 | + options: { enabled: true, maxAttempts: 3, backoffMinMs: 100, backoffMaxMs: 300 }, |
| 127 | + isRetryable: retryable, |
| 128 | + sleep: noSleep, |
| 129 | + random: () => 0.5, |
| 130 | + onRetry: ({ delayMs }) => delays.push(delayMs), |
| 131 | + }); |
| 132 | + expect(delays[0]).toBe(200); |
| 133 | + }); |
| 134 | +}); |
0 commit comments