Skip to content

Commit ff09583

Browse files
committed
Merge bitcoin/bitcoin#34432: test: Turn ElapseSteady into SteadyClockContext
facb2aa test: Turn ElapseSteady into SteadyClockContext (MarcoFalke) Pull request description: `ElapseSteady` was introduced a while back, but is only used in one place. It makes more sense if this were a context manager, so that mocktime does not leak from one test into the next. So turn it into a context manager, rename it and allow easy time advancement via e.g. `steady_ctx += 1h`. ACKs for top commit: l0rinc: ACK facb2aa ismaelsadeeq: utACK facb2aa sedited: ACK facb2aa Tree-SHA512: 1df9cc9685d9be4d3ab8deafd99ac1a5ff752064ae54b83bacd6f44ba2c198b091558a306d49d8b1e2200ac669e95915cc792d589fb3a63b2bef7891d325a1e0
2 parents 81e67d9 + facb2aa commit ff09583

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/test/fuzz/p2p_headers_presync.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ FUZZ_TARGET(p2p_headers_presync, .init = initialize)
172172
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
173173
// The steady clock is currently only used for logging, so a constant
174174
// time-point seems acceptable for now.
175-
ElapseSteady elapse_steady{};
175+
SteadyClockContext steady_ctx{};
176176

177177
ChainstateManager& chainman = *g_testing_setup->m_node.chainman;
178178
CBlockHeader base{chainman.GetParams().GenesisBlock()};

src/test/util/time.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@
55
#ifndef BITCOIN_TEST_UTIL_TIME_H
66
#define BITCOIN_TEST_UTIL_TIME_H
77

8+
#include <util/check.h>
89
#include <util/time.h>
910

10-
struct ElapseSteady {
11+
12+
/// Helper to initialize the global MockableSteadyClock, let a duration elapse,
13+
/// and reset it after use in a test.
14+
class SteadyClockContext
15+
{
1116
MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
12-
ElapseSteady()
13-
{
14-
(*this)(0s); // init
15-
}
16-
void operator()(std::chrono::milliseconds d)
17+
18+
public:
19+
/** Initialize with INITIAL_MOCK_TIME. */
20+
explicit SteadyClockContext() { (*this) += 0s; }
21+
22+
/** Unset mocktime */
23+
~SteadyClockContext() { MockableSteadyClock::ClearMockTime(); }
24+
25+
SteadyClockContext(const SteadyClockContext&) = delete;
26+
SteadyClockContext& operator=(const SteadyClockContext&) = delete;
27+
28+
/** Change mocktime by the given duration delta */
29+
void operator+=(std::chrono::milliseconds d)
1730
{
31+
Assert(d >= 0s); // Steady time can only increase monotonically.
1832
t += d;
1933
MockableSteadyClock::SetMockTime(t);
2034
}

0 commit comments

Comments
 (0)