fix: dispatch guard and Defect-safe rollback (#32)
A readable intent that fails in handling now always gets exactly one ack: the new static internal code, unless the handler's own ack was already enqueued. Both transaction helpers roll back on every unwind path, Defect included, so the shared SQLite connection never keeps an open transaction. Test-only dispfault build covers catchable/defect/after-ack; DESIGN 6.5 records internal as an owner decision and 6.2 names refusal codes as additive.
This commit is contained in:
+51
-1
@@ -3,7 +3,7 @@
|
||||
## migration leaves no trace (one transaction per step).
|
||||
import std/[unittest, os, strutils]
|
||||
import db_connector/db_sqlite
|
||||
import db
|
||||
import db, world
|
||||
|
||||
var tmpCount = 0
|
||||
proc tmpDir(): string =
|
||||
@@ -392,3 +392,53 @@ suite "step 8 wall CHECKs (DESIGN 4.4)":
|
||||
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'wall_v1'") == "0"
|
||||
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'marker_seen'") == "0"
|
||||
check db.getValue(sql"SELECT props_gm FROM entity WHERE id = 'tok000000009'") == "not json"
|
||||
|
||||
suite "transaction unwind (#32)":
|
||||
# The dispatch guard turns an escaping failure into one `internal` ack; the storage half of that
|
||||
# contract is here: a `Defect` must roll back exactly like a `CatchableError` and leave the shared
|
||||
# connection with no open transaction, or the next intent runs inside somebody else's BEGIN.
|
||||
proc insertMeta(db: DbConn, k, v: string) =
|
||||
db.exec(sql"INSERT INTO meta (k, v) VALUES (?, ?)", k, v)
|
||||
|
||||
test "withTransaction rolls back a Defect and leaves the connection usable":
|
||||
let d = tmpDir()
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
check migrate(db) == LatestVersion
|
||||
var raised = false
|
||||
try:
|
||||
db.withTransaction:
|
||||
insertMeta(db, "boom", "1")
|
||||
raise newException(Defect, "injected defect")
|
||||
except Defect:
|
||||
raised = true
|
||||
check raised
|
||||
check db.getValue(sql"SELECT count(*) FROM meta WHERE k = 'boom'") == "0"
|
||||
# no BEGIN was left open: the next ordinary transaction commits its own row and nothing else
|
||||
db.withTransaction:
|
||||
insertMeta(db, "next", "1")
|
||||
check db.getValue(sql"SELECT count(*) FROM meta WHERE k = 'next'") == "1"
|
||||
check db.getValue(sql"SELECT count(*) FROM meta") == "1"
|
||||
|
||||
test "authoringTransaction rolls back a Defect and restores NORMAL":
|
||||
let d = tmpDir()
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
check migrate(db) == LatestVersion
|
||||
var raised = false
|
||||
try:
|
||||
db.authoringTransaction:
|
||||
insertMeta(db, "boom", "1")
|
||||
raise newException(Defect, "injected defect")
|
||||
except Defect:
|
||||
raised = true
|
||||
check raised
|
||||
check db.getValue(sql"SELECT count(*) FROM meta WHERE k = 'boom'") == "0"
|
||||
check db.getValue(sql"PRAGMA synchronous") == "1" # NORMAL again (Q12)
|
||||
db.authoringTransaction:
|
||||
insertMeta(db, "next", "1")
|
||||
check db.getValue(sql"SELECT count(*) FROM meta WHERE k = 'next'") == "1"
|
||||
|
||||
Reference in New Issue
Block a user