|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { |
| 3 | + extractWithDefinitionRetry, |
| 4 | + resolveExtractRetries, |
| 5 | +} from "./extract-with-retry.ts"; |
| 6 | + |
| 7 | +type Row = { id: string; definition: string | null }; |
| 8 | + |
| 9 | +const hasNullDefinition = (r: Row) => r.definition === null; |
| 10 | + |
| 11 | +describe("resolveExtractRetries", () => { |
| 12 | + const originalEnv = process.env.PGDELTA_EXTRACT_RETRIES; |
| 13 | + afterEach(() => { |
| 14 | + if (originalEnv === undefined) { |
| 15 | + process.env.PGDELTA_EXTRACT_RETRIES = undefined; |
| 16 | + delete process.env.PGDELTA_EXTRACT_RETRIES; |
| 17 | + } else { |
| 18 | + process.env.PGDELTA_EXTRACT_RETRIES = originalEnv; |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + test("defaults to 1 when option and env are unset", () => { |
| 23 | + delete process.env.PGDELTA_EXTRACT_RETRIES; |
| 24 | + expect(resolveExtractRetries()).toBe(1); |
| 25 | + }); |
| 26 | + |
| 27 | + test("uses option when provided", () => { |
| 28 | + process.env.PGDELTA_EXTRACT_RETRIES = "5"; |
| 29 | + expect(resolveExtractRetries(0)).toBe(0); |
| 30 | + expect(resolveExtractRetries(1)).toBe(1); |
| 31 | + expect(resolveExtractRetries(7)).toBe(7); |
| 32 | + }); |
| 33 | + |
| 34 | + test("falls back to env when option is undefined", () => { |
| 35 | + process.env.PGDELTA_EXTRACT_RETRIES = "4"; |
| 36 | + expect(resolveExtractRetries()).toBe(4); |
| 37 | + }); |
| 38 | + |
| 39 | + test("clamps negative values to 0", () => { |
| 40 | + delete process.env.PGDELTA_EXTRACT_RETRIES; |
| 41 | + expect(resolveExtractRetries(-3)).toBe(0); |
| 42 | + process.env.PGDELTA_EXTRACT_RETRIES = "-9"; |
| 43 | + expect(resolveExtractRetries()).toBe(0); |
| 44 | + }); |
| 45 | + |
| 46 | + test("ignores non-numeric env values", () => { |
| 47 | + process.env.PGDELTA_EXTRACT_RETRIES = "not-a-number"; |
| 48 | + expect(resolveExtractRetries()).toBe(1); |
| 49 | + }); |
| 50 | + |
| 51 | + test("ignores empty env string", () => { |
| 52 | + process.env.PGDELTA_EXTRACT_RETRIES = ""; |
| 53 | + expect(resolveExtractRetries()).toBe(1); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe("extractWithDefinitionRetry", () => { |
| 58 | + test("returns first attempt when no row has null definition", async () => { |
| 59 | + let attempts = 0; |
| 60 | + const rows = await extractWithDefinitionRetry<Row>({ |
| 61 | + label: "test", |
| 62 | + query: async () => { |
| 63 | + attempts++; |
| 64 | + return [{ id: "a", definition: "OK" }]; |
| 65 | + }, |
| 66 | + hasNullDefinition, |
| 67 | + options: { retries: 2, backoffMs: 0 }, |
| 68 | + }); |
| 69 | + expect(attempts).toBe(1); |
| 70 | + expect(rows).toEqual([{ id: "a", definition: "OK" }]); |
| 71 | + }); |
| 72 | + |
| 73 | + test("retries when definition is null and succeeds on attempt 2", async () => { |
| 74 | + let attempts = 0; |
| 75 | + const rows = await extractWithDefinitionRetry<Row>({ |
| 76 | + label: "test", |
| 77 | + query: async () => { |
| 78 | + attempts++; |
| 79 | + if (attempts === 1) { |
| 80 | + return [ |
| 81 | + { id: "a", definition: "OK" }, |
| 82 | + { id: "b", definition: null }, |
| 83 | + ]; |
| 84 | + } |
| 85 | + return [{ id: "a", definition: "OK" }]; |
| 86 | + }, |
| 87 | + hasNullDefinition, |
| 88 | + options: { retries: 2, backoffMs: 0 }, |
| 89 | + }); |
| 90 | + expect(attempts).toBe(2); |
| 91 | + expect(rows).toEqual([{ id: "a", definition: "OK" }]); |
| 92 | + }); |
| 93 | + |
| 94 | + test("returns last-attempt rows (with offenders) once retries are exhausted", async () => { |
| 95 | + let attempts = 0; |
| 96 | + const rows = await extractWithDefinitionRetry<Row>({ |
| 97 | + label: "test", |
| 98 | + query: async () => { |
| 99 | + attempts++; |
| 100 | + return [ |
| 101 | + { id: "a", definition: "OK" }, |
| 102 | + { id: "b", definition: null }, |
| 103 | + ]; |
| 104 | + }, |
| 105 | + hasNullDefinition, |
| 106 | + options: { retries: 2, backoffMs: 0 }, |
| 107 | + }); |
| 108 | + expect(attempts).toBe(3); |
| 109 | + expect(rows).toEqual([ |
| 110 | + { id: "a", definition: "OK" }, |
| 111 | + { id: "b", definition: null }, |
| 112 | + ]); |
| 113 | + }); |
| 114 | + |
| 115 | + test("retries: 0 disables retrying entirely", async () => { |
| 116 | + let attempts = 0; |
| 117 | + const rows = await extractWithDefinitionRetry<Row>({ |
| 118 | + label: "test", |
| 119 | + query: async () => { |
| 120 | + attempts++; |
| 121 | + return [{ id: "b", definition: null }]; |
| 122 | + }, |
| 123 | + hasNullDefinition, |
| 124 | + options: { retries: 0, backoffMs: 0 }, |
| 125 | + }); |
| 126 | + expect(attempts).toBe(1); |
| 127 | + expect(rows).toEqual([{ id: "b", definition: null }]); |
| 128 | + }); |
| 129 | + |
| 130 | + test("retries: 5 attempts up to 6 times before giving up", async () => { |
| 131 | + let attempts = 0; |
| 132 | + await extractWithDefinitionRetry<Row>({ |
| 133 | + label: "test", |
| 134 | + query: async () => { |
| 135 | + attempts++; |
| 136 | + return [{ id: "b", definition: null }]; |
| 137 | + }, |
| 138 | + hasNullDefinition, |
| 139 | + options: { retries: 5, backoffMs: 0 }, |
| 140 | + }); |
| 141 | + expect(attempts).toBe(6); |
| 142 | + }); |
| 143 | +}); |
0 commit comments