LCOV - code coverage report
Current view: top level - src/test/util - time.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 15 15
Test Date: 2026-06-13 06:51:50 Functions: 100.0 % 8 8
Branches: 42.9 % 14 6

             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                 :             : /// CRTP Helper to limit a class to at most one at a time.
      12                 :             : template <class T>
      13                 :             : class LimitOne
      14                 :             : {
      15                 :             : public:
      16         [ -  + ]:       63751 :     LimitOne() { Assert(g_T_available) = false; }
      17                 :       63751 :     ~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 SteadyClockContext : public LimitOne<SteadyClockContext>
      29                 :             : {
      30                 :             :     MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
      31                 :             : 
      32                 :             : public:
      33                 :             :     /** Initialize with INITIAL_MOCK_TIME. */
      34         [ +  - ]:         726 :     explicit SteadyClockContext() { (*this) += 0s; }
      35                 :             : 
      36                 :             :     /** Unset mocktime */
      37                 :         726 :     ~SteadyClockContext() { MockableSteadyClock::ClearMockTime(); }
      38                 :             : 
      39                 :             :     SteadyClockContext(const SteadyClockContext&) = delete;
      40                 :             :     SteadyClockContext& operator=(const SteadyClockContext&) = delete;
      41                 :             : 
      42                 :             :     /** Change mocktime by the given duration delta */
      43                 :         726 :     void operator+=(std::chrono::milliseconds d)
      44                 :             :     {
      45         [ -  + ]:         726 :         Assert(d >= 0s); // Steady time can only increase monotonically.
      46                 :         726 :         t += d;
      47                 :         726 :         MockableSteadyClock::SetMockTime(t);
      48                 :         726 :     }
      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                 :      106872 :     explicit FakeNodeClock(NodeSeconds init_time) { set(init_time); }
      60         [ +  - ]:        9589 :     explicit FakeNodeClock(std::chrono::seconds init_time) { set(init_time); }
      61                 :             :     /// Initialize with current time.
      62                 :             :     explicit FakeNodeClock() { set(Now<NodeSeconds>()); }
      63                 :             : 
      64                 :             :     /// Unset mocktime.
      65                 :       63025 :     ~FakeNodeClock() { set(0s); }
      66                 :             : 
      67                 :             :     FakeNodeClock(const FakeNodeClock&) = delete;
      68                 :             :     FakeNodeClock& operator=(const FakeNodeClock&) = delete;
      69                 :             : 
      70                 :             :     /// Set mocktime.
      71         [ +  - ]:      199085 :     void set(NodeSeconds t) { SetMockTime(m_t = t); }
           [ +  -  -  - ]
      72                 :       72614 :     void set(std::chrono::seconds t) { set(NodeSeconds{t}); }
      73                 :             : 
      74                 :             :     /// Change mocktime by the given duration delta.
      75                 :       17046 :     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
        

Generated by: LCOV version 2.0-1