@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 129 lines 5.69 KB
Raw
1 import { assert, it } from "@effect/vitest"
2 import { Effect, Fiber, Stream } from "effect"
3 import { SqlClient } from "effect/sql"
4 import { MysqlContainer } from "./utils.ts"
5
6 // Every test uses a pool of one connection, so a statement or lease left
7 // behind by an interrupted fiber blocks the follow-up query.
8 const singleConnection = MysqlContainer.clientWith({ maxConnections: 1 })
9
10 it.layer(MysqlContainer.layer, { timeout: "90 seconds", excludeTestServices: true })((it) => {
11 it.effect("interrupting a running statement kills it", () =>
12 Effect.gen(function*() {
13 const sql = yield* SqlClient.SqlClient
14 const slow = yield* sql`SELECT SLEEP(10)`.pipe(Effect.forkChild({ startImmediately: true }))
15 yield* Effect.sleep("200 millis")
16
17 const rows = yield* Fiber.interrupt(slow).pipe(
18 Effect.andThen(sql<{ running: number }>`
19 SELECT COUNT(*) AS running FROM information_schema.processlist WHERE info = 'SELECT SLEEP(10)'
20 `),
21 Effect.timeout("2 seconds")
22 )
23 assert.deepStrictEqual(rows, [{ running: 0 }])
24 }).pipe(Effect.provide(singleConnection)), { timeout: 30_000 })
25
26 it.effect(
27 "interrupting a pending reservation returns promptly and releases the late connection",
28 () =>
29 Effect.gen(function*() {
30 const sql = yield* SqlClient.SqlClient
31 const busy = yield* sql`SELECT SLEEP(3)`.pipe(Effect.forkChild({ startImmediately: true }))
32 yield* Effect.sleep("200 millis")
33 const waiting = yield* Effect.scoped(sql.reserve).pipe(Effect.forkChild({ startImmediately: true }))
34 yield* Fiber.interrupt(waiting).pipe(Effect.timeout("1 second"))
35
36 yield* Fiber.join(busy)
37 const rows = yield* sql<{ value: number }>`SELECT 1 AS value`.pipe(Effect.timeout("2 seconds"))
38 assert.deepStrictEqual(rows, [{ value: 1 }])
39 }).pipe(Effect.provide(singleConnection)),
40 { timeout: 30_000 }
41 )
42
43 it.effect("ending a stream early stops the statement", () =>
44 Effect.gen(function*() {
45 const sql = yield* SqlClient.SqlClient
46 // Rows arrive before the server reaches the SLEEP, so the stream can end
47 // while the statement is still running.
48 const [rows, next] = yield* Effect.all([
49 sql`
50 WITH RECURSIVE seq (n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM seq WHERE n < 100000)
51 SELECT /*+ SET_VAR(cte_max_recursion_depth = 100000) */ n, IF(n = 100000, SLEEP(10), 0) AS slept FROM seq
52 `.stream.pipe(Stream.take(5), Stream.runCollect),
53 sql<{ value: number }>`SELECT 1 AS value`
54 ]).pipe(Effect.timeout("2 seconds"))
55 assert.strictEqual(rows.length, 5)
56 assert.deepStrictEqual(next, [{ value: 1 }])
57 }).pipe(Effect.provide(singleConnection)), { timeout: 30_000 })
58
59 it.effect(
60 "ending a stream whose remaining rows are already buffered keeps the connection",
61 () =>
62 Effect.gen(function*() {
63 const sql = yield* SqlClient.SqlClient
64 // While the slow consumer holds the first row, the rest of a result of
65 // this size arrives and waits in mysql2's paused packet buffer. Ending
66 // the stream replays it, so the statement finishes during cleanup.
67 for (const size of [1400, 1500, 1600, 1700]) {
68 const [before, after] = yield* Effect.all([
69 sql<{ id: number }>`SELECT CONNECTION_ID() AS id`,
70 sql`
71 WITH RECURSIVE seq (n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM seq WHERE n < ${size})
72 SELECT /*+ SET_VAR(cte_max_recursion_depth = 100000) */ n, REPEAT('x', 100) AS padding FROM seq
73 `.stream.pipe(
74 Stream.mapEffect((row) => Effect.as(Effect.sleep("200 millis"), row)),
75 Stream.take(1),
76 Stream.runCollect
77 ),
78 sql<{ id: number }>`SELECT CONNECTION_ID() AS id`
79 ]).pipe(
80 Effect.map(([before, _, after]) => [before, after] as const),
81 Effect.timeout("2 seconds")
82 )
83 assert.deepStrictEqual(after, before)
84 }
85 }).pipe(Effect.provide(singleConnection)),
86 { timeout: 30_000 }
87 )
88
89 it.effect(
90 "a stream consumer can run statements on the same connection once the streamed statement ends",
91 () =>
92 Effect.gen(function*() {
93 const sql = yield* SqlClient.SqlClient
94 const rows = yield* sql.withTransaction(
95 sql`SELECT 1 AS value`.stream.pipe(
96 Stream.mapEffect(() => sql<{ value: number }>`SELECT 2 AS value`),
97 Stream.runCollect
98 )
99 ).pipe(Effect.timeout("2 seconds"))
100 assert.deepStrictEqual(rows, [[{ value: 2 }]])
101 }).pipe(Effect.provide(singleConnection)),
102 { timeout: 30_000 }
103 )
104
105 it.effect(
106 "interrupting a statement queued on a shared connection leaves the running one alone",
107 () =>
108 Effect.gen(function*() {
109 const sql = yield* SqlClient.SqlClient
110 yield* sql`CREATE TABLE cancel_queued (id INT)`
111 const first = yield* sql.withTransaction(Effect.gen(function*() {
112 const running = yield* sql<{ first: number }>`SELECT SLEEP(1) AS first`.pipe(
113 Effect.forkChild({ startImmediately: true })
114 )
115 yield* Effect.sleep("200 millis")
116 const queued = yield* sql`INSERT INTO cancel_queued VALUES (1)`.pipe(
117 Effect.forkChild({ startImmediately: true })
118 )
119 yield* Effect.sleep("50 millis")
120 yield* Fiber.interrupt(queued)
121 return yield* Fiber.join(running)
122 }))
123 // SLEEP returns 1 when it is killed and 0 when it runs to completion.
124 assert.deepStrictEqual(first, [{ first: 0 }])
125 assert.deepStrictEqual(yield* sql`SELECT id FROM cancel_queued`, [])
126 }).pipe(Effect.provide(singleConnection)),
127 { timeout: 30_000 }
128 )
129 })