Bitcoin Core 31.99.0
P2P Digital Currency
time.h
Go to the documentation of this file.
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/check.h>
9#include <util/time.h>
10
12template <class T>
14{
15public:
18 LimitOne(const LimitOne&) = delete;
19 LimitOne& operator=(const LimitOne&) = delete;
20
21private:
22 static inline bool g_T_available{true};
23};
24
25
28class FakeSteadyClock : public LimitOne<FakeSteadyClock>
29{
30 MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
31
32public:
34 explicit FakeSteadyClock() { (*this) += 0s; }
35
38
41
43 void operator+=(std::chrono::milliseconds d)
44 {
45 Assert(d >= 0s); // Steady time can only increase monotonically.
46 t += d;
48 }
49};
50
53class FakeNodeClock : public LimitOne<FakeNodeClock>
54{
55 NodeSeconds m_t{std::chrono::seconds::max()};
56
57public:
59 explicit FakeNodeClock(NodeSeconds init_time) { set(init_time); }
60 explicit FakeNodeClock(std::chrono::seconds init_time) { set(init_time); }
62 explicit FakeNodeClock() { set(Now<NodeSeconds>()); }
63
66
67 FakeNodeClock(const FakeNodeClock&) = delete;
69
72 void set(std::chrono::seconds t) { set(NodeSeconds{t}); }
73
75 void operator+=(std::chrono::seconds d) { set(m_t += d); }
76 void operator-=(std::chrono::seconds d) { set(m_t -= d); }
77};
78
79#endif // BITCOIN_TEST_UTIL_TIME_H
#define Assert(val)
Identity function.
Definition: check.h:116
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:54
NodeSeconds m_t
Definition: time.h:55
void set(NodeSeconds t)
Set mocktime.
Definition: time.h:71
void operator+=(std::chrono::seconds d)
Change mocktime by the given duration delta.
Definition: time.h:75
FakeNodeClock & operator=(const FakeNodeClock &)=delete
FakeNodeClock(NodeSeconds init_time)
Initialize with the given time.
Definition: time.h:59
FakeNodeClock(const FakeNodeClock &)=delete
void operator-=(std::chrono::seconds d)
Definition: time.h:76
void set(std::chrono::seconds t)
Definition: time.h:72
FakeNodeClock()
Initialize with current time.
Definition: time.h:62
~FakeNodeClock()
Unset mocktime.
Definition: time.h:65
FakeNodeClock(std::chrono::seconds init_time)
Definition: time.h:60
Helper to initialize the global MockableSteadyClock, let a duration elapse, and reset it after use in...
Definition: time.h:29
FakeSteadyClock & operator=(const FakeSteadyClock &)=delete
void operator+=(std::chrono::milliseconds d)
Change mocktime by the given duration delta.
Definition: time.h:43
FakeSteadyClock()
Initialize with INITIAL_MOCK_TIME.
Definition: time.h:34
FakeSteadyClock(const FakeSteadyClock &)=delete
MockableSteadyClock::mock_time_point::duration t
Definition: time.h:30
~FakeSteadyClock()
Unset mocktime.
Definition: time.h:37
CRTP Helper to limit a class to at most one at a time.
Definition: time.h:14
static bool g_T_available
Definition: time.h:22
LimitOne()
Definition: time.h:16
LimitOne & operator=(const LimitOne &)=delete
~LimitOne()
Definition: time.h:17
LimitOne(const LimitOne &)=delete
static void SetMockTime(mock_time_point::duration mock_time_in)
Set mock time for testing.
Definition: time.cpp:78
static constexpr mock_time_point::duration INITIAL_MOCK_TIME
Definition: time.h:51
static void ClearMockTime()
Clear mock time, go back to system steady clock.
Definition: time.cpp:84
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:52
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:35