diff --git a/src/test/util/time.h b/src/test/util/time.h index 0d93a61cb31..2bc2553fea2 100644 --- a/src/test/util/time.h +++ b/src/test/util/time.h @@ -8,10 +8,24 @@ #include #include +/// CRTP Helper to limit a class to at most one at a time. +template +class LimitOne +{ +public: + LimitOne() { Assert(g_T_available) = false; } + ~LimitOne() { g_T_available = true; } + LimitOne(const LimitOne&) = delete; + LimitOne& operator=(const LimitOne&) = delete; + +private: + static inline bool g_T_available{true}; +}; + /// Helper to initialize the global MockableSteadyClock, let a duration elapse, /// and reset it after use in a test. -class SteadyClockContext +class SteadyClockContext : public LimitOne { MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME}; @@ -36,7 +50,7 @@ public: /// Helper to initialize the global NodeClock, let a duration elapse, /// and reset it after use in a test. -class FakeNodeClock +class FakeNodeClock : public LimitOne { NodeSeconds m_t{std::chrono::seconds::max()};