|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { findWithReplicaRetry } from "~/services/replicaLagRetry.server"; |
| 3 | + |
| 4 | +const base = { hasDedicatedReplica: true, retryDelayMs: { min: 0, max: 0 } }; |
| 5 | + |
| 6 | +describe("findWithReplicaRetry", () => { |
| 7 | + it("returns the first replica hit without retrying or touching the primary", async () => { |
| 8 | + const replicaFind = vi.fn().mockResolvedValue({ id: "env_1" }); |
| 9 | + const primaryFind = vi.fn(); |
| 10 | + const onOutcome = vi.fn(); |
| 11 | + |
| 12 | + const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); |
| 13 | + |
| 14 | + expect(result).toEqual({ id: "env_1" }); |
| 15 | + expect(replicaFind).toHaveBeenCalledTimes(1); |
| 16 | + expect(primaryFind).not.toHaveBeenCalled(); |
| 17 | + expect(onOutcome).not.toHaveBeenCalled(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("recovers via a replica retry when the row appears on the second read", async () => { |
| 21 | + const replicaFind = vi.fn().mockResolvedValueOnce(null).mockResolvedValueOnce({ id: "env_1" }); |
| 22 | + const primaryFind = vi.fn(); |
| 23 | + const onOutcome = vi.fn(); |
| 24 | + |
| 25 | + const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); |
| 26 | + |
| 27 | + expect(result).toEqual({ id: "env_1" }); |
| 28 | + expect(replicaFind).toHaveBeenCalledTimes(2); |
| 29 | + expect(primaryFind).not.toHaveBeenCalled(); |
| 30 | + expect(onOutcome).toHaveBeenCalledWith("replica_retry"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("falls back to the primary when the replica misses twice", async () => { |
| 34 | + const replicaFind = vi.fn().mockResolvedValue(null); |
| 35 | + const primaryFind = vi.fn().mockResolvedValue({ id: "env_1" }); |
| 36 | + const onOutcome = vi.fn(); |
| 37 | + |
| 38 | + const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); |
| 39 | + |
| 40 | + expect(result).toEqual({ id: "env_1" }); |
| 41 | + expect(replicaFind).toHaveBeenCalledTimes(2); |
| 42 | + expect(primaryFind).toHaveBeenCalledTimes(1); |
| 43 | + expect(onOutcome).toHaveBeenCalledWith("primary"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("reports a genuine miss and returns null", async () => { |
| 47 | + const replicaFind = vi.fn().mockResolvedValue(null); |
| 48 | + const primaryFind = vi.fn().mockResolvedValue(null); |
| 49 | + const onOutcome = vi.fn(); |
| 50 | + |
| 51 | + const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); |
| 52 | + |
| 53 | + expect(result).toBeNull(); |
| 54 | + expect(onOutcome).toHaveBeenCalledWith("not_found"); |
| 55 | + expect(onOutcome).toHaveBeenCalledTimes(1); |
| 56 | + }); |
| 57 | + |
| 58 | + it("does a single lookup when there is no dedicated replica", async () => { |
| 59 | + const replicaFind = vi.fn().mockResolvedValue(null); |
| 60 | + const primaryFind = vi.fn(); |
| 61 | + const onOutcome = vi.fn(); |
| 62 | + |
| 63 | + const result = await findWithReplicaRetry({ |
| 64 | + ...base, |
| 65 | + hasDedicatedReplica: false, |
| 66 | + replicaFind, |
| 67 | + primaryFind, |
| 68 | + onOutcome, |
| 69 | + }); |
| 70 | + |
| 71 | + expect(result).toBeNull(); |
| 72 | + expect(replicaFind).toHaveBeenCalledTimes(1); |
| 73 | + expect(primaryFind).not.toHaveBeenCalled(); |
| 74 | + expect(onOutcome).toHaveBeenCalledWith("not_found"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("does not fail the lookup when the outcome callback throws", async () => { |
| 78 | + const replicaFind = vi.fn().mockResolvedValueOnce(null).mockResolvedValueOnce({ id: "env_1" }); |
| 79 | + const primaryFind = vi.fn(); |
| 80 | + const onOutcome = vi.fn(() => { |
| 81 | + throw new Error("meter unavailable"); |
| 82 | + }); |
| 83 | + |
| 84 | + const result = await findWithReplicaRetry({ ...base, replicaFind, primaryFind, onOutcome }); |
| 85 | + |
| 86 | + expect(result).toEqual({ id: "env_1" }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments