mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-06 03:59:09 +08:00
test(sql): lock down failed COMMIT cleanup recovery policy
This commit is contained in:
@@ -201,40 +201,38 @@ describe("SqlClient", () => {
|
||||
])
|
||||
}))
|
||||
|
||||
// Preserve both errors without deciding whether the commit must also become
|
||||
// a defect when cleanup fails. That recovery policy needs a separate decision.
|
||||
it.effect("retains the commit error and cleanup defect before closing the connection", () =>
|
||||
Effect.gen(function*() {
|
||||
const commitError = sqlError("commit rejected")
|
||||
const cleanupError = sqlError("cleanup rejected")
|
||||
const harness = makeHarness({
|
||||
commit: () => Effect.fail(commitError),
|
||||
onCommitFailure: () => Effect.fail(cleanupError)
|
||||
})
|
||||
for (const recovery of ["none", "catchTag", "catch"] as const) {
|
||||
it.effect(
|
||||
`keeps both commit and cleanup errors as defects with ${recovery} recovery`,
|
||||
() =>
|
||||
Effect.gen(function*() {
|
||||
const commitError = sqlError("commit rejected")
|
||||
const cleanupError = sqlError("cleanup rejected")
|
||||
const harness = makeHarness({
|
||||
commit: () => Effect.fail(commitError),
|
||||
onCommitFailure: () => Effect.fail(cleanupError)
|
||||
})
|
||||
|
||||
const exit = yield* Effect.exit(harness.withTransaction(Effect.void))
|
||||
const transaction = harness.withTransaction(Effect.void)
|
||||
const exit = yield* Effect.exit(
|
||||
recovery === "catchTag" ?
|
||||
transaction.pipe(Effect.catchTag("SqlError", () => Effect.void)) :
|
||||
recovery === "catch" ?
|
||||
transaction.pipe(Effect.catch(() => Effect.void)) :
|
||||
transaction
|
||||
)
|
||||
|
||||
assert.isTrue(Exit.isFailure(exit))
|
||||
if (Exit.isFailure(exit)) {
|
||||
assert.isTrue(exit.cause.reasons.some((reason) =>
|
||||
Cause.isFailReason(reason) ?
|
||||
reason.error === commitError :
|
||||
Cause.isDieReason(reason) && reason.defect === commitError
|
||||
))
|
||||
assert.isTrue(
|
||||
exit.cause.reasons.some((reason) => Cause.isDieReason(reason) && reason.defect === cleanupError)
|
||||
)
|
||||
assert.match(Cause.pretty(exit.cause), /commit rejected/)
|
||||
assert.match(Cause.pretty(exit.cause), /cleanup rejected/)
|
||||
}
|
||||
assert.deepStrictEqual(harness.calls, [
|
||||
"acquireConnection",
|
||||
"begin",
|
||||
"commit",
|
||||
"onCommitFailure",
|
||||
"closeConnection"
|
||||
])
|
||||
}))
|
||||
assert.deepStrictEqual(exit, Exit.failCause(Cause.combine(Cause.die(commitError), Cause.die(cleanupError))))
|
||||
assert.deepStrictEqual(harness.calls, [
|
||||
"acquireConnection",
|
||||
"begin",
|
||||
"commit",
|
||||
"onCommitFailure",
|
||||
"closeConnection"
|
||||
])
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
it.effect("propagates a failed begin as a typed error without rolling back", () =>
|
||||
Effect.gen(function*() {
|
||||
|
||||
@@ -364,12 +364,12 @@ it.layer(PgContainer.layerClient, { timeout: "30 seconds" })("PgClient", (it) =>
|
||||
yield* Effect.ignore(sql`SELECT 1 / 0`)
|
||||
})).pipe(Effect.sandbox, Effect.flip)
|
||||
|
||||
assert.isTrue(Cause.hasDies(cause))
|
||||
assert.isFalse(Cause.hasFails(cause))
|
||||
const defect = Cause.squash(cause)
|
||||
assert.instanceOf(defect, SqlError.SqlError)
|
||||
assert.strictEqual(defect.reason._tag, "UnknownError")
|
||||
assert.strictEqual(defect.reason.operation, "commit")
|
||||
assert.isFalse(Cause.hasDies(cause))
|
||||
assert.isTrue(Cause.hasFails(cause))
|
||||
const error = Cause.squash(cause)
|
||||
assert.instanceOf(error, SqlError.SqlError)
|
||||
assert.strictEqual(error.reason._tag, "UnknownError")
|
||||
assert.strictEqual(error.reason.operation, "commit")
|
||||
}))
|
||||
})
|
||||
|
||||
@@ -383,12 +383,12 @@ it.layer(PgContainer.layerMakeClientUnprepared, { timeout: "30 seconds" })(
|
||||
yield* Effect.ignore(sql`SELECT 1 / 0`)
|
||||
})).pipe(Effect.sandbox, Effect.flip)
|
||||
|
||||
assert.isTrue(Cause.hasDies(cause))
|
||||
assert.isFalse(Cause.hasFails(cause))
|
||||
const defect = Cause.squash(cause)
|
||||
assert.instanceOf(defect, SqlError.SqlError)
|
||||
assert.strictEqual(defect.reason._tag, "UnknownError")
|
||||
assert.strictEqual(defect.reason.operation, "commit")
|
||||
assert.isFalse(Cause.hasDies(cause))
|
||||
assert.isTrue(Cause.hasFails(cause))
|
||||
const error = Cause.squash(cause)
|
||||
assert.instanceOf(error, SqlError.SqlError)
|
||||
assert.strictEqual(error.reason._tag, "UnknownError")
|
||||
assert.strictEqual(error.reason.operation, "commit")
|
||||
|
||||
const rows = yield* sql.withTransaction(sql<{ value: number }>`SELECT 1 AS value`)
|
||||
assert.deepStrictEqual(rows, [{ value: 1 }])
|
||||
|
||||
Reference in New Issue
Block a user