mirror of
https://github.com/VibedByKaKi/t3-code-android-nightly.git
synced 2026-10-12 13:37:19 +02:00
94 lines
3 KiB
TypeScript
94 lines
3 KiB
TypeScript
import { PgPool } from "@effect/sql-pg"
|
|
import { assert, describe, it } from "@effect/vitest"
|
|
import { Effect } from "effect"
|
|
import { Duplex } from "node:stream"
|
|
|
|
const backendMessage = (tag: string, payload: Buffer): Buffer => {
|
|
const length = Buffer.allocUnsafe(4)
|
|
length.writeInt32BE(payload.length + 4)
|
|
return Buffer.concat([Buffer.from(tag), length, payload])
|
|
}
|
|
|
|
const ready = Buffer.concat([
|
|
backendMessage("R", Buffer.alloc(4)),
|
|
backendMessage("K", Buffer.alloc(8)),
|
|
backendMessage("Z", Buffer.from("I"))
|
|
])
|
|
|
|
const waitFor = (predicate: () => boolean) =>
|
|
Effect.promise(async () => {
|
|
for (let i = 0; i < 100 && !predicate(); i++) {
|
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
}
|
|
assert.ok(predicate(), "background acquisition did not start")
|
|
})
|
|
|
|
describe("PgPool failed startup", () => {
|
|
it.live(
|
|
"retries a stale background acquisition rather than failing an unrelated checkout",
|
|
() =>
|
|
Effect.scoped(Effect.gen(function*() {
|
|
let attempts = 0
|
|
let backgroundStarted = false
|
|
const pool = yield* PgPool.make({
|
|
username: "test",
|
|
connectTimeout: "250 millis",
|
|
minConnections: 2,
|
|
maxConnections: 2,
|
|
stream: () => {
|
|
attempts++
|
|
const attempt = attempts
|
|
let startup = true
|
|
const socket = new Duplex({
|
|
read() {},
|
|
write(_chunk: Buffer, _encoding, callback) {
|
|
if (startup) {
|
|
startup = false
|
|
if (attempt === 2) backgroundStarted = true
|
|
else queueMicrotask(() => socket.push(ready))
|
|
}
|
|
callback()
|
|
}
|
|
})
|
|
return socket
|
|
}
|
|
})
|
|
|
|
// Keep the healthy session leased so the failure becomes a placeholder.
|
|
const healthy = yield* pool.get
|
|
yield* waitFor(() => backgroundStarted)
|
|
yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 400)))
|
|
|
|
const replacement = yield* pool.get
|
|
assert.notStrictEqual(replacement, healthy)
|
|
assert.strictEqual(attempts, 3)
|
|
})),
|
|
10_000
|
|
)
|
|
|
|
it.live(
|
|
"reports a caller-owned connect failure instead of retrying indefinitely",
|
|
() =>
|
|
Effect.scoped(Effect.gen(function*() {
|
|
let attempts = 0
|
|
const pool = yield* PgPool.make({
|
|
username: "test",
|
|
maxConnections: 1,
|
|
stream: () => {
|
|
attempts++
|
|
const socket = new Duplex({ read() {}, write() {} })
|
|
queueMicrotask(() => socket.destroy())
|
|
return socket
|
|
}
|
|
})
|
|
const result = yield* Effect.result(pool.get)
|
|
assert.strictEqual(result._tag, "Failure")
|
|
if (result._tag === "Failure") {
|
|
assert.strictEqual(result.failure.reason._tag, "ConnectionError")
|
|
assert.strictEqual(result.failure.reason.operation, "connect")
|
|
}
|
|
assert.strictEqual(attempts, 1)
|
|
})),
|
|
10_000
|
|
)
|
|
})
|