Skip to content

Commit 3568dce

Browse files
committed
tests: Add BerkeleyRO to db prefix tests
1 parent 70cfbfd commit 3568dce

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/wallet/test/db_tests.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifdef USE_SQLITE
1919
#include <wallet/sqlite.h>
2020
#endif
21+
#include <wallet/migrate.h>
2122
#include <wallet/test/util.h>
2223
#include <wallet/walletutil.h> // for WALLET_FLAG_DESCRIPTORS
2324

@@ -134,6 +135,8 @@ static std::vector<std::unique_ptr<WalletDatabase>> TestDatabases(const fs::path
134135
bilingual_str error;
135136
#ifdef USE_BDB
136137
dbs.emplace_back(MakeBerkeleyDatabase(path_root / "bdb", options, status, error));
138+
// Needs BDB to make the DB to read
139+
dbs.emplace_back(std::make_unique<BerkeleyRODatabase>(BDBDataFile(path_root / "bdb"), /*open=*/false));
137140
#endif
138141
#ifdef USE_SQLITE
139142
dbs.emplace_back(MakeSQLiteDatabase(path_root / "sqlite", options, status, error));
@@ -148,11 +151,16 @@ BOOST_AUTO_TEST_CASE(db_cursor_prefix_range_test)
148151
for (const auto& database : TestDatabases(m_path_root)) {
149152
std::vector<std::string> prefixes = {"", "FIRST", "SECOND", "P\xfe\xff", "P\xff\x01", "\xff\xff"};
150153

151-
// Write elements to it
152154
std::unique_ptr<DatabaseBatch> handler = Assert(database)->MakeBatch();
153-
for (unsigned int i = 0; i < 10; i++) {
154-
for (const auto& prefix : prefixes) {
155-
BOOST_CHECK(handler->Write(std::make_pair(prefix, i), i));
155+
if (dynamic_cast<BerkeleyRODatabase*>(database.get())) {
156+
// For BerkeleyRO, open the file now. This must happen after BDB has written to the file
157+
database->Open();
158+
} else {
159+
// Write elements to it if not berkeleyro
160+
for (unsigned int i = 0; i < 10; i++) {
161+
for (const auto& prefix : prefixes) {
162+
BOOST_CHECK(handler->Write(std::make_pair(prefix, i), i));
163+
}
156164
}
157165
}
158166

@@ -180,6 +188,8 @@ BOOST_AUTO_TEST_CASE(db_cursor_prefix_range_test)
180188
// Let's now read it once more, it should return DONE
181189
BOOST_CHECK(cursor->Next(key, value) == DatabaseCursor::Status::DONE);
182190
}
191+
handler.reset();
192+
database->Close();
183193
}
184194
}
185195

@@ -199,13 +209,23 @@ BOOST_AUTO_TEST_CASE(db_cursor_prefix_byte_test)
199209
ffs{StringData("\xff\xffsuffix"), StringData("ffs")};
200210
for (const auto& database : TestDatabases(m_path_root)) {
201211
std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
202-
for (const auto& [k, v] : {e, p, ps, f, fs, ff, ffs}) {
203-
batch->Write(Span{k}, Span{v});
212+
213+
if (dynamic_cast<BerkeleyRODatabase*>(database.get())) {
214+
// For BerkeleyRO, open the file now. This must happen after BDB has written to the file
215+
database->Open();
216+
} else {
217+
// Write elements to it if not berkeleyro
218+
for (const auto& [k, v] : {e, p, ps, f, fs, ff, ffs}) {
219+
batch->Write(Span{k}, Span{v});
220+
}
204221
}
222+
205223
CheckPrefix(*batch, StringBytes(""), {e, p, ps, f, fs, ff, ffs});
206224
CheckPrefix(*batch, StringBytes("prefix"), {p, ps});
207225
CheckPrefix(*batch, StringBytes("\xff"), {f, fs, ff, ffs});
208226
CheckPrefix(*batch, StringBytes("\xff\xff"), {ff, ffs});
227+
batch.reset();
228+
database->Close();
209229
}
210230
}
211231

@@ -215,6 +235,10 @@ BOOST_AUTO_TEST_CASE(db_availability_after_write_error)
215235
// To simulate the behavior, record overwrites are disallowed, and the test verifies
216236
// that the database remains active after failing to store an existing record.
217237
for (const auto& database : TestDatabases(m_path_root)) {
238+
if (dynamic_cast<BerkeleyRODatabase*>(database.get())) {
239+
// Skip this test if BerkeleyRO
240+
continue;
241+
}
218242
// Write original record
219243
std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
220244
std::string key = "key";
@@ -243,6 +267,10 @@ BOOST_AUTO_TEST_CASE(erase_prefix)
243267
auto make_key = [](std::string type, std::string id) { return std::make_pair(type, id); };
244268

245269
for (const auto& database : TestDatabases(m_path_root)) {
270+
if (dynamic_cast<BerkeleyRODatabase*>(database.get())) {
271+
// Skip this test if BerkeleyRO
272+
continue;
273+
}
246274
std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
247275

248276
// Write two entries with the same key type prefix, a third one with a different prefix

0 commit comments

Comments
 (0)