Branch data Line data Source code
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 : :
12 : : /// Helper to initialize the global MockableSteadyClock, let a duration elapse,
13 : : /// and reset it after use in a test.
14 : : class SteadyClockContext
15 : : {
16 : : MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
17 : :
18 : : public:
19 : : /** Initialize with INITIAL_MOCK_TIME. */
20 : 861 : explicit SteadyClockContext() { (*this) += 0s; }
21 : :
22 : : /** Unset mocktime */
23 : 861 : ~SteadyClockContext() { MockableSteadyClock::ClearMockTime(); }
24 : :
25 : : SteadyClockContext(const SteadyClockContext&) = delete;
26 : : SteadyClockContext& operator=(const SteadyClockContext&) = delete;
27 : :
28 : : /** Change mocktime by the given duration delta */
29 : 861 : void operator+=(std::chrono::milliseconds d)
30 : : {
31 [ - + ]: 861 : Assert(d >= 0s); // Steady time can only increase monotonically.
32 : 861 : t += d;
33 : 861 : MockableSteadyClock::SetMockTime(t);
34 : 861 : }
35 : : };
36 : :
37 : : #endif // BITCOIN_TEST_UTIL_TIME_H
|