Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_CXX_TESTS_UNIT_CLOCK_FIXTURE_HPP |
| 23 | #define NDN_CXX_TESTS_UNIT_CLOCK_FIXTURE_HPP |
| 24 | |
| 25 | #include "ndn-cxx/util/time-unit-test-clock.hpp" |
| 26 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 27 | namespace ndn::tests { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 28 | |
| 29 | /** \brief A test fixture that overrides steady clock and system clock. |
| 30 | */ |
| 31 | class ClockFixture |
| 32 | { |
| 33 | public: |
| 34 | virtual |
| 35 | ~ClockFixture(); |
| 36 | |
| 37 | /** \brief Advance steady and system clocks. |
| 38 | * |
| 39 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
| 40 | * afterTick() is called after each tick. |
| 41 | * |
| 42 | * Exceptions thrown during I/O events are propagated to the caller. |
| 43 | * Clock advancement will stop in the event of an exception. |
| 44 | */ |
| 45 | void |
| 46 | advanceClocks(time::nanoseconds tick, size_t nTicks = 1) |
| 47 | { |
| 48 | advanceClocks(tick, tick * nTicks); |
| 49 | } |
| 50 | |
| 51 | /** \brief Advance steady and system clocks. |
| 52 | * |
| 53 | * Clocks are advanced in increments of \p tick for \p total time. |
| 54 | * The last increment might be shorter than \p tick. |
| 55 | * afterTick() is called after each tick. |
| 56 | * |
| 57 | * Exceptions thrown during I/O events are propagated to the caller. |
| 58 | * Clock advancement will stop in the event of an exception. |
| 59 | */ |
| 60 | void |
| 61 | advanceClocks(time::nanoseconds tick, time::nanoseconds total); |
| 62 | |
| 63 | protected: |
| 64 | ClockFixture(); |
| 65 | |
| 66 | private: |
| 67 | /** \brief Called by advanceClocks() after each clock advancement (tick). |
| 68 | * |
| 69 | * The base class implementation is a no-op. |
| 70 | */ |
| 71 | virtual void |
| 72 | afterTick() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | protected: |
| 77 | shared_ptr<time::UnitTestSteadyClock> m_steadyClock; |
| 78 | shared_ptr<time::UnitTestSystemClock> m_systemClock; |
| 79 | }; |
| 80 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 81 | } // namespace ndn::tests |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 82 | |
| 83 | #endif // NDN_CXX_TESTS_UNIT_CLOCK_FIXTURE_HPP |