Skip to content

Commit 41f891d

Browse files
committed
tests: Skip SQLite fsyncs while testing
Since we want tests to run quickly, and since tests do a lot more db operations than expected we expect to see in actual usage, we disable sqlite's syncing behavior to make db operations run much faster. This syncing behavior is necessary for normal operation as it helps guarantee that data won't become lost or corrupted, but in tests, we don't care about that.
1 parent cb79cab commit 41f891d

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

src/dummywallet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void DummyWalletInit::AddWalletOptions(ArgsManager& argsman) const
5050
"-flushwallet",
5151
"-privdb",
5252
"-walletrejectlongchains",
53+
"-unsafesqlitesync",
5354
});
5455
}
5556

src/wallet/init.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ void WalletInit::AddWalletOptions(ArgsManager& argsman) const
8282
argsman.AddHiddenArgs({"-dblogsize", "-flushwallet", "-privdb"});
8383
#endif
8484

85+
#ifdef USE_SQLITE
86+
argsman.AddArg("-unsafesqlitesync", "Set SQLite synchronous=OFF to disable waiting for the database to sync to disk. This is unsafe and can cause data loss and corruption. This option is only used by tests to improve their performance (default: false)", ArgsManager::ALLOW_BOOL | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
87+
#else
88+
argsman.AddHiddenArgs({"-unsafesqlitesync"});
89+
#endif
90+
8591
argsman.AddArg("-walletrejectlongchains", strprintf("Wallet will not create transactions that violate mempool chain limits (default: %u)", DEFAULT_WALLET_REJECT_LONG_CHAINS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
8692

8793
argsman.AddHiddenArgs({"-zapwallettxes"});

src/wallet/sqlite.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ void SQLiteDatabase::Open()
233233
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable fullfsync: %s\n", sqlite3_errstr(ret)));
234234
}
235235

236+
if (gArgs.GetBoolArg("-unsafesqlitesync", false)) {
237+
// Use normal synchronous mode for the journal
238+
LogPrintf("WARNING SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.\n");
239+
ret = sqlite3_exec(m_db, "PRAGMA synchronous = OFF", nullptr, nullptr, nullptr);
240+
if (ret != SQLITE_OK) {
241+
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set synchronous mode to OFF: %s\n", sqlite3_errstr(ret)));
242+
}
243+
}
244+
236245
// Make the table for our key-value pairs
237246
// First check that the main table exists
238247
sqlite3_stmt* check_main_stmt{nullptr};

src/wallet/test/wallet_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup)
695695
//! rescanning where new transactions in new blocks could be lost.
696696
BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
697697
{
698+
gArgs.ForceSetArg("-unsafesqlitesync", "1");
698699
// Create new wallet with known key and unload it.
699700
auto wallet = TestLoadWallet(*m_node.chain);
700701
CKey key;
@@ -790,6 +791,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
790791

791792
BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
792793
{
794+
gArgs.ForceSetArg("-unsafesqlitesync", "1");
793795
auto wallet = TestLoadWallet(*m_node.chain);
794796
CKey key;
795797
key.MakeNewKey(true);

test/functional/test_framework/util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ def write_config(config_path, *, n, chain, extra_config=""):
374374
f.write("upnp=0\n")
375375
f.write("natpmp=0\n")
376376
f.write("shrinkdebugfile=0\n")
377+
# To improve SQLite wallet performance so that the tests don't timeout, use -unsafesqlitesync
378+
f.write("unsafesqlitesync=1\n")
377379
f.write(extra_config)
378380

379381

0 commit comments

Comments
 (0)