mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-06 11:13:02 +02:00
test: Turn ElapseSteady into SteadyClockContext
This commit is contained in:
@@ -172,7 +172,7 @@ FUZZ_TARGET(p2p_headers_presync, .init = initialize)
|
|||||||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
||||||
// The steady clock is currently only used for logging, so a constant
|
// The steady clock is currently only used for logging, so a constant
|
||||||
// time-point seems acceptable for now.
|
// time-point seems acceptable for now.
|
||||||
ElapseSteady elapse_steady{};
|
SteadyClockContext steady_ctx{};
|
||||||
|
|
||||||
ChainstateManager& chainman = *g_testing_setup->m_node.chainman;
|
ChainstateManager& chainman = *g_testing_setup->m_node.chainman;
|
||||||
CBlockHeader base{chainman.GetParams().GenesisBlock()};
|
CBlockHeader base{chainman.GetParams().GenesisBlock()};
|
||||||
|
|||||||
@@ -5,16 +5,30 @@
|
|||||||
#ifndef BITCOIN_TEST_UTIL_TIME_H
|
#ifndef BITCOIN_TEST_UTIL_TIME_H
|
||||||
#define BITCOIN_TEST_UTIL_TIME_H
|
#define BITCOIN_TEST_UTIL_TIME_H
|
||||||
|
|
||||||
|
#include <util/check.h>
|
||||||
#include <util/time.h>
|
#include <util/time.h>
|
||||||
|
|
||||||
struct ElapseSteady {
|
|
||||||
|
/// Helper to initialize the global MockableSteadyClock, let a duration elapse,
|
||||||
|
/// and reset it after use in a test.
|
||||||
|
class SteadyClockContext
|
||||||
|
{
|
||||||
MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
|
MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
|
||||||
ElapseSteady()
|
|
||||||
{
|
public:
|
||||||
(*this)(0s); // init
|
/** Initialize with INITIAL_MOCK_TIME. */
|
||||||
}
|
explicit SteadyClockContext() { (*this) += 0s; }
|
||||||
void operator()(std::chrono::milliseconds d)
|
|
||||||
|
/** Unset mocktime */
|
||||||
|
~SteadyClockContext() { MockableSteadyClock::ClearMockTime(); }
|
||||||
|
|
||||||
|
SteadyClockContext(const SteadyClockContext&) = delete;
|
||||||
|
SteadyClockContext& operator=(const SteadyClockContext&) = delete;
|
||||||
|
|
||||||
|
/** Change mocktime by the given duration delta */
|
||||||
|
void operator+=(std::chrono::milliseconds d)
|
||||||
{
|
{
|
||||||
|
Assert(d >= 0s); // Steady time can only increase monotonically.
|
||||||
t += d;
|
t += d;
|
||||||
MockableSteadyClock::SetMockTime(t);
|
MockableSteadyClock::SetMockTime(t);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user