/root/bitcoin/src/test/util/time.h
Line | Count | Source |
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 | | |
11 | | /// CRTP Helper to limit a class to at most one at a time. |
12 | | template <class T> |
13 | | class LimitOne |
14 | | { |
15 | | public: |
16 | 125k | LimitOne() { Assert(g_T_available) = false; }_ZN8LimitOneI13FakeNodeClockEC2Ev Line | Count | Source | 16 | 93.7k | LimitOne() { Assert(g_T_available) = false; } |
_ZN8LimitOneI15FakeSteadyClockEC2Ev Line | Count | Source | 16 | 31.5k | LimitOne() { Assert(g_T_available) = false; } |
|
17 | 125k | ~LimitOne() { g_T_available = true; }_ZN8LimitOneI13FakeNodeClockED2Ev Line | Count | Source | 17 | 93.7k | ~LimitOne() { g_T_available = true; } |
_ZN8LimitOneI15FakeSteadyClockED2Ev Line | Count | Source | 17 | 31.5k | ~LimitOne() { g_T_available = true; } |
|
18 | | LimitOne(const LimitOne&) = delete; |
19 | | LimitOne& operator=(const LimitOne&) = delete; |
20 | | |
21 | | private: |
22 | | static inline bool g_T_available{true}; |
23 | | }; |
24 | | |
25 | | |
26 | | /// Helper to initialize the global MockableSteadyClock, let a duration elapse, |
27 | | /// and reset it after use in a test. |
28 | | class FakeSteadyClock : public LimitOne<FakeSteadyClock> |
29 | | { |
30 | | MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME}; |
31 | | |
32 | | public: |
33 | | /** Initialize with INITIAL_MOCK_TIME. */ |
34 | 31.5k | explicit FakeSteadyClock() { (*this) += 0s; } |
35 | | |
36 | | /** Unset mocktime */ |
37 | 31.5k | ~FakeSteadyClock() { MockableSteadyClock::ClearMockTime(); } |
38 | | |
39 | | FakeSteadyClock(const FakeSteadyClock&) = delete; |
40 | | FakeSteadyClock& operator=(const FakeSteadyClock&) = delete; |
41 | | |
42 | | /** Change mocktime by the given duration delta */ |
43 | | void operator+=(std::chrono::milliseconds d) |
44 | 91.8k | { |
45 | 91.8k | Assert(d >= 0s); // Steady time can only increase monotonically. |
46 | 91.8k | t += d; |
47 | 91.8k | MockableSteadyClock::SetMockTime(t); |
48 | 91.8k | } |
49 | | }; |
50 | | |
51 | | /// Helper to initialize the global NodeClock, let a duration elapse, |
52 | | /// and reset it after use in a test. |
53 | | class FakeNodeClock : public LimitOne<FakeNodeClock> |
54 | | { |
55 | | NodeSeconds m_t{std::chrono::seconds::max()}; |
56 | | |
57 | | public: |
58 | | /// Initialize with the given time. |
59 | 73.8k | explicit FakeNodeClock(NodeSeconds init_time) { set(init_time); } |
60 | 19.9k | explicit FakeNodeClock(std::chrono::seconds init_time) { set(init_time); } |
61 | | /// Initialize with current time. |
62 | 0 | explicit FakeNodeClock() { set(Now<NodeSeconds>()); } |
63 | | |
64 | | /// Unset mocktime. |
65 | 93.7k | ~FakeNodeClock() { set(0s); } |
66 | | |
67 | | FakeNodeClock(const FakeNodeClock&) = delete; |
68 | | FakeNodeClock& operator=(const FakeNodeClock&) = delete; |
69 | | |
70 | | /// Set mocktime. |
71 | 378k | void set(NodeSeconds t) { SetMockTime(m_t = t); } |
72 | 113k | void set(std::chrono::seconds t) { set(NodeSeconds{t}); } |
73 | | |
74 | | /// Change mocktime by the given duration delta. |
75 | 79.5k | void operator+=(std::chrono::seconds d) { set(m_t += d); } |
76 | 0 | void operator-=(std::chrono::seconds d) { set(m_t -= d); } |
77 | | }; |
78 | | |
79 | | #endif // BITCOIN_TEST_UTIL_TIME_H |