Skip to content

Commit fa9c387

Browse files
author
MarcoFalke
committed
test: Introduce MockableSteadyClock::mock_time_point and ElapseSteady helper
This refactor clarifies that the MockableSteadyClock::mock_time_point has millisecond precision by defining a type an using it. Moreover, a ElapseSteady helper is added which can be re-used easily.
1 parent faf2d51 commit fa9c387

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

src/test/util/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_library(test_util STATIC EXCLUDE_FROM_ALL
1515
script.cpp
1616
setup_common.cpp
1717
str.cpp
18+
time.cpp
1819
transaction_utils.cpp
1920
txmempool.cpp
2021
validation.cpp

src/test/util/time.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <test/util/time.h>

src/test/util/time.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_TEST_UTIL_TIME_H
6+
#define BITCOIN_TEST_UTIL_TIME_H
7+
8+
#include <util/time.h>
9+
10+
struct ElapseSteady {
11+
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+
t += d;
19+
MockableSteadyClock::SetMockTime(t);
20+
}
21+
};
22+
23+
#endif // BITCOIN_TEST_UTIL_TIME_H

src/util/time.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread
2121

2222
static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
2323
std::atomic<bool> g_used_system_time{false};
24-
static std::atomic<std::chrono::milliseconds> g_mock_steady_time{}; //!< For testing
24+
static std::atomic<MockableSteadyClock::mock_time_point::duration> g_mock_steady_time{}; //!< For testing
2525

2626
NodeClock::time_point NodeClock::now() noexcept
2727
{
@@ -62,7 +62,7 @@ MockableSteadyClock::time_point MockableSteadyClock::now() noexcept
6262
return time_point{ret};
6363
};
6464

65-
void MockableSteadyClock::SetMockTime(std::chrono::milliseconds mock_time_in)
65+
void MockableSteadyClock::SetMockTime(mock_time_point::duration mock_time_in)
6666
{
6767
Assert(mock_time_in >= 0s);
6868
g_mock_steady_time.store(mock_time_in, std::memory_order_relaxed);

src/util/time.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ using SystemClock = std::chrono::system_clock;
3838
struct MockableSteadyClock : public std::chrono::steady_clock {
3939
using time_point = std::chrono::time_point<MockableSteadyClock>;
4040

41-
static constexpr std::chrono::milliseconds INITIAL_MOCK_TIME{1};
41+
using mock_time_point = std::chrono::time_point<MockableSteadyClock, std::chrono::milliseconds>;
42+
static constexpr mock_time_point::duration INITIAL_MOCK_TIME{1};
4243

4344
/** Return current system time or mocked time, if set */
4445
static time_point now() noexcept;
@@ -50,7 +51,7 @@ struct MockableSteadyClock : public std::chrono::steady_clock {
5051
* for testing.
5152
* To stop mocking, call ClearMockTime().
5253
*/
53-
static void SetMockTime(std::chrono::milliseconds mock_time_in);
54+
static void SetMockTime(mock_time_point::duration mock_time_in);
5455

5556
/** Clear mock time, go back to system steady clock. */
5657
static void ClearMockTime();

0 commit comments

Comments
 (0)