The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { PgConnection, PgTypes } from "@effect/sql-pg" |
| 2 | import { assert, it } from "@effect/vitest" |
| 3 | import { Cause, Deferred, Effect, Exit, Fiber, Option, Queue, Redacted, Scheduler, Scope, Stream } from "effect" |
| 4 | import type { SqlError } from "effect/sql/SqlError" |
| 5 | import * as TestClock from "effect/testing/TestClock" |
| 6 | import * as Net from "node:net" |
| 7 | import { PgContainer, waitUntilActive, waitUntilAdvisoryLock } from "./utils.ts" |
| 8 | |
| 9 | const makeConnection = Effect.fnUntraced(function*(options?: PgConnection.Config) { |
| 10 | const container = yield* PgContainer |
| 11 | return yield* PgConnection.make({ |
| 12 | url: Redacted.make(container.getConnectionUri()), |
| 13 | ...options |
| 14 | }) |
| 15 | }) |
| 16 | |
| 17 | const assertInterruptedOnClose = Effect.fnUntraced(function*( |
| 18 | scope: Scope.Closeable, |
| 19 | notifications: Queue.Dequeue<PgConnection.Notification, SqlError> |
| 20 | ) { |
| 21 | const consumer = yield* Effect.forkScoped(Queue.take(notifications)) |
| 22 | yield* Scope.close(scope, Exit.void) |
| 23 | const exit = yield* Fiber.await(consumer) |
| 24 | assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)) |
| 25 | }) |
| 26 | |
| 27 | // Runs a fiber normally until `holding` is set, then queues its tasks and runs |
| 28 | // them one at a time through `step`, so a test can stop it between two operations. |
| 29 | class SteppedScheduler implements Scheduler.Scheduler { |
| 30 | readonly executionMode = "async" |
| 31 | holding = false |
| 32 | private readonly fallback = new Scheduler.MixedScheduler() |
| 33 | private readonly held: Array<() => void> = [] |
| 34 | private onHeld: (() => void) | undefined |
| 35 | |
| 36 | // While holding, yield after one operation; the first operation after a |
| 37 | // yield only resumes the yielded continuation. |
| 38 | shouldYield(fiber: Fiber.Fiber<unknown, unknown>): boolean { |
| 39 | return this.holding ? fiber.currentOpCount > 2 : this.fallback.shouldYield(fiber) |
| 40 | } |
| 41 | |
| 42 | makeDispatcher(): Scheduler.SchedulerDispatcher { |
| 43 | const dispatcher = this.fallback.makeDispatcher() |
| 44 | return { |
| 45 | scheduleTask: (task, priority) => { |
| 46 | if (!this.holding) return dispatcher.scheduleTask(task, priority) |
| 47 | this.held.push(task) |
| 48 | const onHeld = this.onHeld |
| 49 | this.onHeld = undefined |
| 50 | onHeld?.() |
| 51 | }, |
| 52 | flush: () => dispatcher.flush() |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | readonly step: Effect.Effect<void> = Effect.callback((resume) => { |
| 57 | const run = () => |
| 58 | setImmediate(() => { |
| 59 | this.held.shift()!() |
| 60 | resume(Effect.void) |
| 61 | }) |
| 62 | if (this.held.length > 0) run() |
| 63 | else this.onHeld = run |
| 64 | }) |
| 65 | |
| 66 | release(): void { |
| 67 | this.holding = false |
| 68 | for (const task of this.held.splice(0)) setImmediate(task) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | it.layer(PgContainer.layer, { timeout: "30 seconds" })("PgConnection", (it) => { |
| 73 | it.effect("interrupts notification consumers when the connection scope closes", () => |
| 74 | Effect.gen(function*() { |
| 75 | const scope = yield* Scope.fork(yield* Scope.Scope) |
| 76 | const connection = yield* Scope.provide(makeConnection(), scope) |
| 77 | const notifications = yield* connection.listen("closed_listener") |
| 78 | yield* assertInterruptedOnClose(scope, notifications) |
| 79 | })) |
| 80 | |
| 81 | it.effect("interrupts notification consumers when the listener scope closes", () => |
| 82 | Effect.gen(function*() { |
| 83 | const scope = yield* Scope.fork(yield* Scope.Scope) |
| 84 | const connection = yield* makeConnection() |
| 85 | const notifications = yield* Scope.provide(connection.listen("closed_listener"), scope) |
| 86 | yield* assertInterruptedOnClose(scope, notifications) |
| 87 | })) |
| 88 | |
| 89 | it.effect("closes a listener whose connection stops responding", () => |
| 90 | Effect.gen(function*() { |
| 91 | const container = yield* PgContainer |
| 92 | const sockets: Array<Net.Socket> = [] |
| 93 | const connection = yield* makeConnection({ |
| 94 | stream: () => { |
| 95 | const socket = Net.connect({ host: container.getHost(), port: container.getMappedPort(5432) }) |
| 96 | sockets.push(socket) |
| 97 | return socket |
| 98 | } |
| 99 | }) |
| 100 | const scope = yield* Scope.make() |
| 101 | yield* Scope.provide(connection.listen("unanswered_listener"), scope) |
| 102 | for (const socket of sockets) socket.pause() |
| 103 | |
| 104 | const closing = yield* Effect.forkDetach(Scope.close(scope, Exit.void)) |
| 105 | yield* Effect.gen(function*() { |
| 106 | const closed = yield* Fiber.await(closing).pipe(Effect.timeoutOption("10 seconds")) |
| 107 | assert.isTrue(Option.isSome(closed), "listener scope did not close within 10 seconds") |
| 108 | assert.isTrue(Exit.isFailure(yield* Effect.exit(connection.query("SELECT 1")))) |
| 109 | }).pipe(Effect.ensuring(Effect.sync(() => sockets.forEach((socket) => socket.destroy())))) |
| 110 | }).pipe(TestClock.withLive), { timeout: 30000 }) |
| 111 | |
| 112 | it.effect("keeps a busy pinned session when a listener closes", () => |
| 113 | Effect.gen(function*() { |
| 114 | const connection = yield* makeConnection() |
| 115 | const observer = yield* makeConnection() |
| 116 | const pinned = yield* connection.pin |
| 117 | const scope = yield* Scope.make() |
| 118 | yield* Scope.provide(pinned.listen("busy_listener"), scope) |
| 119 | const slow = yield* Effect.forkChild(pinned.query("SELECT pg_sleep(6)")) |
| 120 | yield* waitUntilActive(observer, pinned.processId) |
| 121 | |
| 122 | yield* Scope.close(scope, Exit.void) |
| 123 | yield* Fiber.join(slow) |
| 124 | const result = yield* pinned.query("SELECT 1 AS one") |
| 125 | assert.deepStrictEqual(result.rows, [{ one: 1 }]) |
| 126 | }).pipe(TestClock.withLive), { timeout: 30000 }) |
| 127 | |
| 128 | it.effect("skips UNLISTEN queued behind a pin release", () => |
| 129 | Effect.gen(function*() { |
| 130 | const connection = yield* makeConnection() |
| 131 | const pinScope = yield* Scope.make() |
| 132 | const pinned = yield* Scope.provide(connection.pin, pinScope) |
| 133 | const listenScope = yield* Scope.make() |
| 134 | yield* Scope.provide(pinned.listen("released_listener"), listenScope) |
| 135 | // Release the pin in the same fiber as the statement, so the release |
| 136 | // follows the statement's response without waiting on the scheduler. |
| 137 | const releasing = yield* Effect.forkChild( |
| 138 | Effect.andThen(pinned.query("SELECT pg_sleep(0.05)"), Scope.close(pinScope, Exit.void)), |
| 139 | { startImmediately: true } |
| 140 | ) |
| 141 | // Close the listener while the statement holds the wire, queueing |
| 142 | // UNLISTEN behind it. |
| 143 | const closing = yield* Effect.forkChild(Scope.close(listenScope, Exit.void), { startImmediately: true }) |
| 144 | yield* Fiber.join(releasing) |
| 145 | yield* Fiber.join(closing) |
| 146 | |
| 147 | const next = yield* Effect.forkDetach(connection.query("SELECT 1 AS one")) |
| 148 | const result = yield* Fiber.join(next).pipe(Effect.timeout("2 seconds")) |
| 149 | assert.deepStrictEqual(result.rows, [{ one: 1 }]) |
| 150 | }).pipe(TestClock.withLive)) |
| 151 | |
| 152 | it.effect("waits for an UNLISTEN holding the wire at pin release", () => |
| 153 | Effect.gen(function*() { |
| 154 | // Each run has a narrow window, so repeat it to make a regression show up reliably. |
| 155 | for (let run = 0; run < 10; run++) { |
| 156 | yield* Effect.scoped(Effect.gen(function*() { |
| 157 | const connection = yield* makeConnection() |
| 158 | const pinScope = yield* Scope.make() |
| 159 | const pinned = yield* Scope.provide(connection.pin, pinScope) |
| 160 | const listenScope = yield* Scope.make() |
| 161 | yield* Scope.provide(pinned.listen("unlistening_listener"), listenScope) |
| 162 | const closing = yield* Effect.forkDetach( |
| 163 | Effect.andThen(Effect.yieldNow, Scope.close(listenScope, Exit.void)) |
| 164 | ) |
| 165 | yield* pinned.query("SELECT pg_sleep(0.05)") |
| 166 | // Let the queued UNLISTEN take the wire, so the release lands while |
| 167 | // it runs or between its completion and the permit release. |
| 168 | yield* Effect.yieldNow |
| 169 | yield* Effect.yieldNow |
| 170 | yield* Effect.yieldNow |
| 171 | |
| 172 | yield* Scope.close(pinScope, Exit.void) |
| 173 | const next = yield* Effect.forkDetach(connection.query("SELECT 1 AS one")) |
| 174 | const result = yield* Fiber.join(next).pipe(Effect.timeout("2 seconds")) |
| 175 | assert.deepStrictEqual(result.rows, [{ one: 1 }]) |
| 176 | yield* Fiber.join(closing) |
| 177 | })) |
| 178 | } |
| 179 | }).pipe(TestClock.withLive, Effect.provideService(Scheduler.MaxOpsBeforeYield, 5))) |
| 180 | |
| 181 | it.effect("does not wait for ordinary work that overtakes UNLISTEN at pin release", () => |
| 182 | Effect.gen(function*() { |
| 183 | const container = yield* PgContainer |
| 184 | const sockets: Array<Net.Socket> = [] |
| 185 | const written: Array<string> = [] |
| 186 | const connection = yield* makeConnection({ |
| 187 | stream: () => { |
| 188 | const socket = Net.connect({ host: container.getHost(), port: container.getMappedPort(5432) }) |
| 189 | const write = socket.write.bind(socket) as (chunk: Uint8Array) => boolean |
| 190 | socket.write = ((chunk: Uint8Array) => { |
| 191 | written.push(Buffer.from(chunk).toString("latin1")) |
| 192 | return write(chunk) |
| 193 | }) as typeof socket.write |
| 194 | sockets.push(socket) |
| 195 | return socket |
| 196 | } |
| 197 | }) |
| 198 | const sent = (text: string) => written.some((frame) => frame.includes(text)) |
| 199 | const pinScope = yield* Scope.make() |
| 200 | const pinned = yield* Scope.provide(connection.pin, pinScope) |
| 201 | const listenScope = yield* Scope.make() |
| 202 | yield* Scope.provide(pinned.listen("overtaken_listener"), listenScope) |
| 203 | |
| 204 | yield* Effect.gen(function*() { |
| 205 | // Hold the UNLISTEN reply so the cleanup keeps the wire. |
| 206 | for (const socket of sockets) socket.pause() |
| 207 | const scheduler = new SteppedScheduler() |
| 208 | const closing = yield* Effect.forkDetach(Scope.close(listenScope, Exit.void)).pipe( |
| 209 | Effect.provideService(Scheduler.Scheduler, scheduler) |
| 210 | ) |
| 211 | while (!sent("UNLISTEN")) yield* Effect.yieldNow |
| 212 | const overtaking = yield* Effect.forkDetach(pinned.query("SELECT pg_sleep(30)")) |
| 213 | // The forked query queues on the wire before this fiber resumes. |
| 214 | yield* Effect.yieldNow |
| 215 | |
| 216 | // Step the cleanup until the wire passes to the queued query, leaving |
| 217 | // the rest of the cleanup unrun. |
| 218 | scheduler.holding = true |
| 219 | for (const socket of sockets) socket.resume() |
| 220 | while (!sent("pg_sleep(30)")) yield* scheduler.step |
| 221 | |
| 222 | const releasing = yield* Effect.forkDetach(Scope.close(pinScope, Exit.void)) |
| 223 | scheduler.release() |
| 224 | const released = yield* Fiber.await(releasing).pipe(Effect.timeoutOption("2 seconds")) |
| 225 | assert.isTrue(Option.isSome(released), "pin release waited for the overtaking query") |
| 226 | assert.isTrue(Exit.isFailure(yield* Fiber.await(overtaking))) |
| 227 | yield* Fiber.join(closing) |
| 228 | }).pipe(Effect.ensuring(Effect.sync(() => sockets.forEach((socket) => socket.destroy())))) |
| 229 | }).pipe(TestClock.withLive), { timeout: 30000 }) |
| 230 | |
| 231 | it.effect("connects through ReadyForQuery", () => |
| 232 | Effect.gen(function*() { |
| 233 | const connection = yield* makeConnection() |
| 234 | assert.isAbove(connection.processId, 0) |
| 235 | })) |
| 236 | |
| 237 | it.effect("runs unnamed extended queries with inferred binary parameters", () => |
| 238 | Effect.gen(function*() { |
| 239 | const connection = yield* makeConnection() |
| 240 | const instant = new Date("2024-05-06T07:08:09.123Z") |
| 241 | const result = yield* connection.query( |
| 242 | "SELECT $1::int4 AS i, $2::float8 AS f, $3::int8 AS b, $4::bool AS flag, " + |
| 243 | "$5::text AS text, $6::bytea AS bytes, $7::timestamptz AS instant, " + |
| 244 | "$8::int4[] AS numbers, $9::text AS nil, $10::jsonb AS jsonb", |
| 245 | [ |
| 246 | 1, |
| 247 | 1.5, |
| 248 | BigInt(2), |
| 249 | true, |
| 250 | "hello", |
| 251 | new Int8Array([1, -1]), |
| 252 | instant, |
| 253 | [1, null, 3], |
| 254 | null, |
| 255 | PgTypes.jsonb({ nested: true }) |
| 256 | ] |
| 257 | ) |
| 258 | assert.strictEqual(result.command, "SELECT") |
| 259 | assert.strictEqual(result.rowCount, 1) |
| 260 | assert.strictEqual(result.oid, null) |
| 261 | assert.deepStrictEqual(result.rows, [{ |
| 262 | i: 1, |
| 263 | f: 1.5, |
| 264 | b: BigInt(2), |
| 265 | flag: true, |
| 266 | text: "hello", |
| 267 | bytes: new Uint8Array([1, 255]), |
| 268 | instant, |
| 269 | numbers: [1, null, 3], |
| 270 | nil: null, |
| 271 | jsonb: { nested: true } |
| 272 | }]) |
| 273 | assert.deepStrictEqual(result.fields.map((field) => field.name), [ |
| 274 | "i", |
| 275 | "f", |
| 276 | "b", |
| 277 | "flag", |
| 278 | "text", |
| 279 | "bytes", |
| 280 | "instant", |
| 281 | "numbers", |
| 282 | "nil", |
| 283 | "jsonb" |
| 284 | ]) |
| 285 | })) |
| 286 | |
| 287 | it.effect("applies the session TimeZone only to Dates bound into timestamp columns", () => |
| 288 | Effect.gen(function*() { |
| 289 | const connection = yield* makeConnection() |
| 290 | const instant = new Date("2024-05-06T07:08:09.123Z") |
| 291 | yield* connection.query( |
| 292 | "CREATE TEMP TABLE timestamp_timezone (bare timestamp, explicit timestamp, zoned timestamptz)" |
| 293 | ) |
| 294 | for ( |
| 295 | const [zone, expectedBare] of [ |
| 296 | ["UTC", instant], |
| 297 | ["Europe/Berlin", new Date("2024-05-06T09:08:09.123Z")] |
| 298 | ] as const |
| 299 | ) { |
| 300 | yield* connection.query(`SET TIME ZONE '${zone}'`) |
| 301 | yield* connection.query("TRUNCATE timestamp_timezone") |
| 302 | yield* connection.query( |
| 303 | "INSERT INTO timestamp_timezone VALUES ($1, $2, $3)", |
| 304 | [instant, PgTypes.timestamp(instant), instant] |
| 305 | ) |
| 306 | const result = yield* connection.query("SELECT * FROM timestamp_timezone") |
| 307 | assert.deepStrictEqual(result.rows, [{ bare: expectedBare, explicit: instant, zoned: instant }]) |
| 308 | } |
| 309 | })) |
| 310 | |
| 311 | it.effect("returns queryValues in RowDescription order", () => |
| 312 | Effect.gen(function*() { |
| 313 | const connection = yield* makeConnection() |
| 314 | const rows = yield* connection.queryValues( |
| 315 | "SELECT $1::int8 AS second, $2::text AS first", |
| 316 | [PgTypes.int8(BigInt(7)), "value"] |
| 317 | ) |
| 318 | assert.deepStrictEqual(rows, [[BigInt(7), "value"]]) |
| 319 | })) |
| 320 | |
| 321 | it.effect("drains query errors and keeps the connection usable", () => |
| 322 | Effect.gen(function*() { |
| 323 | const connection = yield* makeConnection() |
| 324 | yield* connection.query("CREATE TEMP TABLE pg_connection_unique (value int4 UNIQUE)") |
| 325 | yield* connection.query("INSERT INTO pg_connection_unique VALUES ($1)", [1]) |
| 326 | const error = yield* Effect.flip(connection.query("INSERT INTO pg_connection_unique VALUES ($1)", [1])) |
| 327 | assert.strictEqual(error.reason._tag, "UniqueViolation") |
| 328 | if (error.reason._tag === "UniqueViolation") { |
| 329 | assert.include(error.reason.constraint, "pg_connection_unique") |
| 330 | } |
| 331 | const result = yield* connection.query("SELECT count(*)::int8 AS count FROM pg_connection_unique") |
| 332 | assert.deepStrictEqual(result.rows, [{ count: BigInt(1) }]) |
| 333 | })) |
| 334 | |
| 335 | it.effect("binds untyped strings and integers beyond int4", () => |
| 336 | Effect.gen(function*() { |
| 337 | const connection = yield* makeConnection() |
| 338 | yield* connection.query("CREATE TEMP TABLE inference_widening (id bigint, at timestamptz)") |
| 339 | yield* connection.query( |
| 340 | "INSERT INTO inference_widening (id, at) VALUES ($1, $2)", |
| 341 | ["12345678901234", "2024-05-06T07:08:09Z"] |
| 342 | ) |
| 343 | yield* connection.query("INSERT INTO inference_widening (id) VALUES ($1)", [1755000000000]) |
| 344 | const rows = yield* connection.queryValues("SELECT id FROM inference_widening ORDER BY id") |
| 345 | assert.deepStrictEqual(rows.map((row) => row[0]), [BigInt(1755000000000), BigInt("12345678901234")]) |
| 346 | })) |
| 347 | |
| 348 | it.effect("reparses a statement whose first execution failed at bind", () => |
| 349 | Effect.gen(function*() { |
| 350 | const connection = yield* makeConnection() |
| 351 | const missing = yield* Effect.flip(connection.query("select $1::regclass", ["prepared_bind_failure"])) |
| 352 | assert.strictEqual(missing.reason._tag, "SqlSyntaxError") |
| 353 | yield* connection.query("CREATE TEMP TABLE prepared_bind_failure (id int)") |
| 354 | const found = yield* connection.query("select $1::regclass AS rel", ["prepared_bind_failure"]) |
| 355 | assert.strictEqual(found.rows.length, 1) |
| 356 | })) |
| 357 | |
| 358 | it.effect("fails with AuthenticationError on a bad password", () => |
| 359 | Effect.gen(function*() { |
| 360 | const container = yield* PgContainer |
| 361 | const error = yield* Effect.flip(PgConnection.make({ |
| 362 | host: container.getHost(), |
| 363 | port: container.getMappedPort(5432), |
| 364 | username: container.getUsername(), |
| 365 | password: Redacted.make("definitely-wrong"), |
| 366 | database: container.getDatabase() |
| 367 | })) |
| 368 | assert.strictEqual(error._tag, "SqlError") |
| 369 | assert.strictEqual(error.reason._tag, "AuthenticationError") |
| 370 | })) |
| 371 | |
| 372 | it.effect("fails when the server refuses TLS", () => |
| 373 | Effect.gen(function*() { |
| 374 | const container = yield* PgContainer |
| 375 | const error = yield* Effect.flip(PgConnection.make({ |
| 376 | host: container.getHost(), |
| 377 | port: container.getMappedPort(5432), |
| 378 | username: container.getUsername(), |
| 379 | password: Redacted.make(container.getPassword()), |
| 380 | database: container.getDatabase(), |
| 381 | ssl: true |
| 382 | })) |
| 383 | assert.strictEqual(error.reason._tag, "ConnectionError") |
| 384 | assert.include(error.reason.message, "refused TLS") |
| 385 | })) |
| 386 | it.effect("reuses a prepared statement and keeps its columns", () => |
| 387 | Effect.gen(function*() { |
| 388 | const connection = yield* makeConnection() |
| 389 | for (let index = 0; index < 3; index++) { |
| 390 | const result = yield* connection.query("SELECT $1::int4 AS value, $2::text AS label", [index, "x"]) |
| 391 | assert.deepStrictEqual(result.rows, [{ value: index, label: "x" }]) |
| 392 | assert.deepStrictEqual(result.fields, [ |
| 393 | { name: "value", dataTypeId: 23 }, |
| 394 | { name: "label", dataTypeId: 25 } |
| 395 | ]) |
| 396 | } |
| 397 | const prepared = yield* connection.query("SELECT count(*)::int4 AS n FROM pg_prepared_statements") |
| 398 | assert.deepStrictEqual(prepared.rows[0], { n: 2 }) |
| 399 | })) |
| 400 | |
| 401 | it.effect("prepares the same text once per parameter signature", () => |
| 402 | Effect.gen(function*() { |
| 403 | const connection = yield* makeConnection() |
| 404 | assert.deepStrictEqual((yield* connection.query("SELECT $1 AS v", [1])).rows, [{ v: 1 }]) |
| 405 | assert.deepStrictEqual((yield* connection.query("SELECT $1 AS v", [BigInt(2)])).rows, [{ v: BigInt(2) }]) |
| 406 | const types = yield* connection.query( |
| 407 | "SELECT parameter_types::text AS t FROM pg_prepared_statements WHERE statement = 'SELECT $1 AS v' ORDER BY t" |
| 408 | ) |
| 409 | assert.deepStrictEqual(types.rows, [{ t: "{bigint}" }, { t: "{integer}" }]) |
| 410 | })) |
| 411 | |
| 412 | it.effect("re-parses a statement whose result type changed", () => |
| 413 | Effect.gen(function*() { |
| 414 | const connection = yield* makeConnection() |
| 415 | yield* connection.query("CREATE TEMP TABLE shifting (a int)") |
| 416 | yield* connection.query("INSERT INTO shifting VALUES (1)") |
| 417 | assert.deepStrictEqual((yield* connection.query("SELECT * FROM shifting")).rows, [{ a: 1 }]) |
| 418 | yield* connection.query("ALTER TABLE shifting ADD COLUMN b text DEFAULT 'added'") |
| 419 | const after = yield* connection.query("SELECT * FROM shifting") |
| 420 | assert.deepStrictEqual(after.rows, [{ a: 1, b: "added" }]) |
| 421 | assert.deepStrictEqual(after.fields, [ |
| 422 | { name: "a", dataTypeId: 23 }, |
| 423 | { name: "b", dataTypeId: 25 } |
| 424 | ]) |
| 425 | })) |
| 426 | |
| 427 | it.effect("re-parses a statement the backend no longer holds", () => |
| 428 | Effect.gen(function*() { |
| 429 | const connection = yield* makeConnection() |
| 430 | assert.deepStrictEqual((yield* connection.query("SELECT $1::int4 AS n", [7])).rows, [{ n: 7 }]) |
| 431 | yield* connection.query("DEALLOCATE ALL") |
| 432 | assert.deepStrictEqual((yield* connection.query("SELECT $1::int4 AS n", [8])).rows, [{ n: 8 }]) |
| 433 | })) |
| 434 | |
| 435 | it.effect("preserves a stale-plan error in a transaction and reparses after rollback", () => |
| 436 | Effect.gen(function*() { |
| 437 | const connection = yield* makeConnection() |
| 438 | yield* connection.query("CREATE TEMP TABLE stale_in_transaction (a int4)") |
| 439 | yield* connection.query("INSERT INTO stale_in_transaction VALUES (1)") |
| 440 | yield* connection.query("SELECT * FROM stale_in_transaction") |
| 441 | yield* connection.query("ALTER TABLE stale_in_transaction ADD COLUMN b text DEFAULT 'added'") |
| 442 | yield* connection.query("BEGIN") |
| 443 | const error = yield* Effect.flip(connection.query("SELECT * FROM stale_in_transaction")) |
| 444 | assert.propertyVal(error.reason.cause, "code", "0A000") |
| 445 | yield* connection.query("ROLLBACK") |
| 446 | assert.deepStrictEqual((yield* connection.query("SELECT * FROM stale_in_transaction")).rows, [{ |
| 447 | a: 1, |
| 448 | b: "added" |
| 449 | }]) |
| 450 | })) |
| 451 | |
| 452 | it.effect("preserves a missing-statement error in a transaction and reparses after rollback", () => |
| 453 | Effect.gen(function*() { |
| 454 | const connection = yield* makeConnection() |
| 455 | yield* connection.query("SELECT $1::int4 AS n", [7]) |
| 456 | yield* connection.query("DEALLOCATE ALL") |
| 457 | yield* connection.query("BEGIN") |
| 458 | const error = yield* Effect.flip(connection.query("SELECT $1::int4 AS n", [8])) |
| 459 | assert.propertyVal(error.reason.cause, "code", "26000") |
| 460 | yield* connection.query("ROLLBACK") |
| 461 | assert.deepStrictEqual((yield* connection.query("SELECT $1::int4 AS n", [9])).rows, [{ n: 9 }]) |
| 462 | })) |
| 463 | |
| 464 | it.effect("closes statements it evicts from a full cache", () => |
| 465 | Effect.gen(function*() { |
| 466 | const connection = yield* makeConnection({ preparedStatementCacheSize: 3 }) |
| 467 | for (let round = 0; round < 2; round++) { |
| 468 | for (let index = 0; index < 8; index++) { |
| 469 | const result = yield* connection.query(`SELECT ${index}::int4 AS n, $1::text AS t`, ["v"]) |
| 470 | assert.deepStrictEqual(result.rows, [{ n: index, t: "v" }]) |
| 471 | } |
| 472 | } |
| 473 | const held = yield* connection.query("SELECT count(*)::int4 AS n FROM pg_prepared_statements") |
| 474 | assert.deepStrictEqual(held.rows[0], { n: 3 }) |
| 475 | })) |
| 476 | |
| 477 | it.effect("closes statements parsed while a pipeline overfills the cache", () => |
| 478 | Effect.gen(function*() { |
| 479 | const connection = yield* makeConnection({ preparedStatementCacheSize: 1, multiplex: true }) |
| 480 | yield* Effect.all( |
| 481 | Array.from({ length: 4 }, (_, index) => connection.query(`SELECT ${index}::int4 AS n`)), |
| 482 | { concurrency: "unbounded" } |
| 483 | ) |
| 484 | const held = yield* connection.query("SELECT count(*)::int4 AS n FROM pg_prepared_statements") |
| 485 | assert.deepStrictEqual(held.rows[0], { n: 1 }) |
| 486 | })) |
| 487 | |
| 488 | it.effect("parses every statement when prepare is off", () => |
| 489 | Effect.gen(function*() { |
| 490 | const connection = yield* makeConnection({ prepare: false }) |
| 491 | for (let index = 0; index < 3; index++) { |
| 492 | assert.deepStrictEqual((yield* connection.query("SELECT $1::int4 AS n", [index])).rows, [{ n: index }]) |
| 493 | } |
| 494 | const held = yield* connection.query("SELECT count(*)::int4 AS n FROM pg_prepared_statements") |
| 495 | assert.deepStrictEqual(held.rows[0], { n: 0 }) |
| 496 | })) |
| 497 | |
| 498 | it.effect("stays usable after a statement the server refuses to parse", () => |
| 499 | Effect.gen(function*() { |
| 500 | const connection = yield* makeConnection() |
| 501 | const missing = yield* Effect.flip(connection.query("SELECT * FROM does_not_exist")) |
| 502 | assert.strictEqual(missing.reason._tag, "SqlSyntaxError") |
| 503 | const syntax = yield* Effect.flip(connection.query("SELECT FROM WHERE ????")) |
| 504 | assert.strictEqual(syntax.reason._tag, "SqlSyntaxError") |
| 505 | assert.deepStrictEqual((yield* connection.query("SELECT 42::int4 AS answer")).rows, [{ answer: 42 }]) |
| 506 | })) |
| 507 | it.effect("closes a statement whose plan it had to throw away", () => |
| 508 | Effect.gen(function*() { |
| 509 | const connection = yield* makeConnection() |
| 510 | yield* connection.query("CREATE TEMP TABLE churn (a int)") |
| 511 | for (let round = 0; round < 4; round++) { |
| 512 | yield* connection.query("SELECT * FROM churn") |
| 513 | yield* connection.query(`ALTER TABLE churn ADD COLUMN c${round} text`) |
| 514 | } |
| 515 | yield* connection.query("SELECT * FROM churn") |
| 516 | // Every re-parse after a stale plan has to close the name it replaced, |
| 517 | // or each DDL change strands a statement on the backend. |
| 518 | const held = yield* connection.query( |
| 519 | "SELECT count(*)::int4 AS n FROM pg_prepared_statements WHERE statement = 'SELECT * FROM churn'" |
| 520 | ) |
| 521 | assert.deepStrictEqual(held.rows[0], { n: 1 }) |
| 522 | })) |
| 523 | it.effect("re-parses a statement first prepared in a rolled back transaction", () => |
| 524 | Effect.gen(function*() { |
| 525 | const connection = yield* makeConnection() |
| 526 | yield* connection.query("CREATE TEMP TABLE rolled_back (a int)") |
| 527 | |
| 528 | // Postgres drops a statement prepared inside a transaction it rolls |
| 529 | // back, so the cached name is gone even though the session lives on. |
| 530 | yield* connection.query("BEGIN") |
| 531 | yield* connection.query("INSERT INTO rolled_back VALUES ($1::int4)", [1]) |
| 532 | yield* connection.query("ROLLBACK") |
| 533 | |
| 534 | yield* connection.query("INSERT INTO rolled_back VALUES ($1::int4)", [2]) |
| 535 | yield* connection.query("INSERT INTO rolled_back VALUES ($1::int4)", [3]) |
| 536 | assert.deepStrictEqual( |
| 537 | (yield* connection.query("SELECT a FROM rolled_back ORDER BY a")).rows, |
| 538 | [{ a: 2 }, { a: 3 }] |
| 539 | ) |
| 540 | })) |
| 541 | it.effect("isolates errors between pipelined queries", () => |
| 542 | Effect.gen(function*() { |
| 543 | const connection = yield* makeConnection({ multiplex: true }) |
| 544 | const exits = yield* Effect.all([ |
| 545 | Effect.exit(connection.query("SELECT $1::int4 AS first", [1])), |
| 546 | Effect.exit(connection.query("SELECT missing_pipeline_column")), |
| 547 | Effect.exit(connection.query("SELECT $1::int4 AS third", [3])) |
| 548 | ], { concurrency: "unbounded" }) |
| 549 | |
| 550 | assert.strictEqual(exits[0]._tag, "Success") |
| 551 | assert.strictEqual(exits[1]._tag, "Failure") |
| 552 | assert.strictEqual(exits[2]._tag, "Success") |
| 553 | if (exits[0]._tag === "Success") assert.deepStrictEqual(exits[0].value.rows, [{ first: 1 }]) |
| 554 | if (exits[2]._tag === "Success") assert.deepStrictEqual(exits[2].value.rows, [{ third: 3 }]) |
| 555 | })) |
| 556 | |
| 557 | it.effect("drains an interrupted pipelined query without cancelling its neighbors", () => |
| 558 | Effect.gen(function*() { |
| 559 | const connection = yield* makeConnection({ multiplex: true }) |
| 560 | const first = yield* connection.query("SELECT pg_sleep(0.05), 1 AS first").pipe(Effect.forkScoped) |
| 561 | const second = yield* connection.query("SELECT 2 AS second").pipe(Effect.forkScoped) |
| 562 | yield* Effect.yieldNow |
| 563 | yield* Fiber.interrupt(first) |
| 564 | |
| 565 | const result = yield* Fiber.join(second) |
| 566 | assert.deepStrictEqual(result.rows, [{ second: 2 }]) |
| 567 | assert.deepStrictEqual((yield* connection.query("SELECT 3 AS after")).rows, [{ after: 3 }]) |
| 568 | })) |
| 569 | |
| 570 | it.effect("places pin requests between earlier and later pipelined queries", () => |
| 571 | Effect.gen(function*() { |
| 572 | const connection = yield* makeConnection({ multiplex: true }) |
| 573 | yield* connection.query( |
| 574 | "CREATE TEMP TABLE pipeline_pin_order (position int GENERATED ALWAYS AS IDENTITY, value int4)" |
| 575 | ) |
| 576 | const first = yield* connection.query( |
| 577 | "INSERT INTO pipeline_pin_order (value) SELECT 1 FROM pg_sleep(0.05)" |
| 578 | ).pipe(Effect.forkScoped) |
| 579 | yield* Effect.yieldNow |
| 580 | |
| 581 | const pinned = yield* Deferred.make<void>() |
| 582 | const release = yield* Deferred.make<void>() |
| 583 | const pinFiber = yield* Effect.scoped(Effect.gen(function*() { |
| 584 | const exclusive = yield* connection.pin |
| 585 | yield* exclusive.query("INSERT INTO pipeline_pin_order (value) VALUES (2)") |
| 586 | yield* Deferred.succeed(pinned, undefined) |
| 587 | yield* Deferred.await(release) |
| 588 | })).pipe(Effect.forkScoped) |
| 589 | yield* Effect.yieldNow |
| 590 | const third = yield* connection.query("INSERT INTO pipeline_pin_order (value) VALUES (3)").pipe( |
| 591 | Effect.forkScoped |
| 592 | ) |
| 593 | |
| 594 | yield* Deferred.await(pinned) |
| 595 | assert.isUndefined(third.pollUnsafe()) |
| 596 | yield* Deferred.succeed(release, undefined) |
| 597 | yield* Fiber.join(first) |
| 598 | yield* Fiber.join(pinFiber) |
| 599 | yield* Fiber.join(third) |
| 600 | |
| 601 | const result = yield* connection.query("SELECT value FROM pipeline_pin_order ORDER BY position") |
| 602 | assert.deepStrictEqual(result.rows, [{ value: 1 }, { value: 2 }, { value: 3 }]) |
| 603 | })) |
| 604 | |
| 605 | it.effect("reuses prepared statements across a pipeline", () => |
| 606 | Effect.gen(function*() { |
| 607 | const connection = yield* makeConnection({ multiplex: true }) |
| 608 | for (let round = 0; round < 3; round++) { |
| 609 | const results = yield* Effect.all( |
| 610 | Array.from({ length: 8 }, (_, index) => connection.query("SELECT $1::int4 AS n", [index])), |
| 611 | { concurrency: "unbounded" } |
| 612 | ) |
| 613 | results.forEach((result, index) => assert.deepStrictEqual(result.rows, [{ n: index }])) |
| 614 | } |
| 615 | const held = yield* connection.query( |
| 616 | "SELECT count(*)::int4 AS n FROM pg_prepared_statements WHERE statement = 'SELECT $1::int4 AS n'" |
| 617 | ) |
| 618 | assert.deepStrictEqual(held.rows[0], { n: 1 }) |
| 619 | })) |
| 620 | it.effect("orders ordinary statements against a stream on the same session", () => |
| 621 | Effect.gen(function*() { |
| 622 | const connection = yield* makeConnection() |
| 623 | // An ordinary statement holds `owner` rather than the wire permit, so |
| 624 | // this is what says it still cannot interleave with a stream, which |
| 625 | // reaches the session through a pin. |
| 626 | const streamed = yield* Effect.forkScoped( |
| 627 | Stream.runCollect(connection.stream("SELECT generate_series(1, 200) AS n")) |
| 628 | ) |
| 629 | const queried = yield* Effect.forkScoped( |
| 630 | Effect.all( |
| 631 | Array.from({ length: 12 }, (_, index) => connection.query("SELECT $1::int4 AS n", [index])), |
| 632 | { concurrency: "unbounded" } |
| 633 | ) |
| 634 | ) |
| 635 | const rows = yield* Fiber.join(streamed) |
| 636 | assert.strictEqual(Array.from(rows).length, 200) |
| 637 | const results = yield* Fiber.join(queried) |
| 638 | results.forEach((result, index) => assert.deepStrictEqual(result.rows, [{ n: index }])) |
| 639 | assert.deepStrictEqual((yield* connection.query("SELECT 1 AS ok")).rows, [{ ok: 1 }]) |
| 640 | })) |
| 641 | |
| 642 | it.effect("fails work through a released pin without touching the next owner's session", () => |
| 643 | Effect.gen(function*() { |
| 644 | const connection = yield* makeConnection() |
| 645 | const released = yield* Effect.scoped(connection.pin) |
| 646 | yield* Effect.scoped(Effect.gen(function*() { |
| 647 | const owner = yield* connection.pin |
| 648 | yield* owner.query("BEGIN") |
| 649 | yield* owner.query("SET LOCAL application_name = 'next_owner'") |
| 650 | |
| 651 | const exits: ReadonlyArray<Exit.Exit<unknown, SqlError>> = [ |
| 652 | yield* Effect.exit(released.query("ROLLBACK")), |
| 653 | yield* Effect.exit(released.queryValues("ROLLBACK")), |
| 654 | yield* Effect.exit(Stream.runCollect(released.stream("SELECT 1"))), |
| 655 | yield* Effect.exit(Effect.scoped(released.listen("released_pin"))) |
| 656 | ] |
| 657 | exits.forEach((exit) => assert.isTrue(Exit.isFailure(exit))) |
| 658 | |
| 659 | const setting = yield* owner.query("SELECT current_setting('application_name') AS name") |
| 660 | assert.deepStrictEqual(setting.rows, [{ name: "next_owner" }]) |
| 661 | yield* owner.query("ROLLBACK") |
| 662 | })) |
| 663 | })) |
| 664 | |
| 665 | it.effect("fails a statement that was queued on a pin before its release", () => |
| 666 | Effect.gen(function*() { |
| 667 | const connection = yield* makeConnection() |
| 668 | const scope = yield* Scope.make() |
| 669 | const pinned = yield* Scope.provide(connection.pin, scope) |
| 670 | const queued = yield* Effect.forkScoped(Effect.andThen(Effect.yieldNow, pinned.query("SELECT 1 AS queued"))) |
| 671 | // `queued` waits for the wire behind this statement, and the pin is |
| 672 | // released before it wakes, so the session is idle and handed on. |
| 673 | yield* pinned.query("SELECT 1 AS holder") |
| 674 | yield* Scope.close(scope, Exit.void) |
| 675 | assert.isTrue(Exit.isFailure(yield* Fiber.await(queued))) |
| 676 | assert.deepStrictEqual((yield* connection.query("SELECT 1 AS ok")).rows, [{ ok: 1 }]) |
| 677 | })) |
| 678 | |
| 679 | it.effect("ignores interrupt on a released pin", () => |
| 680 | Effect.gen(function*() { |
| 681 | const connection = yield* makeConnection() |
| 682 | const blocker = yield* makeConnection() |
| 683 | const observer = yield* makeConnection() |
| 684 | const released = yield* Effect.scoped(connection.pin) |
| 685 | const waiting = yield* Effect.gen(function*() { |
| 686 | yield* blocker.query("BEGIN") |
| 687 | yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId]) |
| 688 | const waiting = yield* Effect.forkScoped( |
| 689 | connection.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId]) |
| 690 | ) |
| 691 | // A cancel only lands while the backend is executing, so wait for the lock. |
| 692 | yield* waitUntilAdvisoryLock(observer, connection.processId) |
| 693 | yield* released.interrupt |
| 694 | return waiting |
| 695 | }).pipe(Effect.ensuring(Effect.ignore(blocker.query("ROLLBACK")))) |
| 696 | assert.isTrue(Exit.isSuccess(yield* Fiber.await(waiting))) |
| 697 | })) |
| 698 | }) |