time.h raw

   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      LimitOne() { Assert(g_T_available) = false; }
  17      ~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      explicit FakeSteadyClock() { (*this) += 0s; }
  35  
  36      /** Unset mocktime */
  37      ~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      {
  45          Assert(d >= 0s); // Steady time can only increase monotonically.
  46          t += d;
  47          MockableSteadyClock::SetMockTime(t);
  48      }
  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      explicit FakeNodeClock(NodeSeconds init_time) { set(init_time); }
  60      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      ~FakeNodeClock() { set(0s); }
  66  
  67      FakeNodeClock(const FakeNodeClock&) = delete;
  68      FakeNodeClock& operator=(const FakeNodeClock&) = delete;
  69  
  70      /// Set mocktime.
  71      void set(NodeSeconds t) { SetMockTime(m_t = t); }
  72      void set(std::chrono::seconds t) { set(NodeSeconds{t}); }
  73  
  74      /// Change mocktime by the given duration delta.
  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
  80