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
11
15{
16 MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
17
18public:
20 explicit SteadyClockContext() { (*this) += 0s; }
21
24
27
29 void operator+=(std::chrono::milliseconds d)
30 {
31 Assert(d >= 0s); // Steady time can only increase monotonically.
32 t += d;
34 }
35};
36
40{
41 NodeSeconds m_t{std::chrono::seconds::max()};
42
43public:
45 explicit NodeClockContext(NodeSeconds init_time) { set(init_time); }
46 explicit NodeClockContext(std::chrono::seconds init_time) { set(init_time); }
48 explicit NodeClockContext() { set(++Now<NodeSeconds>().time_since_epoch()); }
49
52
55
58 void set(std::chrono::seconds t) { set(NodeSeconds{t}); }
59
61 void operator+=(std::chrono::seconds d) { set(m_t += d); }
62 void operator-=(std::chrono::seconds d) { set(m_t -= d); }
63};
64
65#endif // BITCOIN_TEST_UTIL_TIME_H
#define Assert(val)
Identity function.
Definition: check.h:113
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:40
void set(std::chrono::seconds t)
Definition: time.h:58
void operator+=(std::chrono::seconds d)
Change mocktime by the given duration delta.
Definition: time.h:61
NodeClockContext & operator=(const NodeClockContext &)=delete
NodeClockContext()
Initialize with current time, using the next tick to avoid going back by rounding to seconds.
Definition: time.h:48
void set(NodeSeconds t)
Set mocktime.
Definition: time.h:57
void operator-=(std::chrono::seconds d)
Definition: time.h:62
NodeClockContext(std::chrono::seconds init_time)
Definition: time.h:46
NodeClockContext(const NodeClockContext &)=delete
~NodeClockContext()
Unset mocktime.
Definition: time.h:51
NodeSeconds m_t
Definition: time.h:41
NodeClockContext(NodeSeconds init_time)
Initialize with the given time.
Definition: time.h:45
Helper to initialize the global MockableSteadyClock, let a duration elapse, and reset it after use in...
Definition: time.h:15
~SteadyClockContext()
Unset mocktime.
Definition: time.h:23
MockableSteadyClock::mock_time_point::duration t
Definition: time.h:16
SteadyClockContext & operator=(const SteadyClockContext &)=delete
void operator+=(std::chrono::milliseconds d)
Change mocktime by the given duration delta.
Definition: time.h:29
SteadyClockContext(const SteadyClockContext &)=delete
SteadyClockContext()
Initialize with INITIAL_MOCK_TIME.
Definition: time.h:20
static void SetMockTime(mock_time_point::duration mock_time_in)
Set mock time for testing.
Definition: time.cpp:70
static constexpr mock_time_point::duration INITIAL_MOCK_TIME
Definition: time.h:43
static void ClearMockTime()
Clear mock time, go back to system steady clock.
Definition: time.cpp:76
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:44
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:27