blob: 852304168cf04fab076d852c095289d6a618fe01 [file] [log] [blame]
Davide Pesaventof91d1df2020-11-25 14:50:41 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesaventoc407dee2022-07-21 23:56:05 -04003 * Copyright (c) 2013-2022 Regents of the University of California
Davide Pesaventof91d1df2020-11-25 14:50:41 -05004 * 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
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040025namespace psync::tests {
26
27namespace time = ndn::time;
Davide Pesaventof91d1df2020-11-25 14:50:41 -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 Pesaventoc45a4ea2022-09-19 02:10:53 -040077 std::shared_ptr<time::UnitTestSteadyClock> m_steadyClock;
78 std::shared_ptr<time::UnitTestSystemClock> m_systemClock;
Davide Pesaventof91d1df2020-11-25 14:50:41 -050079};
80
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040081} // namespace psync::tests
Davide Pesaventof91d1df2020-11-25 14:50:41 -050082
83#endif // PSYNC_TESTS_CLOCK_FIXTURE_HPP