mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
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.
This commit is contained in:
parent
faf2d512c5
commit
fa9c38794e
5 changed files with 34 additions and 4 deletions
|
@ -15,6 +15,7 @@ add_library(test_util STATIC EXCLUDE_FROM_ALL
|
|||
script.cpp
|
||||
setup_common.cpp
|
||||
str.cpp
|
||||
time.cpp
|
||||
transaction_utils.cpp
|
||||
txmempool.cpp
|
||||
validation.cpp
|
||||
|
|
5
src/test/util/time.cpp
Normal file
5
src/test/util/time.cpp
Normal file
|
@ -0,0 +1,5 @@
|
|||
// Copyright (c) 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/time.h>
|
23
src/test/util/time.h
Normal file
23
src/test/util/time.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
// Copyright (c) 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_TIME_H
|
||||
#define BITCOIN_TEST_UTIL_TIME_H
|
||||
|
||||
#include <util/time.h>
|
||||
|
||||
struct ElapseSteady {
|
||||
MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
|
||||
ElapseSteady()
|
||||
{
|
||||
(*this)(0s); // init
|
||||
}
|
||||
void operator()(std::chrono::milliseconds d)
|
||||
{
|
||||
t += d;
|
||||
MockableSteadyClock::SetMockTime(t);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // BITCOIN_TEST_UTIL_TIME_H
|
|
@ -21,7 +21,7 @@ void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread
|
|||
|
||||
static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
|
||||
std::atomic<bool> g_used_system_time{false};
|
||||
static std::atomic<std::chrono::milliseconds> g_mock_steady_time{}; //!< For testing
|
||||
static std::atomic<MockableSteadyClock::mock_time_point::duration> g_mock_steady_time{}; //!< For testing
|
||||
|
||||
NodeClock::time_point NodeClock::now() noexcept
|
||||
{
|
||||
|
@ -62,7 +62,7 @@ MockableSteadyClock::time_point MockableSteadyClock::now() noexcept
|
|||
return time_point{ret};
|
||||
};
|
||||
|
||||
void MockableSteadyClock::SetMockTime(std::chrono::milliseconds mock_time_in)
|
||||
void MockableSteadyClock::SetMockTime(mock_time_point::duration mock_time_in)
|
||||
{
|
||||
Assert(mock_time_in >= 0s);
|
||||
g_mock_steady_time.store(mock_time_in, std::memory_order_relaxed);
|
||||
|
|
|
@ -38,7 +38,8 @@ using SystemClock = std::chrono::system_clock;
|
|||
struct MockableSteadyClock : public std::chrono::steady_clock {
|
||||
using time_point = std::chrono::time_point<MockableSteadyClock>;
|
||||
|
||||
static constexpr std::chrono::milliseconds INITIAL_MOCK_TIME{1};
|
||||
using mock_time_point = std::chrono::time_point<MockableSteadyClock, std::chrono::milliseconds>;
|
||||
static constexpr mock_time_point::duration INITIAL_MOCK_TIME{1};
|
||||
|
||||
/** Return current system time or mocked time, if set */
|
||||
static time_point now() noexcept;
|
||||
|
@ -50,7 +51,7 @@ struct MockableSteadyClock : public std::chrono::steady_clock {
|
|||
* for testing.
|
||||
* To stop mocking, call ClearMockTime().
|
||||
*/
|
||||
static void SetMockTime(std::chrono::milliseconds mock_time_in);
|
||||
static void SetMockTime(mock_time_point::duration mock_time_in);
|
||||
|
||||
/** Clear mock time, go back to system steady clock. */
|
||||
static void ClearMockTime();
|
||||
|
|
Loading…
Add table
Reference in a new issue