From 331a25cb16632042dd6782a9b62fcc5c8aa6da3b Mon Sep 17 00:00:00 2001 From: furszy Date: Fri, 6 Jun 2025 14:24:46 -0400 Subject: [PATCH] test: indexes, avoid creating threads when sync runs synchronously The indexes test call StartBackgroundSync(), which spawns a thread to run Sync(), only for the test thread to wait for it to complete by calling IndexWaitSynced(). So, since the sync is performed synchronously, we can skip the extra thread creation entirely and call Sync() directly. --- src/test/blockfilter_index_tests.cpp | 6 +----- src/test/coinstatsindex_tests.cpp | 8 ++------ src/test/txindex_tests.cpp | 6 +----- src/test/util/CMakeLists.txt | 1 - src/test/util/index.cpp | 22 ---------------------- src/test/util/index.h | 16 ---------------- 6 files changed, 4 insertions(+), 55 deletions(-) delete mode 100644 src/test/util/index.cpp delete mode 100644 src/test/util/index.h diff --git a/src/test/blockfilter_index_tests.cpp b/src/test/blockfilter_index_tests.cpp index 81119f52b1..1fc5bbdf3f 100644 --- a/src/test/blockfilter_index_tests.cpp +++ b/src/test/blockfilter_index_tests.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include @@ -143,10 +142,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup) // BlockUntilSyncedToCurrentChain should return false before index is started. BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain()); - BOOST_REQUIRE(filter_index.StartBackgroundSync()); - - // Allow filter index to catch up with the block index. - IndexWaitSynced(filter_index, *Assert(m_node.shutdown_signal)); + filter_index.Sync(); // Check that filter index has all blocks that were in the chain before it started. { diff --git a/src/test/coinstatsindex_tests.cpp b/src/test/coinstatsindex_tests.cpp index e09aad05e9..7d54f0fdf1 100644 --- a/src/test/coinstatsindex_tests.cpp +++ b/src/test/coinstatsindex_tests.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -33,9 +32,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup) // is started. BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain()); - BOOST_REQUIRE(coin_stats_index.StartBackgroundSync()); - - IndexWaitSynced(coin_stats_index, *Assert(m_node.shutdown_signal)); + coin_stats_index.Sync(); // Check that CoinStatsIndex works for genesis block. const CBlockIndex* genesis_block_index; @@ -85,8 +82,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_unclean_shutdown, TestChain100Setup) { CoinStatsIndex index{interfaces::MakeChain(m_node), 1 << 20}; BOOST_REQUIRE(index.Init()); - BOOST_REQUIRE(index.StartBackgroundSync()); - IndexWaitSynced(index, *Assert(m_node.shutdown_signal)); + index.Sync(); std::shared_ptr new_block; CBlockIndex* new_block_index = nullptr; { diff --git a/src/test/txindex_tests.cpp b/src/test/txindex_tests.cpp index 9ee5387830..e06735e186 100644 --- a/src/test/txindex_tests.cpp +++ b/src/test/txindex_tests.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -30,10 +29,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup) // BlockUntilSyncedToCurrentChain should return false before txindex is started. BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain()); - BOOST_REQUIRE(txindex.StartBackgroundSync()); - - // Allow tx index to catch up with the block index. - IndexWaitSynced(txindex, *Assert(m_node.shutdown_signal)); + txindex.Sync(); // Check that txindex excludes genesis block transactions. const CBlock& genesis_block = Params().GenesisBlock(); diff --git a/src/test/util/CMakeLists.txt b/src/test/util/CMakeLists.txt index 3a6e31c720..32396c41c3 100644 --- a/src/test/util/CMakeLists.txt +++ b/src/test/util/CMakeLists.txt @@ -6,7 +6,6 @@ add_library(test_util STATIC EXCLUDE_FROM_ALL blockfilter.cpp coins.cpp coverage.cpp - index.cpp json.cpp logging.cpp mining.cpp diff --git a/src/test/util/index.cpp b/src/test/util/index.cpp deleted file mode 100644 index cfeba35756..0000000000 --- a/src/test/util/index.cpp +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#include - -#include -#include -#include -#include - -void IndexWaitSynced(const BaseIndex& index, const util::SignalInterrupt& interrupt) -{ - while (!index.BlockUntilSyncedToCurrentChain()) { - // Assert shutdown was not requested to abort the test, instead of looping forever, in case - // there was an unexpected error in the index that caused it to stop syncing and request a shutdown. - Assert(!interrupt); - - UninterruptibleSleep(100ms); - } - assert(index.GetSummary().synced); -} diff --git a/src/test/util/index.h b/src/test/util/index.h deleted file mode 100644 index a3bd1dddc3..0000000000 --- a/src/test/util/index.h +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_TEST_UTIL_INDEX_H -#define BITCOIN_TEST_UTIL_INDEX_H - -class BaseIndex; -namespace util { -class SignalInterrupt; -} // namespace util - -/** Block until the index is synced to the current chain */ -void IndexWaitSynced(const BaseIndex& index, const util::SignalInterrupt& interrupt); - -#endif // BITCOIN_TEST_UTIL_INDEX_H