diff options
Diffstat (limited to 'src/wallet/test/db_tests.cpp')
-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 |