The best way to control Claude Code, Codex and more from any computer or phone

main
ts 468 lines 16.9 KB
Raw
1 import { PgClient, PgPool } from "@effect/sql-pg"
2 import { assert, describe, it } from "@effect/vitest"
3 import { Cause, Effect, Fiber, Latch, Option, Queue, References, Tracer } from "effect"
4 import type * as Duration from "effect/Duration"
5 import * as Reactivity from "effect/reactivity/Reactivity"
6 import type * as Scope from "effect/Scope"
7 import type { SqlError } from "effect/sql/SqlError"
8 import * as Statement from "effect/sql/Statement"
9 import * as TestClock from "effect/testing/TestClock"
10 import { Duplex } from "node:stream"
11
12 const backendMessage = (tag: string, payload: Buffer): Buffer => {
13 const length = Buffer.allocUnsafe(4)
14 length.writeInt32BE(payload.length + 4)
15 return Buffer.concat([Buffer.from(tag), length, payload])
16 }
17
18 const ready = Buffer.concat([
19 backendMessage("R", Buffer.alloc(4)),
20 backendMessage("K", Buffer.alloc(8)),
21 backendMessage("Z", Buffer.from("I"))
22 ])
23
24 const emptyQueryResult = Buffer.concat([
25 backendMessage("1", Buffer.alloc(0)),
26 backendMessage("2", Buffer.alloc(0)),
27 backendMessage("n", Buffer.alloc(0)),
28 backendMessage("C", Buffer.from("SELECT 0\0")),
29 backendMessage("Z", Buffer.from("I"))
30 ])
31
32 const completed = (tag: string, status: string) =>
33 Buffer.concat([
34 backendMessage("1", Buffer.alloc(0)),
35 backendMessage("2", Buffer.alloc(0)),
36 backendMessage("n", Buffer.alloc(0)),
37 backendMessage("C", Buffer.from(`${tag}\0`)),
38 backendMessage("Z", Buffer.from(status))
39 ])
40
41 const waitFor = (predicate: () => boolean) =>
42 Effect.promise(async () => {
43 for (let i = 0; i < 100 && !predicate(); i++) {
44 await new Promise((resolve) => setTimeout(resolve, 10))
45 }
46 assert.ok(predicate(), "background acquisition did not start")
47 })
48
49 describe("PgPool failed startup", () => {
50 it.live(
51 "retries a stale background acquisition rather than failing an unrelated checkout",
52 () =>
53 Effect.scoped(Effect.gen(function*() {
54 let attempts = 0
55 let backgroundStarted = false
56 const pool = yield* PgPool.make({
57 username: "test",
58 connectTimeout: "250 millis",
59 minConnections: 2,
60 maxConnections: 2,
61 stream: () => {
62 attempts++
63 const attempt = attempts
64 let startup = true
65 const socket = new Duplex({
66 read() {},
67 write(_chunk: Buffer, _encoding, callback) {
68 if (startup) {
69 startup = false
70 if (attempt === 2) backgroundStarted = true
71 else queueMicrotask(() => socket.push(ready))
72 }
73 callback()
74 }
75 })
76 return socket
77 }
78 })
79
80 // Keep the healthy session leased so the failure becomes a placeholder.
81 const healthy = yield* pool.get
82 yield* waitFor(() => backgroundStarted)
83 yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 400)))
84
85 const replacement = yield* pool.get
86 assert.notStrictEqual(replacement, healthy)
87 assert.strictEqual(attempts, 3)
88 })),
89 10_000
90 )
91
92 it.live(
93 "reports a caller-owned connect failure instead of retrying indefinitely",
94 () =>
95 Effect.scoped(Effect.gen(function*() {
96 let attempts = 0
97 const pool = yield* PgPool.make({
98 username: "test",
99 maxConnections: 1,
100 stream: () => {
101 attempts++
102 const socket = new Duplex({ read() {}, write() {} })
103 queueMicrotask(() => socket.destroy())
104 return socket
105 }
106 })
107 const result = yield* Effect.result(pool.get)
108 assert.strictEqual(result._tag, "Failure")
109 if (result._tag === "Failure") {
110 assert.strictEqual(result.failure.reason._tag, "ConnectionError")
111 assert.strictEqual(result.failure.reason.operation, "connect")
112 }
113 assert.strictEqual(attempts, 1)
114 })),
115 10_000
116 )
117 })
118
119 describe("PgPool acquireTimeout", () => {
120 // The timeouts run on the TestClock, so opening the first connection on a
121 // busy machine cannot use up the acquireTimeout before the pool is exhausted.
122 it.effect("fails a checkout that waits on an exhausted pool past acquireTimeout", () =>
123 Effect.gen(function*() {
124 const sql = yield* PgClient.make({
125 username: "test",
126 maxConnections: 1,
127 acquireTimeout: "100 millis",
128 stream: () => {
129 const socket = new Duplex({
130 read() {},
131 write(_chunk: Buffer, _encoding, callback) {
132 queueMicrotask(() => socket.push(ready))
133 callback()
134 }
135 })
136 return socket
137 }
138 })
139 yield* sql.reserve
140
141 const statement = yield* Effect.forkChild(Effect.flip(sql`SELECT 1`))
142 const transaction = yield* Effect.forkChild(Effect.flip(sql.withTransaction(sql`SELECT 1`)))
143 yield* TestClock.adjust("100 millis")
144 for (const error of [yield* Fiber.join(statement), yield* Fiber.join(transaction)]) {
145 assert.strictEqual(error.reason._tag, "ConnectionError")
146 assert.strictEqual(error.reason.operation, "acquireConnection")
147 }
148 }).pipe(
149 Effect.scoped,
150 Effect.provide(Reactivity.layer)
151 ), 10_000)
152
153 it.effect(
154 "fails a multiplexed reservation that waits on a busy pipeline past acquireTimeout",
155 () =>
156 Effect.gen(function*() {
157 let socket!: Duplex
158 let writes = 0
159 const statementSent = Latch.makeUnsafe()
160 const sql = yield* PgClient.make({
161 username: "test",
162 maxConnections: 1,
163 multiplex: true,
164 acquireTimeout: "100 millis",
165 stream: () => {
166 socket = new Duplex({
167 read() {},
168 write(_chunk: Buffer, _encoding, callback) {
169 if (++writes === 1) queueMicrotask(() => socket.push(ready))
170 else statementSent.openUnsafe()
171 callback()
172 }
173 })
174 return socket
175 }
176 })
177 // The shared statement stays on the wire until the backend answers it.
178 const busy = yield* Effect.forkChild(sql`SELECT 1`)
179 yield* statementSent.await
180
181 // The failed reservation shares the test scope, which stays open.
182 const reservation = yield* Effect.forkChild(Effect.flip(sql.reserve))
183 yield* TestClock.adjust("100 millis")
184 const error = yield* Fiber.join(reservation)
185 assert.strictEqual(error.reason._tag, "ConnectionError")
186 assert.strictEqual(error.reason.operation, "acquireConnection")
187
188 socket.push(emptyQueryResult)
189 yield* Fiber.join(busy)
190 yield* Effect.scoped(sql.reserve)
191 }).pipe(
192 Effect.scoped,
193 Effect.provide(Reactivity.layer)
194 ),
195 10_000
196 )
197 })
198
199 describe("PgPool connection spans", () => {
200 // A server that holds each connection's startup until the test answers it, so
201 // the test decides when a connect finishes and the TestClock decides how long
202 // it took.
203 const heldServer = Effect.map(Queue.unbounded<Effect.Effect<void>>(), (startups) => ({
204 startups,
205 stream: () => {
206 let startup = true
207 const socket = new Duplex({
208 read() {},
209 write(_chunk: Buffer, _encoding, callback) {
210 if (startup) {
211 startup = false
212 Queue.offerUnsafe(startups, Effect.sync(() => socket.push(ready)))
213 }
214 callback()
215 }
216 })
217 return socket
218 }
219 }))
220
221 const recording = () => {
222 const spans: Array<Tracer.NativeSpan> = []
223 const tracer = Tracer.make({
224 span(options) {
225 const span = new Tracer.NativeSpan(options)
226 spans.push(span)
227 return span
228 }
229 })
230 return { tracer, named: (name: string) => spans.filter((span) => span.name === name) }
231 }
232
233 const traced = <A, E, R>(effect: Effect.Effect<A, E, R>, tracer: Tracer.Tracer, propagate: boolean) =>
234 effect.pipe(Effect.withTracer(tracer), Effect.provideService(Statement.SpanPropagationEnabled, propagate))
235
236 // Runs a statement whose checkout opens the pool's first session, answers the
237 // connect after 40 ms, then runs a second statement on the same session.
238 const firstCheckout = (
239 checkout: (pool: PgPool.PgPool) => Effect.Effect<unknown, SqlError, Scope.Scope>,
240 options: {
241 readonly acquireTimeout?: Duration.Input | undefined
242 readonly propagate?: boolean
243 readonly timing?: boolean
244 } = {}
245 ) => {
246 const { named, tracer } = recording()
247 return Effect.scoped(Effect.gen(function*() {
248 const server = yield* heldServer
249 const pool = yield* PgPool.make({
250 username: "test",
251 maxConnections: 1,
252 acquireTimeout: options.acquireTimeout,
253 stream: server.stream
254 })
255 const first = yield* Effect.forkChild(Effect.scoped(checkout(pool)).pipe(Effect.withSpan("first statement")))
256 const answer = yield* Queue.take(server.startups)
257 yield* TestClock.adjust("40 millis")
258 yield* answer
259 yield* Fiber.join(first)
260 yield* Effect.scoped(pool.get).pipe(Effect.withSpan("second statement"))
261 return { connects: named("db.connect"), first: named("first statement")[0] }
262 })).pipe(
263 Effect.provideService(References.TracerTimingEnabled, options.timing ?? true),
264 (effect) => traced(effect, tracer, options.propagate ?? true)
265 )
266 }
267
268 it.effect("reports the connect a statement waited for under its span, only with propagation and timing", () =>
269 Effect.gen(function*() {
270 const use = (pool: PgPool.PgPool) => pool.use(() => Effect.void)
271 for (
272 const [checkout, acquireTimeout] of [
273 [use, undefined],
274 [use, "1 second"],
275 [(pool: PgPool.PgPool) => pool.reserve, undefined]
276 ] as const
277 ) {
278 const { connects, first } = yield* firstCheckout(checkout, { acquireTimeout })
279 assert.strictEqual(connects.length, 1)
280 const [connect] = connects
281 assert.strictEqual(Option.getOrThrow(connect.parent).spanId, first.spanId)
282 assert.strictEqual(connect.status.startTime, first.status.startTime)
283 assert.strictEqual(connect.attributes.get("db.client.connection.connect_time_ms"), 40)
284 }
285 assert.strictEqual((yield* firstCheckout(use, { propagate: false })).connects.length, 0)
286 assert.strictEqual((yield* firstCheckout(use, { timing: false })).connects.length, 0)
287 }))
288
289 it.live("reports no connect for a session that was ready before the statement", () => {
290 const { named, tracer } = recording()
291 return traced(
292 Effect.scoped(Effect.gen(function*() {
293 const server = yield* heldServer
294 const pool = yield* PgPool.make({
295 username: "test",
296 minConnections: 1,
297 maxConnections: 2,
298 stream: server.stream
299 })
300 yield* Effect.flatten(Queue.take(server.startups))
301 // A background session gives no signal when its handshake lands, so this
302 // waits for it the way the failed-startup tests above do.
303 yield* Effect.sleep("50 millis")
304 yield* Effect.scoped(Effect.gen(function*() {
305 yield* pool.get.pipe(Effect.withSpan("ready statement"))
306 const waiting = yield* Effect.forkChild(Effect.scoped(pool.get).pipe(Effect.withSpan("waiting statement")))
307 yield* Effect.flatten(Queue.take(server.startups))
308 yield* Fiber.join(waiting)
309 }))
310
311 const connects = named("db.connect")
312 assert.strictEqual(connects.length, 1)
313 assert.strictEqual(Option.getOrThrow(connects[0].parent).spanId, named("waiting statement")[0].spanId)
314 })),
315 tracer,
316 true
317 )
318 })
319
320 it.effect("reports a cold transaction's connect and control statements under its transaction span", () => {
321 const { named, tracer } = recording()
322 // A backend that accepts every BEGIN and COMMIT.
323 const stream = () => {
324 let startup = true
325 const socket = new Duplex({
326 read() {},
327 write(chunk: Buffer, _encoding, callback) {
328 if (startup) {
329 startup = false
330 queueMicrotask(() => socket.push(ready))
331 } else if (chunk[0] === 0x50) {
332 const response = chunk.includes("BEGIN") ? completed("BEGIN", "T") : completed("COMMIT", "I")
333 queueMicrotask(() => socket.push(response))
334 }
335 callback()
336 }
337 })
338 return socket
339 }
340 return traced(
341 Effect.gen(function*() {
342 const sql = yield* PgClient.make({
343 username: "test",
344 database: "app",
345 maxConnections: 1,
346 prepare: false,
347 stream
348 })
349 yield* sql.withTransaction(Effect.void).pipe(Effect.withSpan("outer"))
350
351 const [transaction] = named("sql.transaction")
352 assert.deepStrictEqual(
353 ["db.connect", "BEGIN app", "COMMIT app"].map((name) =>
354 named(name).map((span) => Option.getOrThrow(span.parent).spanId)
355 ),
356 [[transaction.spanId], [transaction.spanId], [transaction.spanId]]
357 )
358 }).pipe(
359 Effect.scoped,
360 Effect.provide(Reactivity.layer)
361 ),
362 tracer,
363 true
364 )
365 })
366 })
367
368 describe("PgClient failed transaction cleanup", () => {
369 const failed = (code: string) =>
370 Buffer.concat([
371 backendMessage("E", Buffer.from(`SERROR\0C${code}\0Mfailed\0\0`)),
372 backendMessage("Z", Buffer.from("E"))
373 ])
374
375 // A backend whose COMMIT is rejected before it runs, as when a pooler has
376 // lost the prepared statement, and whose ROLLBACK is cancelled. Both leave
377 // the transaction aborted, and an aborted session rejects everything else.
378 const abortingBackend = () => {
379 let startup = true
380 let aborted = false
381 const socket = new Duplex({
382 read() {},
383 write(chunk: Buffer, _encoding, callback) {
384 if (startup) {
385 startup = false
386 queueMicrotask(() => socket.push(ready))
387 } else if (chunk[0] === 0x50) {
388 let response: Buffer
389 if (chunk.includes("ROLLBACK") || chunk.includes("COMMIT")) {
390 aborted = true
391 response = failed(chunk.includes("COMMIT") ? "26000" : "57014")
392 } else if (aborted) {
393 response = failed("25P02")
394 } else if (chunk.includes("BEGIN")) {
395 response = completed("BEGIN", "T")
396 } else {
397 response = completed("SELECT 0", "I")
398 }
399 queueMicrotask(() => socket.push(response))
400 }
401 callback()
402 }
403 })
404 return socket
405 }
406
407 it.effect("does not reuse a session left in an aborted transaction", () =>
408 Effect.gen(function*() {
409 const sql = yield* PgClient.make({ username: "test", maxConnections: 1, prepare: false, stream: abortingBackend })
410
411 const commit = yield* sql.withTransaction(Effect.void).pipe(Effect.sandbox, Effect.flip)
412 assert.isFalse(Cause.hasDies(commit))
413 assert.propertyVal(Cause.squash(commit), "_tag", "SqlError")
414 yield* sql`SELECT 1`
415
416 yield* Effect.exit(sql.withTransaction(Effect.fail("body")))
417 yield* sql`SELECT 1`
418 }).pipe(
419 Effect.scoped,
420 Effect.provide(Reactivity.layer)
421 ))
422 })
423
424 const readyStream = () => {
425 const socket = new Duplex({
426 read() {},
427 write(_chunk: Buffer, _encoding, callback) {
428 queueMicrotask(() => socket.push(ready))
429 callback()
430 }
431 })
432 return socket
433 }
434
435 describe("pool stats", () => {
436 it.effect("counts a multiplexed session once however many fibers share it", () =>
437 Effect.scoped(Effect.gen(function*() {
438 const pool = yield* PgPool.make({ username: "test", multiplex: true, maxConnections: 2, stream: readyStream })
439 yield* pool.get
440 yield* pool.get
441 yield* pool.get
442 assert.deepStrictEqual(yield* pool.stats, { idle: 0, used: 1, pending: 0, min: 0, max: 2 })
443 })))
444
445 it.effect("PgClient.make reports the occupancy of its pool", () =>
446 Effect.scoped(Effect.gen(function*() {
447 const sql = yield* PgClient.make({ username: "test", maxConnections: 3, stream: readyStream })
448 assert.deepStrictEqual(yield* sql.poolStats, { idle: 0, used: 0, pending: 0, min: 0, max: 3 })
449 yield* Effect.scoped(Effect.gen(function*() {
450 yield* sql.reserve
451 assert.deepStrictEqual(yield* sql.poolStats, { idle: 0, used: 1, pending: 0, min: 0, max: 3 })
452 }))
453 assert.deepStrictEqual(yield* sql.poolStats, { idle: 1, used: 0, pending: 0, min: 0, max: 3 })
454 })).pipe(Effect.provide(Reactivity.layer)))
455
456 it.effect("PgClient.makeClient reports its one session as permanently leased", () =>
457 Effect.scoped(Effect.gen(function*() {
458 const sql = yield* PgClient.makeClient({ username: "test", stream: readyStream })
459 // The snapshot describes the session, not statement activity, so it does
460 // not change while work runs on it.
461 const leased = { idle: 0, used: 1, pending: 0, min: 1, max: 1 }
462 assert.deepStrictEqual(yield* sql.poolStats, leased)
463 yield* Effect.scoped(Effect.gen(function*() {
464 yield* sql.reserve
465 assert.deepStrictEqual(yield* sql.poolStats, leased)
466 }))
467 })).pipe(Effect.provide(Reactivity.layer)))
468 })