File tree Expand file tree Collapse file tree 5 files changed +34
-4
lines changed Expand file tree Collapse file tree 5 files changed +34
-4
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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>
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread
2121
2222static std::atomic<std::chrono::seconds> g_mock_time{}; // !< For testing
2323std::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
2626NodeClock::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);
Original file line number Diff line number Diff line change @@ -38,7 +38,8 @@ using SystemClock = std::chrono::system_clock;
3838struct 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 ();
You can’t perform that action at this time.
0 commit comments