@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 724 lines 30.9 KB
Raw
1 import { PgConnection, PgPool, PgTypes } from "@effect/sql-pg"
2 import { assert, it } from "@effect/vitest"
3 import { Effect, Exit, Fiber, Latch, Queue, Redacted, Result, Scope, Stream } from "effect"
4 import * as Net from "node:net"
5 import { Duplex } from "node:stream"
6 import { PgContainer, realSleep, waitUntilActive, waitUntilAdvisoryLock } from "./utils.ts"
7
8 const cancellationTestTimeout = 20_000
9
10 const poolConfig = Effect.gen(function*() {
11 const container = yield* PgContainer
12 return { url: Redacted.make(container.getConnectionUri()) }
13 })
14
15 const cancelRequestCode = 80877102
16
17 /** Holds `CancelRequest` messages until the test releases them. */
18 class LateCancelProxy extends Duplex {
19 private readonly backend: Net.Socket
20 private cancelDeferred = false
21
22 constructor(host: string, port: number, private readonly gate: CancelRequestGate) {
23 super()
24 this.backend = Net.connect({ host, port, noDelay: true })
25 this.backend.on("data", (chunk: Buffer) => {
26 if (!this.push(chunk)) this.backend.pause()
27 })
28 this.backend.on("end", () => this.push(null))
29 this.backend.on("error", (error) => this.destroy(error))
30 this.backend.on("close", () => {
31 if (this.cancelDeferred) this.gate.markDelivered()
32 else this.destroy()
33 })
34 }
35
36 override _read(): void {
37 this.backend.resume()
38 }
39
40 override _write(chunk: Buffer, _encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
41 if (chunk.length === 16 && chunk.readInt32BE(0) === 16 && chunk.readInt32BE(4) === cancelRequestCode) {
42 this.cancelDeferred = true
43 const backend = this.backend
44 this.gate.intercept(() => backend.end(chunk))
45 callback()
46 this.destroy()
47 return
48 }
49 this.backend.write(chunk, callback)
50 }
51
52 override _final(callback: (error?: Error | null) => void): void {
53 this.backend.end(callback)
54 }
55
56 override _destroy(error: Error | null, callback: (error?: Error | null) => void): void {
57 if (!this.cancelDeferred) this.backend.destroy()
58 callback(error)
59 }
60 }
61
62 class CancelRequestGate {
63 private readonly interceptedPromise: Promise<void>
64 private readonly deliveredPromise: Promise<void>
65 private resolveIntercepted!: () => void
66 private resolveDelivered!: () => void
67 private forward: (() => void) | undefined
68 private released = false
69
70 constructor() {
71 this.interceptedPromise = new Promise((resolve) => {
72 this.resolveIntercepted = resolve
73 })
74 this.deliveredPromise = new Promise((resolve) => {
75 this.resolveDelivered = resolve
76 })
77 }
78
79 get intercepted() {
80 return Effect.promise(() => this.interceptedPromise).pipe(Effect.timeout("5 seconds"))
81 }
82
83 get delivered() {
84 return Effect.promise(() => this.deliveredPromise).pipe(Effect.timeout("5 seconds"))
85 }
86
87 intercept(forward: () => void): void {
88 this.forward = forward
89 this.resolveIntercepted()
90 if (this.released) this.release()
91 }
92
93 release(): void {
94 this.released = true
95 const forward = this.forward
96 this.forward = undefined
97 forward?.()
98 }
99
100 markDelivered(): void {
101 this.resolveDelivered()
102 }
103 }
104
105 const lateCancelPoolConfig = Effect.fnUntraced(function*(gate: CancelRequestGate) {
106 const container = yield* PgContainer
107 const host = container.getHost()
108 const port = container.getMappedPort(5432)
109 return {
110 ...(yield* poolConfig),
111 maxConnections: 1,
112 stream: () => new LateCancelProxy(host, port, gate)
113 }
114 })
115
116 const blockedStream = `
117 SELECT n FROM generate_series(1, 1000) AS g(n)
118 UNION ALL
119 SELECT 1001::int4 AS n
120 FROM (SELECT pg_advisory_xact_lock($1::int4)) AS blocked
121 `
122
123 it.layer(PgContainer.layer, { timeout: "30 seconds", concurrent: false })("PgPool", (it) => {
124 it.effect("reuses checked out connections", () =>
125 Effect.gen(function*() {
126 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 1 })
127 const checkout = Effect.scoped(Effect.gen(function*() {
128 const connection = yield* pool.get
129 const result = yield* connection.query("SELECT $1::int4 AS one", [1])
130 assert.deepStrictEqual(result.rows, [{ one: 1 }])
131 return connection.processId
132 }))
133 const first = yield* checkout
134 const second = yield* checkout
135 assert.strictEqual(first, second)
136 }))
137
138 it.effect("uses a connection once when its TTL is zero", () =>
139 Effect.gen(function*() {
140 const pool = yield* PgPool.make({ ...(yield* poolConfig), connectionTTL: 0, maxConnections: 1 })
141 const checkout = Effect.scoped(Effect.map(pool.get, (connection) => connection.processId)).pipe(
142 Effect.timeout("1 second")
143 )
144 const first = yield* checkout
145 const second = yield* checkout
146 assert.notStrictEqual(first, second)
147 }))
148
149 it.effect("streams rows incrementally and cancels on early abort", () =>
150 Effect.gen(function*() {
151 const pool = yield* PgPool.make(yield* poolConfig)
152 const connection = yield* pool.get
153 const rows = yield* Stream.runCollect(
154 connection.stream("SELECT n FROM generate_series(1, $1::int4) AS g(n)", [1000])
155 )
156 assert.strictEqual(rows.length, 1000)
157 assert.deepStrictEqual(rows[0], { n: 1 })
158 assert.deepStrictEqual(rows[999], { n: 1000 })
159
160 const aborted = yield* Stream.runCollect(
161 connection.stream("SELECT n FROM generate_series(1, 5000) AS g(n)").pipe(Stream.take(5))
162 )
163 assert.deepStrictEqual(aborted, [{ n: 1 }, { n: 2 }, { n: 3 }, { n: 4 }, { n: 5 }])
164
165 const result = yield* connection.query("SELECT pg_backend_pid()::int4 AS pid, $1::text AS after", ["ok"])
166 assert.deepStrictEqual(result.rows, [{ pid: connection.processId, after: "ok" }])
167 }), cancellationTestTimeout)
168
169 it.effect("acquires a multiplexed listener after registration", () =>
170 Effect.gen(function*() {
171 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 2, multiplex: true })
172 const listener = yield* pool.reserve
173 const notifications = yield* listener.listen("test_channel")
174 const notifier = yield* pool.get
175 assert.notStrictEqual(listener.processId, notifier.processId)
176 yield* notifier.query("SELECT pg_notify($1, $2)", ["test_channel", "hello"])
177 const notification = yield* Queue.take(notifications)
178 assert.strictEqual(notification.channel, "test_channel")
179 assert.strictEqual(notification.payload, "hello")
180 assert.strictEqual(notification.processId, notifier.processId)
181 }))
182
183 it.effect("does not hand on a listener registered through a released pin", () =>
184 Effect.gen(function*() {
185 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 1 })
186 const pinScope = yield* Scope.make()
187 const listenScope = yield* Scope.make()
188 const pinned = yield* Scope.provide(pool.reserve, pinScope)
189 yield* Scope.provide(pinned.listen("released_pin_listener"), listenScope)
190 yield* Scope.close(pinScope, Exit.void)
191 yield* Scope.close(listenScope, Exit.void)
192
193 const channels = yield* Effect.scoped(
194 Effect.flatMap(pool.reserve, (connection) => connection.query("SELECT pg_listening_channels() AS channel"))
195 )
196 assert.deepStrictEqual(channels.rows, [])
197 }))
198
199 it.effect("reports listener registration failure during acquisition", () =>
200 Effect.gen(function*() {
201 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 1 })
202 const connection = yield* pool.get
203 yield* connection.query("BEGIN")
204 yield* Effect.flip(connection.query("SELECT * FROM effect_missing_relation"))
205
206 const error = yield* Effect.flip(connection.listen("test_channel"))
207 assert.strictEqual(error._tag, "SqlError")
208
209 // A failed acquisition must release the connection pin.
210 yield* connection.query("ROLLBACK")
211 const result = yield* connection.query("SELECT 1 AS one")
212 assert.deepStrictEqual(result.rows, [{ one: 1 }])
213 }))
214
215 it.effect("preserves the server error when a listener backend is terminated", () =>
216 Effect.gen(function*() {
217 const config = yield* poolConfig
218 const pool = yield* PgPool.make({ ...config, maxConnections: 1, multiplex: true })
219 const listener = yield* pool.reserve
220 const notifications = yield* listener.listen("terminated_listener")
221 const consumer = yield* Effect.forkScoped(Queue.take(notifications))
222 const terminator = yield* PgConnection.make(config)
223
224 yield* terminator.query("SELECT pg_terminate_backend($1)", [listener.processId])
225 const error = yield* Effect.flip(Fiber.join(consumer))
226 assert.strictEqual(error._tag, "SqlError")
227 assert.propertyVal(error.reason.cause, "code", "57P01")
228 assert.strictEqual(yield* Effect.flip(listener.query("SELECT 1")), error)
229 }))
230
231 it.effect("returns a multiplexed reservation to shared circulation", () =>
232 Effect.gen(function*() {
233 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 1, multiplex: true })
234 const reserve = Effect.scoped(Effect.map(pool.reserve, (connection) => connection.processId))
235 const get = Effect.scoped(Effect.map(pool.get, (connection) => connection.processId))
236
237 const first = yield* reserve
238 assert.strictEqual(yield* get, first)
239 assert.strictEqual(yield* reserve, first)
240 assert.strictEqual(yield* get, first)
241 }))
242
243 it.effect("interrupt cancels an in-flight query", () =>
244 Effect.gen(function*() {
245 const config = yield* poolConfig
246 const pool = yield* PgPool.make({ ...config, maxConnections: 1 })
247 const blocker = yield* PgConnection.make(config)
248 const observer = yield* PgConnection.make(config)
249 // Roll back inside the scope, before it interrupts/joins the query fiber on failure.
250 const first = yield* Effect.scoped(
251 Effect.gen(function*() {
252 const connection = yield* pool.get
253 yield* blocker.query("BEGIN")
254 yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
255 const fiber = yield* Effect.forkScoped(
256 connection.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
257 )
258 yield* waitUntilAdvisoryLock(observer, connection.processId)
259 yield* connection.interrupt
260 const error = yield* Effect.flip(Fiber.join(fiber))
261 assert.strictEqual(error._tag, "SqlError")
262 assert.strictEqual(error.reason._tag, "StatementTimeoutError")
263 return connection.processId
264 }).pipe(Effect.ensuring(Effect.ignore(blocker.query("ROLLBACK"))))
265 )
266
267 const second = yield* Effect.scoped(Effect.map(pool.get, (connection) => connection.processId))
268 assert.strictEqual(second, first)
269 }), cancellationTestTimeout)
270
271 it.effect("replaces a session whose pin is released with a statement in flight", () =>
272 Effect.gen(function*() {
273 const config = yield* poolConfig
274 const pool = yield* PgPool.make({ ...config, maxConnections: 1 })
275 const blocker = yield* PgConnection.make(config)
276 const observer = yield* PgConnection.make(config)
277 const scope = yield* Scope.make()
278 const pinned = yield* Scope.provide(pool.reserve, scope)
279
280 yield* Effect.gen(function*() {
281 yield* blocker.query("BEGIN")
282 yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [pinned.processId])
283 const leaked = yield* Effect.forkScoped(
284 pinned.query("SELECT pg_advisory_xact_lock($1::int4)", [pinned.processId])
285 )
286 yield* waitUntilAdvisoryLock(observer, pinned.processId)
287 yield* Scope.close(scope, Exit.void)
288 // Freeing the lock lets the leaked statement finish if it still has a session.
289 yield* blocker.query("ROLLBACK")
290 assert.isTrue(Exit.isFailure(yield* Fiber.await(leaked)))
291 }).pipe(Effect.ensuring(Effect.ignore(blocker.query("ROLLBACK"))))
292
293 const next = yield* Effect.scoped(Effect.gen(function*() {
294 const connection = yield* pool.reserve
295 const result = yield* connection.query("SELECT 1 AS ok")
296 return { processId: connection.processId, rows: result.rows }
297 }))
298 assert.notStrictEqual(next.processId, pinned.processId)
299 assert.deepStrictEqual(next.rows, [{ ok: 1 }])
300 }), cancellationTestTimeout)
301
302 it.effect("replaces a pooled session after an unconfirmed query cancel", () =>
303 Effect.gen(function*() {
304 const config = yield* poolConfig
305 const gate = new CancelRequestGate()
306 const pool = yield* PgPool.make(yield* lateCancelPoolConfig(gate))
307 const blocker = yield* PgConnection.make(config)
308 const observer = yield* PgConnection.make(config)
309
310 yield* Effect.gen(function*() {
311 const first = yield* Effect.scoped(
312 Effect.gen(function*() {
313 const connection = yield* pool.get
314 yield* blocker.query("BEGIN")
315 yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
316 const query = yield* Effect.forkScoped(
317 connection.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
318 )
319 yield* waitUntilAdvisoryLock(observer, connection.processId)
320 const interruption = yield* Effect.forkScoped(Fiber.interrupt(query))
321 yield* gate.intercepted
322 yield* blocker.query("COMMIT")
323 yield* Fiber.join(interruption)
324 return connection.processId
325 }).pipe(Effect.ensuring(Effect.ignore(blocker.query("ROLLBACK"))))
326 )
327
328 const second = yield* Effect.scoped(Effect.map(pool.get, (connection) => connection.processId))
329 assert.notStrictEqual(second, first)
330 gate.release()
331 yield* gate.delivered
332 }).pipe(Effect.ensuring(Effect.sync(() => gate.release())))
333 }), cancellationTestTimeout)
334
335 it.effect("replaces a session before pool.use after an unconfirmed query cancel", () =>
336 Effect.gen(function*() {
337 const config = yield* poolConfig
338 const gate = new CancelRequestGate()
339 const pool = yield* PgPool.make(yield* lateCancelPoolConfig(gate))
340 const blocker = yield* PgConnection.make(config)
341 const observer = yield* PgConnection.make(config)
342
343 yield* Effect.gen(function*() {
344 const first = yield* Effect.scoped(
345 Effect.gen(function*() {
346 const connection = yield* pool.get
347 yield* blocker.query("BEGIN")
348 yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
349 const query = yield* Effect.forkScoped(
350 connection.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
351 )
352 yield* waitUntilAdvisoryLock(observer, connection.processId)
353 const interruption = yield* Effect.forkScoped(Fiber.interrupt(query))
354 yield* gate.intercepted
355 yield* blocker.query("COMMIT")
356 yield* Fiber.join(interruption)
357 return connection.processId
358 }).pipe(Effect.ensuring(Effect.ignore(blocker.query("ROLLBACK"))))
359 )
360
361 const second = yield* pool.use((connection) => Effect.succeed(connection.processId))
362 assert.notStrictEqual(second, first)
363 gate.release()
364 yield* gate.delivered
365 }).pipe(Effect.ensuring(Effect.sync(() => gate.release())))
366 }), cancellationTestTimeout)
367
368 it.effect("protects an active pool.use query from a late cancel", () =>
369 Effect.gen(function*() {
370 const config = yield* poolConfig
371 const gate = new CancelRequestGate()
372 const pool = yield* PgPool.make(yield* lateCancelPoolConfig(gate))
373 const blocker = yield* PgConnection.make(config)
374 const observer = yield* PgConnection.make(config)
375 const started = yield* Queue.unbounded<number>()
376 const lockAcquired = yield* Queue.unbounded<void>()
377
378 yield* Effect.gen(function*() {
379 yield* Effect.scoped(
380 Effect.gen(function*() {
381 const connection = yield* pool.get
382 yield* blocker.query("BEGIN")
383 yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
384 const query = yield* Effect.forkScoped(
385 connection.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
386 )
387 yield* waitUntilAdvisoryLock(observer, connection.processId)
388 const interruption = yield* Effect.forkScoped(Fiber.interrupt(query))
389 yield* gate.intercepted
390 yield* blocker.query("COMMIT")
391 yield* Fiber.join(interruption)
392 }).pipe(Effect.ensuring(Effect.ignore(blocker.query("ROLLBACK"))))
393 )
394
395 const followUp = yield* Effect.forkScoped(
396 pool.use((connection) =>
397 Effect.gen(function*() {
398 yield* Queue.offer(started, connection.processId)
399 yield* Queue.take(lockAcquired)
400 return yield* connection.query(
401 "SELECT 1 AS after FROM pg_advisory_xact_lock($1::int4)",
402 [connection.processId]
403 )
404 })
405 )
406 )
407 const followUpPid = yield* Queue.take(started)
408 yield* blocker.query("BEGIN")
409 yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [followUpPid])
410 yield* Queue.offer(lockAcquired, undefined)
411 yield* waitUntilAdvisoryLock(observer, followUpPid)
412 gate.release()
413 yield* gate.delivered
414 yield* blocker.query("COMMIT")
415 const result = yield* Fiber.join(followUp)
416 assert.deepStrictEqual(result.rows, [{ after: 1 }])
417 }).pipe(
418 Effect.ensuring(
419 Effect.sync(() => gate.release()).pipe(
420 Effect.andThen(blocker.query("ROLLBACK")),
421 Effect.ignore
422 )
423 )
424 )
425 }), cancellationTestTimeout)
426
427 it.effect("retires an idle interrupted session before pool.use", () =>
428 Effect.gen(function*() {
429 const config = yield* poolConfig
430 const gate = new CancelRequestGate()
431 const pool = yield* PgPool.make(yield* lateCancelPoolConfig(gate))
432 const observer = yield* PgConnection.make(config)
433 const started = yield* Queue.unbounded<number>()
434
435 yield* Effect.gen(function*() {
436 const first = yield* Effect.scoped(Effect.gen(function*() {
437 const connection = yield* pool.get
438 yield* connection.interrupt
439 yield* gate.intercepted
440 return connection.processId
441 }))
442
443 const followUp = yield* Effect.forkScoped(
444 pool.use((connection) =>
445 Effect.gen(function*() {
446 yield* Queue.offer(started, connection.processId)
447 return yield* connection.query("SELECT 1 AS after FROM pg_sleep(1)")
448 })
449 )
450 )
451 const followUpPid = yield* Queue.take(started)
452 assert.notStrictEqual(followUpPid, first)
453 yield* waitUntilActive(observer, followUpPid)
454 gate.release()
455 yield* gate.delivered
456 const result = yield* Fiber.join(followUp)
457 assert.deepStrictEqual(result.rows, [{ after: 1 }])
458 }).pipe(Effect.ensuring(Effect.sync(() => gate.release())))
459 }), cancellationTestTimeout)
460
461 it.effect("replaces a pooled session after an unconfirmed stream cancel", () =>
462 Effect.gen(function*() {
463 const config = yield* poolConfig
464 const gate = new CancelRequestGate()
465 const pool = yield* PgPool.make(yield* lateCancelPoolConfig(gate))
466 const blocker = yield* PgConnection.make(config)
467
468 yield* Effect.gen(function*() {
469 const first = yield* Effect.scoped(
470 Effect.gen(function*() {
471 const connection = yield* pool.get
472 yield* blocker.query("BEGIN")
473 yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
474 const stream = yield* Effect.forkScoped(
475 connection.stream(blockedStream, [connection.processId]).pipe(Stream.take(1), Stream.runCollect)
476 )
477 yield* gate.intercepted
478 yield* blocker.query("COMMIT")
479 const rows = yield* Fiber.join(stream)
480 assert.deepStrictEqual(rows, [{ n: 1 }])
481 return connection.processId
482 }).pipe(Effect.ensuring(Effect.ignore(blocker.query("ROLLBACK"))))
483 )
484
485 yield* Effect.scoped(Effect.gen(function*() {
486 const replacement = yield* pool.get
487 assert.notStrictEqual(replacement.processId, first)
488 const query = yield* Effect.forkScoped(replacement.query("SELECT 1 AS after FROM pg_sleep(1)"))
489 yield* waitUntilActive(blocker, replacement.processId)
490 gate.release()
491 yield* gate.delivered
492 const result = yield* Fiber.join(query)
493 assert.deepStrictEqual(result.rows, [{ after: 1 }])
494 }))
495 }).pipe(Effect.ensuring(Effect.sync(() => gate.release())))
496 }), cancellationTestTimeout)
497
498 it.effect("keeps a session after a confirmed stream cancel", () =>
499 Effect.gen(function*() {
500 const config = yield* poolConfig
501 const pool = yield* PgPool.make({ ...config, maxConnections: 1 })
502 const blocker = yield* PgConnection.make(config)
503 const connection = yield* pool.get
504 yield* blocker.query("BEGIN")
505 yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
506 const rows = yield* connection.stream(blockedStream, [connection.processId]).pipe(
507 Stream.take(1),
508 Stream.runCollect,
509 Effect.timeout("5 seconds")
510 )
511 assert.deepStrictEqual(rows, [{ n: 1 }])
512 const result = yield* connection.query("SELECT pg_backend_pid()::int4 AS pid")
513 assert.deepStrictEqual(result.rows, [{ pid: connection.processId }])
514 yield* blocker.query("ROLLBACK")
515 }), cancellationTestTimeout)
516
517 it.effect("keeps a held session after an unconfirmed query cancel", () =>
518 Effect.gen(function*() {
519 const config = yield* poolConfig
520 const gate = new CancelRequestGate()
521 const pool = yield* PgPool.make(yield* lateCancelPoolConfig(gate))
522 const blocker = yield* PgConnection.make(config)
523 const observer = yield* PgConnection.make(config)
524
525 yield* Effect.gen(function*() {
526 const connection = yield* pool.get
527 yield* blocker.query("BEGIN")
528 yield* blocker.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
529 const query = yield* Effect.forkScoped(
530 connection.query("SELECT pg_advisory_xact_lock($1::int4)", [connection.processId])
531 )
532 yield* waitUntilAdvisoryLock(observer, connection.processId)
533 const interruption = yield* Effect.forkScoped(Fiber.interrupt(query))
534 yield* gate.intercepted
535 yield* blocker.query("COMMIT")
536 yield* Fiber.join(interruption)
537
538 const beforeDelivery = yield* connection.query("SELECT pg_backend_pid()::int4 AS pid")
539 assert.deepStrictEqual(beforeDelivery.rows, [{ pid: connection.processId }])
540 gate.release()
541 yield* gate.delivered
542 const afterDelivery = yield* connection.query("SELECT pg_backend_pid()::int4 AS pid")
543 assert.deepStrictEqual(afterDelivery.rows, [{ pid: connection.processId }])
544 }).pipe(
545 Effect.ensuring(
546 Effect.sync(() => gate.release()).pipe(
547 Effect.andThen(blocker.query("ROLLBACK")),
548 Effect.ignore
549 )
550 )
551 )
552 }), cancellationTestTimeout)
553
554 it.effect("replaces connections that die", () =>
555 Effect.gen(function*() {
556 const config = yield* poolConfig
557 const pool = yield* PgPool.make({ ...config, maxConnections: 1 })
558 const first = yield* Effect.scoped(Effect.map(pool.get, (connection) => connection.processId))
559 const terminator = yield* PgConnection.make(config)
560 yield* terminator.query("SELECT pg_terminate_backend($1)", [first])
561 // The pool notices the dead connection asynchronously; retry until it
562 // hands out a healthy replacement.
563 while (true) {
564 const replaced = yield* Effect.scoped(Effect.gen(function*() {
565 const connection = yield* pool.get
566 const alive = yield* Effect.isSuccess(connection.query("SELECT 1 AS one"))
567 return alive && connection.processId !== first ? connection.processId : undefined
568 }))
569 if (replaced !== undefined) {
570 assert.notStrictEqual(replaced, first)
571 break
572 }
573 yield* realSleep
574 }
575 }))
576 it.effect("defaults multiplex concurrency to 32", () =>
577 Effect.gen(function*() {
578 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 4, multiplex: true })
579 const processIds = yield* Effect.all(
580 Array.from({ length: 32 }, () =>
581 Effect.scoped(Effect.flatMap(pool.get, (connection) =>
582 Effect.as(
583 connection.query("SELECT pg_sleep(0.05)"),
584 connection.processId
585 )))),
586 { concurrency: "unbounded" }
587 )
588 assert.strictEqual(new Set(processIds).size, 1)
589 }))
590 it.effect("invalidates a reserved connection", () =>
591 Effect.gen(function*() {
592 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 2, multiplex: true })
593 const reserved = yield* pool.reserve
594 yield* pool.invalidate(reserved)
595 const replacement = yield* pool.get
596 assert.notStrictEqual(replacement.processId, reserved.processId)
597 }))
598
599 it.effect("keeps a multiplexed stream's connection to itself", () =>
600 Effect.gen(function*() {
601 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 2, multiplex: true })
602 const connection = yield* pool.get
603 // A stream pins its connection for its lifetime. A checkout that landed
604 // on the same one would wait behind a stream only it could drain.
605 const doubled = yield* connection.stream("SELECT generate_series(1, 5) AS n").pipe(
606 Stream.mapEffect((row) =>
607 Effect.flatMap(pool.get, (other) => other.query("SELECT $1::int4 AS d", [(row as any).n * 2]))
608 ),
609 Stream.map((result) => (result.rows[0] as any).d),
610 Stream.runCollect
611 )
612 assert.deepStrictEqual(Array.from(doubled), [2, 4, 6, 8, 10])
613 }))
614 it.effect("admits a waiting checkout when a reserved connection dies", () =>
615 Effect.gen(function*() {
616 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 1, multiplex: true })
617 const reserved = yield* pool.reserve
618 const waiter = yield* Effect.forkScoped(
619 Effect.scoped(Effect.flatMap(pool.get, (connection) => connection.query("SELECT 1 AS ok")))
620 )
621 yield* realSleep
622
623 // The reservation is still held when its connection dies, so nothing
624 // returns the connection to the pool. The waiter behind it would queue
625 // for a connection that is never coming back.
626 yield* Effect.ignore(reserved.query("SELECT pg_terminate_backend(pg_backend_pid())"))
627
628 const result = yield* Fiber.join(waiter)
629 assert.deepStrictEqual(result.rows, [{ ok: 1 }])
630 }))
631 it.effect("returns a borrowed session on success, failure, and interruption", () =>
632 Effect.gen(function*() {
633 // A pool of one: a lease that is not returned hangs the next borrow.
634 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 1 })
635 const borrow = <A, E>(effect: (connection: PgConnection.PgConnection) => Effect.Effect<A, E>) => pool.use(effect)
636
637 assert.deepStrictEqual(
638 (yield* borrow((connection) => connection.query("SELECT 1 AS ok"))).rows,
639 [{ ok: 1 }]
640 )
641
642 const failed = yield* Effect.result(borrow((connection) => connection.query("SELECT * FROM nope")))
643 assert.strictEqual(failed._tag, "Failure")
644
645 const running = yield* Effect.forkScoped(borrow((connection) => connection.query("SELECT pg_sleep(30)")))
646 yield* realSleep
647 yield* Fiber.interrupt(running)
648
649 assert.deepStrictEqual(
650 (yield* borrow((connection) => connection.query("SELECT 2 AS ok"))).rows,
651 [{ ok: 2 }]
652 )
653 }), cancellationTestTimeout)
654
655 it.effect("replaces a fatal session before the next pool.use query", () =>
656 Effect.gen(function*() {
657 const failure = Result.fail(new PgTypes.CodecError({ message: "Intentional decode failure" }))
658 const types = PgTypes.makeRegistry()
659 types.register(PgTypes.OID.interval, { encode: () => failure, decode: () => failure })
660 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 1, types })
661 const first = yield* pool.use((connection) => Effect.succeed(connection.processId))
662
663 // The failing codec errors fatally in the connection's data handler.
664 // The next borrow must not see the dead session, even in the same fiber.
665 const error = yield* Effect.flip(pool.use((connection) => connection.query("SELECT interval '-1 day' AS v")))
666 assert.strictEqual(error._tag, "SqlError")
667 assert.strictEqual(error.reason.message, "PgConnection: Failed to decode row")
668 const next = yield* pool.use((connection) =>
669 Effect.map(connection.query("SELECT 1::int4 AS one"), (result) => ({
670 pid: connection.processId,
671 rows: result.rows
672 }))
673 )
674 assert.notStrictEqual(next.pid, first)
675 assert.deepStrictEqual(next.rows, [{ one: 1 }])
676 }))
677
678 it.effect("borrows around a connection that has to be replaced", () =>
679 Effect.gen(function*() {
680 const pool = yield* PgPool.make({ ...(yield* poolConfig), maxConnections: 1 })
681 const first = yield* pool.use((connection) => Effect.as(connection.query("SELECT 1 AS ok"), connection.processId))
682 // Killing the only session leaves it queued dead, which is exactly the
683 // case the fast path declines to take. The backend answers with an error
684 // and closes afterwards, so wait for the close to land before borrowing
685 // again - both paths would race it otherwise.
686 yield* Effect.ignore(
687 pool.use((connection) => connection.query("SELECT pg_terminate_backend(pg_backend_pid())"))
688 )
689 for (let i = 0; i < 50; i++) yield* realSleep
690 const second = yield* pool.use((connection) =>
691 Effect.as(connection.query("SELECT 2 AS ok"), connection.processId)
692 )
693 assert.notStrictEqual(second, first)
694 }), 20_000)
695 it.effect("honours a configured multiplex concurrency", () =>
696 Effect.gen(function*() {
697 // Two statements to a connection means eight of them need four
698 // connections, where the default for a pool this size would fit them on
699 // one.
700 const pool = yield* PgPool.make({
701 ...(yield* poolConfig),
702 maxConnections: 4,
703 multiplex: true,
704 multiplexConcurrency: 2
705 })
706 // Every checkout stays held until all eight are in, so a slot freed on
707 // an early connection can't serve a waiter while a slow connect is
708 // still in flight.
709 const allHeld = yield* Latch.make()
710 let held = 0
711 const processIds = yield* Effect.all(
712 Array.from({ length: 8 }, () =>
713 Effect.scoped(Effect.gen(function*() {
714 const connection = yield* pool.get
715 yield* connection.query("SELECT 1")
716 if (++held === 8) yield* allHeld.open
717 yield* allHeld.await
718 return connection.processId
719 }))),
720 { concurrency: "unbounded" }
721 )
722 assert.strictEqual(new Set(processIds).size, 4)
723 }))
724 })