mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 03:52:49 +01:00
merge bitcoin#25101: Add mockable clock type
This commit is contained in:
parent
ccde10b914
commit
2d33cfba41
@ -47,7 +47,6 @@
|
|||||||
// trivial to get the mocked time from the server, nor is it needed for now, so
|
// trivial to get the mocked time from the server, nor is it needed for now, so
|
||||||
// just use a plain system_clock.
|
// just use a plain system_clock.
|
||||||
using CliClock = std::chrono::system_clock;
|
using CliClock = std::chrono::system_clock;
|
||||||
using CliSeconds = std::chrono::time_point<CliClock, std::chrono::seconds>;
|
|
||||||
|
|
||||||
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
||||||
UrlDecodeFn* const URL_DECODE = urlDecode;
|
UrlDecodeFn* const URL_DECODE = urlDecode;
|
||||||
@ -491,7 +490,7 @@ public:
|
|||||||
if (networkinfo["version"].get_int() < 200000) {
|
if (networkinfo["version"].get_int() < 200000) {
|
||||||
throw std::runtime_error("-netinfo requires dashd server to be running v20.0 and up");
|
throw std::runtime_error("-netinfo requires dashd server to be running v20.0 and up");
|
||||||
}
|
}
|
||||||
const int64_t time_now{count_seconds(Now<CliSeconds>())};
|
const int64_t time_now{TicksSinceEpoch<std::chrono::seconds>(CliClock::now())};
|
||||||
|
|
||||||
// Count peer connection totals, and if DetailsRequested(), store peer data in a vector of structs.
|
// Count peer connection totals, and if DetailsRequested(), store peer data in a vector of structs.
|
||||||
for (const UniValue& peer : batch[ID_PEERINFO]["result"].getValues()) {
|
for (const UniValue& peer : batch[ID_PEERINFO]["result"].getValues()) {
|
||||||
|
@ -1525,8 +1525,12 @@ BOOST_AUTO_TEST_CASE(util_time_GetTime)
|
|||||||
for (const auto& num_sleep : {0ms, 1ms}) {
|
for (const auto& num_sleep : {0ms, 1ms}) {
|
||||||
UninterruptibleSleep(num_sleep);
|
UninterruptibleSleep(num_sleep);
|
||||||
BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
|
BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
|
||||||
|
BOOST_CHECK_EQUAL(111, Now<NodeSeconds>().time_since_epoch().count());
|
||||||
|
BOOST_CHECK_EQUAL(111, TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()));
|
||||||
|
BOOST_CHECK_EQUAL(111, TicksSinceEpoch<SecondsDouble>(Now<NodeSeconds>()));
|
||||||
BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
|
BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
|
||||||
BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
|
BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
|
||||||
|
BOOST_CHECK_EQUAL(111000, TicksSinceEpoch<std::chrono::milliseconds>(NodeClock::now()));
|
||||||
BOOST_CHECK_EQUAL(111000000, GetTime<std::chrono::microseconds>().count());
|
BOOST_CHECK_EQUAL(111000000, GetTime<std::chrono::microseconds>().count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,20 +66,16 @@ bool ChronoSanityCheck()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
NodeClock::time_point NodeClock::now() noexcept
|
||||||
T GetTime()
|
|
||||||
{
|
{
|
||||||
const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
|
const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
|
||||||
const auto ret{
|
const auto ret{
|
||||||
mocktime.count() ?
|
mocktime.count() ?
|
||||||
mocktime :
|
mocktime :
|
||||||
std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch())};
|
std::chrono::system_clock::now().time_since_epoch()};
|
||||||
assert(ret > 0s);
|
assert(ret > 0s);
|
||||||
return ret;
|
return time_point{ret};
|
||||||
}
|
};
|
||||||
template std::chrono::seconds GetTime();
|
|
||||||
template std::chrono::milliseconds GetTime();
|
|
||||||
template std::chrono::microseconds GetTime();
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static T GetSystemTime()
|
static T GetSystemTime()
|
||||||
|
@ -14,6 +14,16 @@
|
|||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
|
/** Mockable clock in the context of tests, otherwise the system clock */
|
||||||
|
struct NodeClock : public std::chrono::system_clock {
|
||||||
|
using time_point = std::chrono::time_point<NodeClock>;
|
||||||
|
/** Return current system time or mocked time, if set */
|
||||||
|
static time_point now() noexcept;
|
||||||
|
static std::time_t to_time_t(const time_point&) = delete; // unused
|
||||||
|
static time_point from_time_t(std::time_t) = delete; // unused
|
||||||
|
};
|
||||||
|
using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>;
|
||||||
|
|
||||||
using SteadyClock = std::chrono::steady_clock;
|
using SteadyClock = std::chrono::steady_clock;
|
||||||
using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
|
using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
|
||||||
using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
|
using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
|
||||||
@ -31,10 +41,10 @@ void UninterruptibleSleep(const std::chrono::microseconds& n);
|
|||||||
* This helper is used to convert durations/time_points before passing them over an
|
* This helper is used to convert durations/time_points before passing them over an
|
||||||
* interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI)
|
* interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI)
|
||||||
*/
|
*/
|
||||||
template <typename Clock>
|
template <typename Duration, typename Timepoint>
|
||||||
constexpr int64_t count_seconds(std::chrono::time_point<Clock, std::chrono::seconds> t)
|
constexpr auto TicksSinceEpoch(Timepoint t)
|
||||||
{
|
{
|
||||||
return t.time_since_epoch().count();
|
return std::chrono::time_point_cast<Duration>(t).time_since_epoch().count();
|
||||||
}
|
}
|
||||||
constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
|
constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
|
||||||
constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
|
constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
|
||||||
@ -49,7 +59,11 @@ inline double CountSecondsDouble(SecondsDouble t) { return t.count(); }
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* DEPRECATED
|
* DEPRECATED
|
||||||
* Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
|
* Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
|
||||||
|
* ClockType is
|
||||||
|
* - std::chrono::steady_clock for steady time
|
||||||
|
* - std::chrono::system_clock for system time
|
||||||
|
* - NodeClock for mockable system time
|
||||||
*/
|
*/
|
||||||
int64_t GetTime();
|
int64_t GetTime();
|
||||||
|
|
||||||
@ -72,9 +86,6 @@ void SetMockTime(std::chrono::seconds mock_time_in);
|
|||||||
/** For testing */
|
/** For testing */
|
||||||
std::chrono::seconds GetMockTime();
|
std::chrono::seconds GetMockTime();
|
||||||
|
|
||||||
/** Return system time (or mocked time, if set) */
|
|
||||||
template <typename T>
|
|
||||||
T GetTime();
|
|
||||||
/**
|
/**
|
||||||
* Return the current time point cast to the given precicion. Only use this
|
* Return the current time point cast to the given precicion. Only use this
|
||||||
* when an exact precicion is needed, otherwise use T::clock::now() directly.
|
* when an exact precicion is needed, otherwise use T::clock::now() directly.
|
||||||
@ -84,6 +95,12 @@ T Now()
|
|||||||
{
|
{
|
||||||
return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
|
return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
|
||||||
}
|
}
|
||||||
|
/** DEPRECATED, see GetTime */
|
||||||
|
template <typename T>
|
||||||
|
T GetTime()
|
||||||
|
{
|
||||||
|
return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date,Time}
|
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date,Time}
|
||||||
|
Loading…
Reference in New Issue
Block a user