Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

MysqlClient.integration.test.ts

299 lines11.0 KB

1import { MysqlClient } from "@effect/sql-mysql2"
2import { assert, describe, it } from "@effect/vitest"
3import { Effect, Fiber, Stream } from "effect"
4import * as Arr from "effect/Array"
5import * as Scheduler from "effect/Scheduler"
6import * as Mysql from "mysql2"
7import { EventEmitter } from "node:events"
8import { Readable } from "node:stream"
9import { vi } from "vitest"
10​
11import { MysqlContainer } from "./utils.ts"
12​
13vi.mock("mysql2", { spy: true })
14​
15describe("MysqlClient", () => {
16 it.effect("stream returns same rows as direct execution", () =>
17 Effect.gen(function*() {
18 const sql = yield* MysqlClient.MysqlClient
19​
20 yield* sql`DROP TABLE IF EXISTS stream_regression_test`
21 yield* sql`CREATE TABLE stream_regression_test (id INT PRIMARY KEY AUTO_INCREMENT, value VARCHAR(255))`
22​
23 const testData = Array.from({ length: 100 }, (_, i) => ({ value: `row_${i}` }))
24 assert(Arr.isArrayNonEmpty(testData))
25 yield* sql`INSERT INTO stream_regression_test ${sql.insert(testData)}`
26​
27 const directResults = yield* sql`SELECT * FROM stream_regression_test`
28​
29 const streamResults = yield* Stream.runCollect(
30 sql`SELECT * FROM stream_regression_test`.stream
31 )
32​
33 yield* sql`DROP TABLE stream_regression_test`
34​
35 assert.strictEqual(
36 streamResults.length,
37 directResults.length,
38 `Stream returned ${streamResults.length} rows, expected ${directResults.length}`
39 )
40 }).pipe(
41 Effect.provide(MysqlContainer.layerClient),
42 Effect.catchTag("ContainerError", () => Effect.void)
43 ), { timeout: 60_000 })
44​
45 // These replace mysql2's pool with fakes through one-shot mocks, so they
46 // must not run alongside each other.
47 describe("with a fake mysql2 pool", { concurrent: false }, () => {
48 it.effect("uses query when prepared statements are disabled", () =>
49 Effect.gen(function*() {
50 const calls: Array<"execute" | "query"> = []
51 const query = (options: string | object, callback: (cause: null, rows?: Array<unknown>) => void) => {
52 if (typeof options !== "string") {
53 calls.push("query")
54 }
55 callback(null, [])
56 }
57 const conn = {
58 threadId: 1,
59 query,
60 execute: (_options: object, callback: (cause: null, rows: Array<unknown>) => void) => {
61 calls.push("execute")
62 callback(null, [])
63 },
64 release: () => {}
65 }
66 vi.mocked(Mysql.createPool).mockReturnValueOnce({
67 query,
68 getConnection: (callback: (cause: null, conn: unknown) => void) => callback(null, conn),
69 end: (callback: () => void) => callback()
70 } as unknown as Mysql.Pool)
71​
72 yield* Effect.gen(function*() {
73 const sql = yield* MysqlClient.MysqlClient
74 yield* sql`SELECT ${1}`
75 yield* sql`SELECT ${1}`.values
76 yield* sql.unsafe("SELECT ?", [1]).raw
77 }).pipe(Effect.provide(MysqlClient.layer({ disablePreparedStatements: true })))
78​
79 assert.deepStrictEqual(calls, ["query", "query", "query"])
80 }))
81​
82 it.live(
83 "keeps the connection when a cancelled stream settles before its kill is registered",
84 () =>
85 Effect.gen(function*() {
86 // The yield budget decides where the fiber yields. Small budgets put a
87 // yield between deciding to cancel the stream's statement and
88 // registering for its settlement, which is where the statement ends.
89 for (const maxOpsBeforeYield of [3, 4, 5, 6, 7, 8]) {
90 const log = yield* cancelSettlingStream(maxOpsBeforeYield)
91 // A kill that reaches the server while the queued statement runs
92 // interrupts that statement instead.
93 const kill = log.indexOf("kill 7")
94 assert.isTrue(
95 kill === -1 || kill < log.indexOf("start SELECT 2 AS value"),
96 `budget ${maxOpsBeforeYield}: ${log.join(", ")}`
97 )
98 assert.notInclude(log, "connection destroyed", `budget ${maxOpsBeforeYield}`)
99 }
100 }),
101 { timeout: 60_000 }
102 )
103​
104 it.live("kills a stream's statement when interrupted before the first row", () =>
105 Effect.gen(function*() {
106 // As above, small budgets put a yield right after the statement starts,
107 // which is where the stream is interrupted.
108 for (const maxOpsBeforeYield of [3, 4, 5, 6, 7, 8]) {
109 const log = yield* interruptStreamSetup(maxOpsBeforeYield)
110 // The connection is only reused once the streamed statement has ended.
111 const end = log.indexOf("stream end")
112 assert.isTrue(
113 end !== -1 && end < log.indexOf("start SELECT 2 AS value"),
114 `budget ${maxOpsBeforeYield}: ${log.join(", ")}`
115 )
116 }
117 }), { timeout: 60_000 })
118​
119 it.live("gives a shared connection back when the kill connection cannot be created", () =>
120 Effect.gen(function*() {
121 const log: Array<string> = []
122 const command = Object.assign(new EventEmitter(), {
123 stream: () => {
124 // Unless it is killed, the statement ends on its own after 100 ms.
125 setTimeout(() => command.emit("end"), 100)
126 return new Readable({ objectMode: true, read() {} })
127 }
128 })
129 // mysql2 calls a configured `stream` factory while constructing the
130 // connection, so creating the kill connection can throw synchronously.
131 mockPool(log, command, {
132 kill: () => {
133 throw new Error("SSH tunnel unavailable")
134 }
135 })
136​
137 yield* withMockClient(Effect.scoped(Effect.gen(function*() {
138 const sql = yield* MysqlClient.MysqlClient
139 const conn = yield* sql.reserve
140 const consumer = yield* conn.executeStream("SELECT 1 AS value", [], undefined).pipe(
141 Stream.runDrain,
142 Effect.forkChild
143 )
144 yield* Effect.sleep("20 millis")
145 yield* Fiber.interrupt(consumer)
146 // The connection may be retired instead of reused, so the statement
147 // only has to settle rather than succeed.
148 yield* conn.execute("SELECT 2 AS value", [], undefined).pipe(
149 Effect.exit,
150 Effect.timeout("2 seconds")
151 )
152 })))
153 assert.isTrue(
154 log.includes("start SELECT 2 AS value") || log.includes("connection destroyed"),
155 log.join(", ")
156 )
157 }), { timeout: 60_000 })
158 })
159})
160​
161/**
162 * Provides a mysql2 pool whose only connection, thread 7, runs streams through
163 * `command` and every other statement in 50 ms. Unless `kill` provides the
164 * connection for it, a `KILL QUERY` reaches the server 20 ms after it is sent,
165 * and then calls `onKill`.
166 */
167const mockPool = (
168 log: Array<string>,
169 command: EventEmitter,
170 options: { readonly onKill?: () => void; readonly kill?: () => Mysql.Connection } = {}
171) => {
172 const onKill = options.onKill ?? (() => {})
173 const conn = {
174 threadId: 7,
175 query: (options: string | { sql: string }, ...args: Array<any>) => {
176 if (typeof options === "string") return command
177 args[args.length - 1](null, [])
178 },
179 execute: (options: { sql: string }, callback: (cause: null, rows: Array<unknown>) => void) => {
180 log.push(`start ${options.sql}`)
181 setTimeout(() => {
182 log.push(`end ${options.sql}`)
183 callback(null, [{ value: 2 }])
184 }, 50)
185 },
186 release: () => {},
187 destroy: () => {
188 log.push("connection destroyed")
189 }
190 }
191 vi.mocked(Mysql.createPool).mockReturnValueOnce({
192 query: (_sql: string, callback: (cause: null) => void) => callback(null),
193 getConnection: (callback: (cause: null, conn: unknown) => void) => callback(null, conn),
194 end: (callback: () => void) => callback()
195 } as unknown as Mysql.Pool)
196 vi.mocked(Mysql.createConnection).mockImplementationOnce(
197 options.kill ?? (() =>
198 ({
199 on: () => {},
200 destroy: () => {},
201 query: (_sql: string, values: Array<unknown>, callback: () => void) => {
202 setTimeout(() => {
203 log.push(`kill ${values[0]}`)
204 onKill()
205 callback()
206 }, 20)
207 }
208 }) as unknown as Mysql.Connection)
209 )
211​
212const withMockClient = <A, E>(effect: Effect.Effect<A, E, MysqlClient.MysqlClient>) =>
213 effect.pipe(
214 Effect.provide(MysqlClient.layer({})),
215 Effect.ensuring(Effect.sync(() => vi.mocked(Mysql.createConnection).mockReset()))
216 )
217​
218const cancelSettlingStream = Effect.fnUntraced(function*(maxOpsBeforeYield: number) {
219 const log: Array<string> = []
220 const command = Object.assign(new EventEmitter(), {
221 stream: () => {
222 const rows = new Readable({ objectMode: true, read() {} })
223 rows.push({ value: 1 })
224 rows._destroy = (error, callback) => {
225 // The statement ends right after the stream is destroyed, before
226 // the fiber resumes.
227 queueMicrotask(() => {
228 log.push("stream end")
229 command.emit("end")
230 })
231 callback(error)
232 }
233 return rows
234 }
235 })
236 mockPool(log, command)
237​
238 yield* withMockClient(Effect.gen(function*() {
239 const sql = yield* MysqlClient.MysqlClient
240 yield* sql.withTransaction(Effect.gen(function*() {
241 let queued: Fiber.Fiber<unknown, unknown> | undefined
242 yield* sql`SELECT 1 AS value`.stream.pipe(
243 // Queue a statement on the same connection while the stream runs.
244 Stream.tap(() =>
245 Effect.map(Effect.forkChild(sql`SELECT 2 AS value`, { startImmediately: true }), (fiber) => {
246 queued = fiber
247 })
248 ),
249 Stream.take(1),
250 Stream.runCollect,
251 Effect.provideService(Scheduler.MaxOpsBeforeYield, maxOpsBeforeYield)
252 )
253 yield* Fiber.join(queued!)
254 }))
255 }))
256 return log
257})
258​
259const interruptStreamSetup = Effect.fnUntraced(function*(maxOpsBeforeYield: number) {
260 const log: Array<string> = []
261 let consumer: Fiber.Fiber<unknown, unknown> | undefined
262 let ended = false
263 const end = () => {
264 if (ended) return
265 ended = true
266 log.push("stream end")
267 command.emit("end")
268 }
269 const command = Object.assign(new EventEmitter(), {
270 stream: () => {
271 // As soon as the statement has started, interrupt the consumer, which
272 // closes the stream, and the stream's setup, which closing the stream
273 // would otherwise only reach after the setup resumes. Unless it is
274 // killed, the statement ends on its own after 100 ms.
275 const setup = Fiber.getCurrent()!
276 queueMicrotask(() => {
277 setup.interruptUnsafe()
278 consumer!.interruptUnsafe()
279 })
280 setTimeout(end, 100)
281 return new Readable({ objectMode: true, read() {} })
282 }
283 })
284 mockPool(log, command, { onKill: end })
285​
286 yield* withMockClient(Effect.gen(function*() {
287 const sql = yield* MysqlClient.MysqlClient
288 yield* sql.withTransaction(Effect.gen(function*() {
289 consumer = yield* sql`SELECT 1 AS value`.stream.pipe(
290 Stream.runDrain,
291 Effect.provideService(Scheduler.MaxOpsBeforeYield, maxOpsBeforeYield),
292 Effect.forkChild
293 )
294 yield* Fiber.await(consumer)
295 yield* sql`SELECT 2 AS value`
296 }))
297 }))
298 return log
299})