Skip to content

Commit

Permalink
test: wallet, coverage for concurrent db transactions
Browse files Browse the repository at this point in the history
Verifying that a database handler can't commit/abort changes
occurring in a different database handler.
  • Loading branch information
furszy authored and achow101 committed Feb 6, 2024
1 parent 548ecd1 commit cfcb9b1
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/wallet/test/db_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,48 @@ BOOST_AUTO_TEST_CASE(txn_close_failure_dangling_txn)
BOOST_CHECK(!batch2->Exists(key));
}

#endif // USE_SQLITE
BOOST_AUTO_TEST_CASE(concurrent_txn_dont_interfere)
{
std::string key = "key";
std::string value = "value";
std::string value2 = "value_2";

DatabaseOptions options;
DatabaseStatus status;
bilingual_str error;
const auto& database = MakeSQLiteDatabase(m_path_root / "sqlite", options, status, error);

std::unique_ptr<DatabaseBatch> handler = Assert(database)->MakeBatch();

// Verify concurrent db transactions does not interfere between each other.
// Start db txn, write key and check the key does exist within the db txn.
BOOST_CHECK(handler->TxnBegin());
BOOST_CHECK(handler->Write(key, value));
BOOST_CHECK(handler->Exists(key));

// But, the same key, does not exist in another handler
std::unique_ptr<DatabaseBatch> handler2 = Assert(database)->MakeBatch();
BOOST_CHECK(handler2->Exists(key));

// Attempt to commit the handler txn calling the handler2 methods.
// Which, must not be possible.
BOOST_CHECK(!handler2->TxnCommit());
BOOST_CHECK(!handler2->TxnAbort());

// Only the first handler can commit the changes.
BOOST_CHECK(handler->TxnCommit());
// And, once commit is completed, handler2 can read the record
std::string read_value;
BOOST_CHECK(handler2->Read(key, read_value));
BOOST_CHECK_EQUAL(read_value, value);

// Also, once txn is committed, single write statements are re-enabled.
// Which means that handler2 can read the record changes directly.
BOOST_CHECK(handler->Write(key, value2, /*fOverwrite=*/true));
BOOST_CHECK(handler2->Read(key, read_value));
BOOST_CHECK_EQUAL(read_value, value2);
}
#endif // USE_SQLITE

BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet

0 comments on commit cfcb9b1

Please sign in to comment.