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