blob: b6b40b13a34267d0241629cdccf1a0982f88ec9c [file] [log] [blame]
Davide Pesaventoba3f6892020-12-08 22:18:35 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventobde084f2022-04-17 00:21:35 -04003 * Copyright (c) 2014-2022, Regents of the University of California
Davide Pesaventoba3f6892020-12-08 22:18:35 -05004 *
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 Pesaventobde084f2022-04-17 00:21:35 -040025namespace ndn::nac::tests {
Davide Pesaventoba3f6892020-12-08 22:18:35 -050026
Davide Pesaventobde084f2022-04-17 00:21:35 -040027/**
28 * \brief A test fixture that overrides steady clock and system clock.
Davide Pesaventoba3f6892020-12-08 22:18:35 -050029 */
30class ClockFixture
31{
32public:
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
62protected:
63 ClockFixture();
64
65private:
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
75protected:
76 shared_ptr<time::UnitTestSteadyClock> m_steadyClock;
77 shared_ptr<time::UnitTestSystemClock> m_systemClock;
78};
79
Davide Pesaventobde084f2022-04-17 00:21:35 -040080} // namespace ndn::nac::tests
Davide Pesaventoba3f6892020-12-08 22:18:35 -050081
82#endif // NAC_TESTS_CLOCK_FIXTURE_HPP