Sqlite: per-connection phase-targetable fault guard (VL-SQLITE-03)

This commit is contained in:
faumaray committed 2026-12-03 18:34:19 +03:00
1 parent 103523f04a
commit 3b1ef4b028
3 files changed
+170 -11

No files matched your search

@@ -806,3 +806,74 @@ Void::Testing::Test sqlite_failed_commit_outcome_is_unknown_not_rolled_back()
Void::Testing::expect(tx->state() == TransactionState::RolledBack);
}
}
Void::Testing::Test sqlite_fault_guard_targets_connection_and_phase()
{
TempSqliteDir dir;
auto db1_r = open_database(dir.path / "one.db");
auto db2_r = open_database(dir.path / "two.db");
Void::Testing::require(db1_r.has_value());
Void::Testing::require(db2_r.has_value());
auto& db1 = *db1_r;
auto& db2 = *db2_r;
Void::Testing::require(db1.exec("CREATE TABLE t(id INTEGER PRIMARY KEY)").has_value());
Void::Testing::require(db2.exec("CREATE TABLE t(id INTEGER PRIMARY KEY)").has_value());
// A connection-targeted exec fault does not leak to the other connection.
{
ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(),
.phase = FaultPhase::exec,
.fail_next_rc = SQLITE_IOERR}};
auto failed = db1.exec("INSERT INTO t VALUES(1)");
Void::Testing::require(!failed.has_value());
Void::Testing::expect_eq(failed.error().code().value,
static_cast<std::int32_t>(SqliteErrorCode::io));
Void::Testing::require(db2.exec("INSERT INTO t VALUES(2)").has_value());
Void::Testing::require(db1.exec("INSERT INTO t VALUES(1)").has_value());
}
// A phase mismatch neither fires nor consumes the pending fault.
{
ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(),
.phase = FaultPhase::commit,
.fail_next_rc = SQLITE_IOERR}};
Void::Testing::require(db1.exec("INSERT INTO t VALUES(3)").has_value());
auto tx = db1.begin(BeginMode::Immediate);
Void::Testing::require(tx.has_value());
auto failed = tx->commit();
Void::Testing::require(!failed.has_value());
Void::Testing::expect(tx->state() == TransactionState::Active);
Void::Testing::require(tx->rollback_checked().has_value());
}
// prepare, step and checkpoint phases are individually targetable.
{
ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(),
.phase = FaultPhase::prepare,
.fail_next_rc = SQLITE_IOERR}};
auto stmt = db1.prepare("SELECT 1");
Void::Testing::require(!stmt.has_value());
Void::Testing::expect_eq(stmt.error().code().value,
static_cast<std::int32_t>(SqliteErrorCode::io));
}
{
ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(),
.phase = FaultPhase::step,
.fail_next_rc = SQLITE_IOERR}};
auto stmt = db1.prepare("SELECT 1");
Void::Testing::require(stmt.has_value());
auto stepped = stmt->step();
Void::Testing::require(!stepped.has_value());
Void::Testing::expect_eq(stepped.error().code().value,
static_cast<std::int32_t>(SqliteErrorCode::io));
}
{
ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(),
.phase = FaultPhase::checkpoint,
.fail_next_rc = SQLITE_IOERR}};
auto checkpoint = db1.checkpoint();
Void::Testing::require(!checkpoint.has_value());
Void::Testing::expect_eq(checkpoint.error().code().value,
static_cast<std::int32_t>(SqliteErrorCode::io));
}
}