blob: c20b8d98369d3c16399e296253f6dfc64a580871 [file] [log] [blame]
Davide Pesaventoba3f6892020-12-08 22:18:35 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2020, Regents of the University of California
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
25namespace ndn {
26namespace nac {
27namespace tests {
28
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:
77 shared_ptr<time::UnitTestSteadyClock> m_steadyClock;
78 shared_ptr<time::UnitTestSystemClock> m_systemClock;
79};
80
81} // namespace tests
82} // namespace nac
83} // namespace ndn
84
85#endif // NAC_TESTS_CLOCK_FIXTURE_HPP