Skip to content

Commit

Permalink
merge bitcoin#27988: Use same timeout for all index sync
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg authored and knst committed Aug 1, 2023
1 parent 560400b commit 62b006c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.test_util.include
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ EXTRA_LIBRARIES += \

TEST_UTIL_H = \
test/util/blockfilter.h \
test/util/index.h \
test/util/logging.h \
test/util/mining.h \
test/util/net.h \
Expand All @@ -21,6 +22,7 @@ libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAG
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libtest_util_a_SOURCES = \
test/util/blockfilter.cpp \
test/util/index.cpp \
test/util/logging.cpp \
test/util/mining.cpp \
test/util/net.cpp \
Expand Down
9 changes: 2 additions & 7 deletions src/test/blockfilter_index_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include <script/standard.h>
#include <spork.h>
#include <test/util/blockfilter.h>
#include <test/util/index.h>
#include <test/util/setup_common.h>
#include <util/time.h>
#include <validation.h>

#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -141,12 +141,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
BOOST_REQUIRE(filter_index.Start(::ChainstateActive()));

// Allow filter index to catch up with the block index.
constexpr auto timeout{10s};
const auto time_start{SteadyClock::now()};
while (!filter_index.BlockUntilSyncedToCurrentChain()) {
BOOST_REQUIRE(time_start + timeout > SteadyClock::now());
UninterruptibleSleep(std::chrono::milliseconds{100});
}
IndexWaitSynced(filter_index);

// Check that filter index has all blocks that were in the chain before it started.
{
Expand Down
15 changes: 1 addition & 14 deletions src/test/coinstatsindex_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,15 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <index/coinstatsindex.h>
#include <test/util/index.h>
#include <test/util/setup_common.h>
#include <util/time.h>
#include <validation.h>

#include <boost/test/unit_test.hpp>

#include <chrono>


BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)

static void IndexWaitSynced(BaseIndex& index)
{
// Allow the CoinStatsIndex to catch up with the block index that is syncing
// in a background thread.
const auto timeout = GetTime<std::chrono::seconds>() + 120s;
while (!index.BlockUntilSyncedToCurrentChain()) {
BOOST_REQUIRE(timeout > GetTime<std::chrono::milliseconds>());
UninterruptibleSleep(100ms);
}
}

BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
{
CoinStatsIndex coin_stats_index{1 << 20, true};
Expand Down
9 changes: 2 additions & 7 deletions src/test/txindex_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <chainparams.h>
#include <index/txindex.h>
#include <script/standard.h>
#include <test/util/index.h>
#include <test/util/setup_common.h>
#include <util/time.h>
#include <validation.h>

#include <boost/test/unit_test.hpp>
Expand All @@ -31,12 +31,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
BOOST_REQUIRE(txindex.Start(::ChainstateActive()));

// Allow tx index to catch up with the block index.
constexpr auto timeout{10s};
const auto time_start{SteadyClock::now()};
while (!txindex.BlockUntilSyncedToCurrentChain()) {
BOOST_REQUIRE(time_start + timeout > SteadyClock::now());
UninterruptibleSleep(std::chrono::milliseconds{100});
}
IndexWaitSynced(txindex);

// Check that txindex excludes genesis block transactions.
const CBlock& genesis_block = Params().GenesisBlock();
Expand Down
18 changes: 18 additions & 0 deletions src/test/util/index.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 <test/util/index.h>

#include <index/base.h>
#include <util/check.h>
#include <util/time.h>

void IndexWaitSynced(BaseIndex& index)
{
const auto timeout{SteadyClock::now() + 120s};
while (!index.BlockUntilSyncedToCurrentChain()) {
Assert(timeout > SteadyClock::now());
UninterruptibleSleep(100ms);
}
}
13 changes: 13 additions & 0 deletions src/test/util/index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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;

/** Block until the index is synced to the current chain */
void IndexWaitSynced(BaseIndex& index);

#endif // BITCOIN_TEST_UTIL_INDEX_H

0 comments on commit 62b006c

Please sign in to comment.