diff options
author | furszy <matiasfurszyfer@protonmail.com> | 2023-12-23 12:49:41 -0300 |
---|---|---|
committer | Ava Chow <github@achow101.com> | 2024-02-06 12:24:36 -0500 |
commit | cfcb9b1ecf9be5267487713fa1e112ca09a1ae55 (patch) | |
tree | 3df3eb3589530e236fe6c7cc84b282a97c145508 /src | |
parent | 548ecd11553bea28bc79e6f9840836283e9c4e99 (diff) | |
download | bitcoin-cfcb9b1ecf9be5267487713fa1e112ca09a1ae55.tar.xz |
test: wallet, coverage for concurrent db transactions
Verifying that a database handler can't commit/abort changes
occurring in a different database handler.
Diffstat (limited to 'src')
-rw-r--r-- | src/wallet/test/db_tests.cpp | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/src/wallet/test/db_tests.cpp b/src/wallet/test/db_tests.cpp index 7e6219378f..adbbb94318 100644 --- a/src/wallet/test/db_tests.cpp +++ b/src/wallet/test/db_tests.cpp @@ -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 |