blob: c340526de4941e0f144c58b8bba157269eb3bc58 [file] [log] [blame]
Davide Pesaventof91d1df2020-11-25 14:50:41 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013-2020 Regents of the University of California
4 * The University of Memphis
5 *
6 * This file is part of PSync.
7 *
8 * PSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU Lesser General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License along with
17 * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef PSYNC_TESTS_CLOCK_FIXTURE_HPP
21#define PSYNC_TESTS_CLOCK_FIXTURE_HPP
22
23#include <ndn-cxx/util/time-unit-test-clock.hpp>
24
25namespace ndn {
26namespace tests {
27
28/** \brief A test fixture that overrides steady clock and system clock.
29 */
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
80} // namespace tests
81} // namespace ndn
82
83#endif // PSYNC_TESTS_CLOCK_FIXTURE_HPP