|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { buildPrismaConnectionUrl } from "./prismaConnectionUrl"; |
| 2 | +import { |
| 3 | + buildPrismaConnectionUrl, |
| 4 | + connectionLifetimePoolOptions, |
| 5 | + resolveConnectionLifetimeSeconds, |
| 6 | +} from "./prismaConnectionUrl"; |
3 | 7 |
|
4 | 8 | describe("buildPrismaConnectionUrl", () => { |
5 | 9 | it("sets connect_timeout (the Postgres connector parameter), not the ignored connection_timeout", () => { |
@@ -27,4 +31,105 @@ describe("buildPrismaConnectionUrl", () => { |
27 | 31 | expect(url.searchParams.get("sslmode")).toBe("require"); |
28 | 32 | expect(url.searchParams.get("connect_timeout")).toBe("20"); |
29 | 33 | }); |
| 34 | + |
| 35 | + // The inertness guard: with the lifetime unconfigured the DSN must be exactly |
| 36 | + // what it was before this parameter existed. Make the searchParams.set |
| 37 | + // unconditional and this goes red on the literal string "undefined". |
| 38 | + it("omits max_connection_lifetime entirely when it is not configured", () => { |
| 39 | + const base = { connectionLimit: "5", poolTimeout: "10", connectTimeout: "20" }; |
| 40 | + |
| 41 | + const withoutKey = buildPrismaConnectionUrl( |
| 42 | + "postgresql://u:p@host:5432/db?schema=public&sslmode=require", |
| 43 | + { ...base, applicationName: "svc" } |
| 44 | + ); |
| 45 | + const withUndefined = buildPrismaConnectionUrl( |
| 46 | + "postgresql://u:p@host:5432/db?schema=public&sslmode=require", |
| 47 | + { ...base, applicationName: "svc", maxConnectionLifetime: undefined } |
| 48 | + ); |
| 49 | + |
| 50 | + expect(withoutKey.searchParams.has("max_connection_lifetime")).toBe(false); |
| 51 | + expect(withUndefined.searchParams.has("max_connection_lifetime")).toBe(false); |
| 52 | + expect(withUndefined.href).toBe(withoutKey.href); |
| 53 | + expect(withoutKey.href).toBe( |
| 54 | + "postgresql://u:p@host:5432/db?schema=public&sslmode=require&connection_limit=5&pool_timeout=10&connect_timeout=20&application_name=svc" |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + // Pins the contract as "omit iff undefined": any supplied string is passed |
| 59 | + // through, so the caller decides, not this builder. |
| 60 | + it("passes through an explicitly supplied zero", () => { |
| 61 | + const url = buildPrismaConnectionUrl("postgresql://u:p@host:5432/db", { |
| 62 | + connectionLimit: "5", |
| 63 | + poolTimeout: "10", |
| 64 | + connectTimeout: "20", |
| 65 | + applicationName: "svc", |
| 66 | + maxConnectionLifetime: "0", |
| 67 | + }); |
| 68 | + |
| 69 | + expect(url.searchParams.get("max_connection_lifetime")).toBe("0"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("sets max_connection_lifetime when configured", () => { |
| 73 | + const url = buildPrismaConnectionUrl("postgresql://u:p@host:5432/db", { |
| 74 | + connectionLimit: "5", |
| 75 | + poolTimeout: "10", |
| 76 | + connectTimeout: "20", |
| 77 | + applicationName: "svc", |
| 78 | + maxConnectionLifetime: "3600", |
| 79 | + }); |
| 80 | + |
| 81 | + expect(url.searchParams.get("max_connection_lifetime")).toBe("3600"); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe("resolveConnectionLifetimeSeconds", () => { |
| 86 | + it.each([ |
| 87 | + ["unset", undefined], |
| 88 | + ["zero", 0], |
| 89 | + ["negative", -1], |
| 90 | + ["NaN", Number.NaN], |
| 91 | + ["Infinity", Number.POSITIVE_INFINITY], |
| 92 | + ])("returns undefined when the base is %s", (_label, base) => { |
| 93 | + expect(resolveConnectionLifetimeSeconds(base, () => 0.5)).toBeUndefined(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("returns the base with no jitter at the bottom of the random range", () => { |
| 97 | + expect(resolveConnectionLifetimeSeconds(3600, () => 0)).toBe(3600); |
| 98 | + }); |
| 99 | + |
| 100 | + it("subtracts at most 20% jitter at the top of the random range", () => { |
| 101 | + expect(resolveConnectionLifetimeSeconds(3600, () => 1)).toBe(2880); |
| 102 | + }); |
| 103 | + |
| 104 | + it("clamps an out-of-range random into [base * 0.8, base]", () => { |
| 105 | + expect(resolveConnectionLifetimeSeconds(3600, () => -5)).toBe(3600); |
| 106 | + expect(resolveConnectionLifetimeSeconds(3600, () => 5)).toBe(2880); |
| 107 | + expect(resolveConnectionLifetimeSeconds(3600, () => Number.NaN)).toBe(3600); |
| 108 | + }); |
| 109 | + |
| 110 | + // The configured value is a maximum: an operator may set it just below an |
| 111 | + // upstream cutoff, so jitter must never push a connection past it. Flip the |
| 112 | + // sign in resolveConnectionLifetimeSeconds and this goes red. |
| 113 | + it.each([1, 2, 60, 3600, 82800])("never exceeds the configured cap of %i", (base) => { |
| 114 | + for (const r of [0, 0.25, 0.5, 0.75, 1]) { |
| 115 | + const resolved = resolveConnectionLifetimeSeconds(base, () => r); |
| 116 | + expect(resolved).toBeLessThanOrEqual(base); |
| 117 | + expect(resolved).toBeGreaterThanOrEqual(1); |
| 118 | + } |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe("connectionLifetimePoolOptions", () => { |
| 123 | + // Omitting the key is what keeps an unconfigured deployment inert, so assert |
| 124 | + // absence rather than an undefined value. |
| 125 | + it("omits maxLifetimeSeconds entirely when uncapped", () => { |
| 126 | + const options = connectionLifetimePoolOptions(undefined); |
| 127 | + |
| 128 | + expect("maxLifetimeSeconds" in options).toBe(false); |
| 129 | + expect(Object.keys(options)).toHaveLength(0); |
| 130 | + }); |
| 131 | + |
| 132 | + it("carries maxLifetimeSeconds when a lifetime is resolved", () => { |
| 133 | + expect(connectionLifetimePoolOptions(3600)).toEqual({ maxLifetimeSeconds: 3600 }); |
| 134 | + }); |
30 | 135 | }); |
0 commit comments