blob: d4a2c960022f4ab7aac7ee069a0fb00d96c6ba72 [file] [log] [blame]
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento3de7c522024-01-25 18:50:12 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05004 *
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 Pesavento47ce2ee2023-05-09 01:33:33 -040027namespace ndn::tests {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050028
29/** \brief A test fixture that overrides steady clock and system clock.
30 */
31class ClockFixture
32{
33public:
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
63protected:
64 ClockFixture();
65
66private:
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
76protected:
Davide Pesavento3de7c522024-01-25 18:50:12 -050077 std::shared_ptr<time::UnitTestSteadyClock> m_steadyClock;
78 std::shared_ptr<time::UnitTestSystemClock> m_systemClock;
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050079};
80
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040081} // namespace ndn::tests
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050082
83#endif // NDN_CXX_TESTS_UNIT_CLOCK_FIXTURE_HPP