Repository navigation
Expand file tree
/
Copy pathafter-commit.test.ts
More file actions
217 lines (196 loc) · 8.5 KB
/
Copy pathafter-commit.test.ts
File metadata and controls
217 lines (196 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// `afterCommit` against a real Effect SqlClient (`@effect/sql-pglite`): work
// that must wait for COMMIT, such as a vendor call, and must never run after a
// rollback.
import { PgliteClient } from "@effect/sql-pglite"
import { assert, expect, layer } from "@effect/vitest"
import { Cause, Context, Deferred, Effect, ErrorReporter, Exit, Fiber, Layer, Logger, Ref, Schedule } from "effect"
import * as SqlClient from "effect/sql/SqlClient"
import * as CH from "../ch/index"
import * as PG from "../postgres"
import { postgresDialect } from "../pg/dialect"
import * as Db from "../database"
const Live = Layer.effect(
Db.Database,
Effect.gen(function* () {
return Db.fromSqlClient(yield* SqlClient.SqlClient, { dialect: postgresDialect })
}),
).pipe(Layer.provideMerge(PgliteClient.layer({ postgresqlconf: "timezone = 'UTC'" })))
let tables = 0
const freshTable = Effect.gen(function* () {
const name = `after_commit_${++tables}`
yield* Db.execute(Db.sql`CREATE TABLE ${Db.sql.identifier(name)} (id int4 PRIMARY KEY)`)
return name
})
const insert = (table: string, id: number) => Db.execute(Db.sql`INSERT INTO ${Db.sql.identifier(table)} VALUES (${id})`)
const ids = (table: string) =>
Db.run(CH.from(CH.table(table, { id: PG.int4 })).select("id").orderBy(["id", "asc"])).pipe(
Effect.map((rows) => rows.map((row) => row.id)),
)
class Domain extends Error {
readonly _tag = "Domain"
}
class Sms extends Context.Service<Sms, { readonly sent: Array<string> }>()("test/Sms") {}
const dies = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
Effect.map(Effect.exit(effect), (exit) => {
assert(Exit.isFailure(exit) && Cause.hasDies(exit.cause))
return Cause.squash(exit.cause)
})
layer(Live, { excludeTestServices: true })("afterCommit on PGlite", (it) => {
it.effect("runs after COMMIT, outside the transaction, and never after a rollback", () =>
Effect.gen(function* () {
const table = yield* freshTable
const seen = yield* Ref.make<ReadonlyArray<unknown>>([])
const record = (label: string) =>
Effect.gen(function* () {
const open = yield* Effect.serviceOption(Db.Transaction)
const rows = yield* ids(table)
yield* Ref.update(seen, (all) => [...all, { label, ids: rows, open: open._tag === "Some" }])
})
const value = yield* Db.transaction(
Effect.gen(function* () {
yield* insert(table, 1)
yield* Db.afterCommit(record("sms"))
expect(yield* Ref.get(seen)).toEqual([])
return "done"
}),
)
expect(value).toBe("done")
expect(yield* Ref.get(seen)).toEqual([{ label: "sms", ids: [1], open: false }])
const error = yield* Effect.flip(
Db.transaction(
insert(table, 2).pipe(
Effect.andThen(Db.afterCommit(record("rolled back"))),
Effect.andThen(Effect.fail(new Domain("no"))),
),
),
)
assert(error instanceof Domain)
expect(yield* Ref.get(seen)).toHaveLength(1)
}),
)
// The settings go in through `SqlClient.BeginTransaction`; a hook runs after
// COMMIT and must not start its own transaction with them.
it.effect("runs after a transaction with settings, and a transaction it starts keeps the default BEGIN", () =>
Effect.gen(function* () {
const table = yield* freshTable
const seen = yield* Ref.make<ReadonlyArray<unknown>>([])
yield* Db.transaction(
Effect.gen(function* () {
yield* insert(table, 1)
yield* Db.afterCommit(
Effect.gen(function* () {
const inHook = yield* Effect.service(SqlClient.BeginTransaction)
const rows = yield* Db.transaction(
Db.query(
Db.sql`SELECT current_setting('transaction_isolation') AS isolation, current_setting('transaction_read_only') AS read_only`,
),
)
yield* Ref.set(seen, [inHook, rows[0]])
}).pipe(Effect.orDie),
)
}),
{ isolationLevel: "repeatable read" },
)
expect(yield* Ref.get(seen)).toEqual([undefined, { isolation: "read committed", read_only: "off" }])
}),
)
it.effect("runs in queue order; a rolled-back savepoint drops its hooks, a kept one hands them up", () =>
Effect.gen(function* () {
const ran = yield* Ref.make<ReadonlyArray<string>>([])
const queue = (label: string) => Db.afterCommit(Ref.update(ran, (all) => [...all, label]))
yield* Db.transaction(
Effect.gen(function* () {
yield* queue("outer 1")
yield* Db.transaction(queue("kept savepoint"))
yield* Db.transaction(queue("dropped savepoint").pipe(Effect.andThen(Effect.fail(new Domain("inner"))))).pipe(
Effect.ignore,
)
yield* Db.transaction(Db.transaction(queue("two deep")))
yield* queue("outer 2")
expect(yield* Ref.get(ran)).toEqual([])
}),
)
expect(yield* Ref.get(ran)).toEqual(["outer 1", "kept savepoint", "two deep", "outer 2"])
// A kept savepoint inside an outer transaction that rolls back runs nothing.
yield* Ref.set(ran, [])
yield* Db.transaction(Db.transaction(queue("inner")).pipe(Effect.andThen(Effect.fail(new Domain("outer"))))).pipe(
Effect.ignore,
)
expect(yield* Ref.get(ran)).toEqual([])
}),
)
it.effect("a failing hook does not fail the committed transaction: it is reported and logged, and the next hook runs", () =>
Effect.gen(function* () {
const table = yield* freshTable
const reported: Array<unknown> = []
const logged: Array<{ level: string; message: unknown; failure: unknown }> = []
const reporter = ErrorReporter.make(({ cause }) => void reported.push(Cause.squash(cause)))
const logger = Logger.make(({ logLevel, message, cause }) => {
logged.push({ level: logLevel, message, failure: Cause.squash(cause) })
})
const ran = yield* Ref.make(0)
const vendorDown = new Domain("vendor down")
const bug = new Error("bug in a hook")
const value = yield* Db.transaction(
Effect.gen(function* () {
yield* insert(table, 1)
yield* Db.afterCommit(Effect.fail(vendorDown))
yield* Db.afterCommit(Effect.die(bug))
yield* Db.afterCommit(Ref.update(ran, (n) => n + 1))
return "committed"
}),
).pipe(Effect.provide(Layer.merge(ErrorReporter.layer([reporter]), Logger.layer([logger]))))
expect(value).toBe("committed")
expect(yield* ids(table)).toEqual([1])
expect(yield* Ref.get(ran)).toBe(1)
expect(reported).toEqual([vendorDown, bug])
expect(logged.map((entry) => [entry.level, entry.failure])).toEqual([
["Error", vendorDown],
["Error", bug],
])
}),
)
it.effect("a hook sees the services of the place it was queued, and its R stays required", () =>
Effect.gen(function* () {
const sent: Array<string> = []
const sendSms = (text: string) => Effect.flatMap(Effect.service(Sms), (sms) => Effect.sync(() => void sms.sent.push(text)))
yield* Db.transaction(Db.afterCommit(sendSms("tour booked")).pipe(Effect.provideService(Sms, { sent })))
expect(sent).toEqual(["tour booked"])
}),
)
it.effect("a retried attempt drops its hooks; only the attempt that commits runs them", () =>
Effect.gen(function* () {
const ran = yield* Ref.make<ReadonlyArray<number>>([])
const attempts = yield* Ref.make(0)
yield* Db.transaction(
Effect.gen(function* () {
const attempt = yield* Ref.updateAndGet(attempts, (n) => n + 1)
yield* Db.afterCommit(Ref.update(ran, (all) => [...all, attempt]))
if (attempt < 3) yield* Db.execute({ sql: "DO $$ BEGIN RAISE EXCEPTION 'forced' USING ERRCODE = '40001'; END $$" })
}),
{ retry: { times: 3, schedule: Schedule.spaced("1 millis") } },
)
expect(yield* Ref.get(ran)).toEqual([3])
}),
)
it.effect("runs under a plain sql.withTransaction too; dies where nothing will run it", () =>
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient
const ran = yield* Ref.make(0)
const work = Ref.update(ran, (n) => n + 1)
// Effect's own transaction queues it: it runs once the outer COMMIT succeeds.
yield* sql.withTransaction(
Db.transaction(Db.afterCommit(work)).pipe(Effect.andThen(Effect.flatMap(Ref.get(ran), (n) => Effect.sync(() => expect(n).toBe(0))))),
)
expect(yield* Ref.get(ran)).toBe(1)
// Transaction provided by hand, with no transaction behind it.
yield* dies(Db.afterCommit(work).pipe(Effect.provideService(Db.Transaction, { depth: 0, isolationLevel: undefined, accessMode: undefined })))
// A fiber forked in the transaction that queues work after it ended.
const release = yield* Deferred.make<void>()
const straggler = yield* Db.transaction(Deferred.await(release).pipe(Effect.andThen(Db.afterCommit(work)), Effect.forkDetach))
yield* Deferred.succeed(release, undefined)
yield* dies(Fiber.join(straggler))
expect(yield* Ref.get(ran)).toBe(1)
}),
)
})