Skip to content

Commit

Permalink
test: sqlite, add coverage for txn abortion failure during destruction
Browse files Browse the repository at this point in the history
Verifying that the SQLite database remains active and usable even after
a transaction abortion failure during the destruction of the handler object.
  • Loading branch information
furszy committed Jan 8, 2024
1 parent cb57ce7 commit 3d23c0f
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/wallet/test/db_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,47 @@ BOOST_AUTO_TEST_CASE(concurrent_txn_dont_interfere)
BOOST_CHECK(handler2->Read(key, read_value));
BOOST_CHECK_EQUAL(read_value, value2);
}

class DbExecBlocker : public SQliteExecHandler
{
public:
bool CanExecute(const std::string& statement) override {
if (statement == "ROLLBACK TRANSACTION") return false;
return true;
}
};

BOOST_AUTO_TEST_CASE(on_destroy_txn_abortion_failure)
{
// Verifies that the SQLite database remains active and usable even after a transaction abortion failure
// during the destruction of the handler object.
DatabaseOptions options;
DatabaseStatus status;
bilingual_str error;
std::unique_ptr<SQLiteDatabase> database = MakeSQLiteDatabase(m_path_root / "sqlite", options, status, error);

std::string key = "key";
std::string value = "value";
std::string value2 = "value_2";

{
std::unique_ptr<SQLiteBatch> handler = std::make_unique<SQLiteBatch>(*database);
BOOST_CHECK(handler->TxnBegin());
BOOST_CHECK(handler->Write(key, value));
// Set a handler to prevent the txn abortion during destruction.
// Mimicking a db statement execution failure.
handler->SetExecHandler(std::make_unique<DbExecBlocker>());
}

// Now, verify that any subsequent write and read operations can be performed
std::unique_ptr<DatabaseBatch> handler2 = Assert(database)->MakeBatch();
BOOST_CHECK(handler2->Write(key, value2));

std::string read_value;
BOOST_CHECK(handler2->Read(key, read_value));
BOOST_CHECK_EQUAL(read_value, value2);
}

#endif // USE_SQLITE


Expand Down

0 comments on commit 3d23c0f

Please sign in to comment.