|
| 1 | +import { describe, expect, it, vi } 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 | +describe("withInfraRetry", () => { |
| 11 | + it("runs the thunk exactly once when disabled", async () => { |
| 12 | + const run = vi.fn().mockRejectedValue(new Error("infra")); |
| 13 | + await expect( |
| 14 | + withInfraRetry(run, { options: { ...options, enabled: false }, isRetryable: retryable }) |
| 15 | + ).rejects.toThrow("infra"); |
| 16 | + expect(run).toHaveBeenCalledTimes(1); |
| 17 | + }); |
| 18 | + |
| 19 | + it("retries a retryable error until it succeeds", async () => { |
| 20 | + const run = vi |
| 21 | + .fn() |
| 22 | + .mockRejectedValueOnce(new Error("infra")) |
| 23 | + .mockRejectedValueOnce(new Error("infra")) |
| 24 | + .mockResolvedValue("ok"); |
| 25 | + const result = await withInfraRetry(run, { options, isRetryable: retryable, sleep: noSleep }); |
| 26 | + expect(result).toBe("ok"); |
| 27 | + expect(run).toHaveBeenCalledTimes(3); |
| 28 | + }); |
| 29 | + |
| 30 | + it("does not retry a non-retryable error", async () => { |
| 31 | + const run = vi.fn().mockRejectedValue(new Error("query bug")); |
| 32 | + await expect( |
| 33 | + withInfraRetry(run, { options, isRetryable: () => false, sleep: noSleep }) |
| 34 | + ).rejects.toThrow("query bug"); |
| 35 | + expect(run).toHaveBeenCalledTimes(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it("gives up after maxAttempts", async () => { |
| 39 | + const run = vi.fn().mockRejectedValue(new Error("infra")); |
| 40 | + await expect( |
| 41 | + withInfraRetry(run, { options, isRetryable: retryable, sleep: noSleep }) |
| 42 | + ).rejects.toThrow("infra"); |
| 43 | + expect(run).toHaveBeenCalledTimes(4); |
| 44 | + }); |
| 45 | + |
| 46 | + it("stops retrying once the shared budget is exhausted", async () => { |
| 47 | + const run = vi.fn().mockRejectedValue(new Error("infra")); |
| 48 | + let tokens = 1; |
| 49 | + const budget: RetryBudget = { tryConsume: () => (tokens-- > 0 ? true : false) }; |
| 50 | + await expect( |
| 51 | + withInfraRetry(run, { options, isRetryable: retryable, budget, sleep: noSleep }) |
| 52 | + ).rejects.toThrow("infra"); |
| 53 | + // first attempt + one budgeted retry, then the budget denies the next |
| 54 | + expect(run).toHaveBeenCalledTimes(2); |
| 55 | + }); |
| 56 | + |
| 57 | + it("runs the thunk exactly once when maxAttempts <= 1", async () => { |
| 58 | + const run = vi.fn().mockRejectedValue(new Error("infra")); |
| 59 | + await expect( |
| 60 | + withInfraRetry(run, { |
| 61 | + options: { ...options, maxAttempts: 1 }, |
| 62 | + isRetryable: retryable, |
| 63 | + sleep: noSleep, |
| 64 | + }) |
| 65 | + ).rejects.toThrow("infra"); |
| 66 | + expect(run).toHaveBeenCalledTimes(1); |
| 67 | + }); |
| 68 | + |
| 69 | + it("defaults to isInfrastructureError when no isRetryable is provided", async () => { |
| 70 | + const infra = new Prisma.PrismaClientKnownRequestError("boom", { |
| 71 | + code: "P1001", |
| 72 | + clientVersion: "6.14.0", |
| 73 | + }); |
| 74 | + const run = vi.fn().mockRejectedValueOnce(infra).mockResolvedValue("ok"); |
| 75 | + // no isRetryable in the config — the module must default to the classifier |
| 76 | + const result = await withInfraRetry(run, { options, sleep: noSleep }); |
| 77 | + expect(result).toBe("ok"); |
| 78 | + expect(run).toHaveBeenCalledTimes(2); |
| 79 | + }); |
| 80 | + |
| 81 | + it("with the default classifier, does not retry a real query error", async () => { |
| 82 | + const queryError = new Prisma.PrismaClientKnownRequestError("not found", { |
| 83 | + code: "P2025", |
| 84 | + clientVersion: "6.14.0", |
| 85 | + }); |
| 86 | + const run = vi.fn().mockRejectedValue(queryError); |
| 87 | + await expect(withInfraRetry(run, { options, sleep: noSleep })).rejects.toBe(queryError); |
| 88 | + expect(run).toHaveBeenCalledTimes(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it("clamps a swapped min/max backoff", async () => { |
| 92 | + const run = vi.fn().mockRejectedValueOnce(new Error("infra")).mockResolvedValue("ok"); |
| 93 | + const onRetry = vi.fn(); |
| 94 | + await withInfraRetry(run, { |
| 95 | + options: { enabled: true, maxAttempts: 2, backoffMinMs: 300, backoffMaxMs: 100 }, |
| 96 | + isRetryable: retryable, |
| 97 | + sleep: noSleep, |
| 98 | + random: () => 0.9, |
| 99 | + onRetry, |
| 100 | + }); |
| 101 | + // min > max collapses to [100, 100], so the delay is 100 regardless of random |
| 102 | + expect(onRetry).toHaveBeenCalledWith(expect.objectContaining({ delayMs: 100 })); |
| 103 | + }); |
| 104 | + |
| 105 | + it("computes a jittered delay within bounds and reports it", async () => { |
| 106 | + const run = vi.fn().mockRejectedValueOnce(new Error("infra")).mockResolvedValue("ok"); |
| 107 | + const onRetry = vi.fn(); |
| 108 | + await withInfraRetry(run, { |
| 109 | + options: { enabled: true, maxAttempts: 3, backoffMinMs: 100, backoffMaxMs: 300 }, |
| 110 | + isRetryable: retryable, |
| 111 | + sleep: noSleep, |
| 112 | + random: () => 0.5, |
| 113 | + onRetry, |
| 114 | + }); |
| 115 | + expect(onRetry).toHaveBeenCalledWith(expect.objectContaining({ attempt: 1, delayMs: 200 })); |
| 116 | + }); |
| 117 | +}); |
0 commit comments