blob: 6f799509a3fb757162e598a48943b0775466dead [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 Pesaventoc407dee2022-07-21 23:56:05 -040025namespace ndn::tests {
Davide Pesaventof91d1df2020-11-25 14:50:41 -050026
27/** \brief A test fixture that overrides steady clock and system clock.
28 */
29class ClockFixture
30{
31public:
32 virtual
33 ~ClockFixture();
34
35 /** \brief Advance steady and system clocks.
36 *
37 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
38 * afterTick() is called after each tick.
39 *
40 * Exceptions thrown during I/O events are propagated to the caller.
41 * Clock advancement will stop in the event of an exception.
42 */
43 void
44 advanceClocks(time::nanoseconds tick, size_t nTicks = 1)
45 {
46 advanceClocks(tick, tick * nTicks);
47 }
48
49 /** \brief Advance steady and system clocks.
50 *
51 * Clocks are advanced in increments of \p tick for \p total time.
52 * The last increment might be shorter than \p tick.
53 * afterTick() is called after each tick.
54 *
55 * Exceptions thrown during I/O events are propagated to the caller.
56 * Clock advancement will stop in the event of an exception.
57 */
58 void
59 advanceClocks(time::nanoseconds tick, time::nanoseconds total);
60
61protected:
62 ClockFixture();
63
64private:
65 /** \brief Called by advanceClocks() after each clock advancement (tick).
66 *
67 * The base class implementation is a no-op.
68 */
69 virtual void
70 afterTick()
71 {
72 }
73
74protected:
75 shared_ptr<time::UnitTestSteadyClock> m_steadyClock;
76 shared_ptr<time::UnitTestSystemClock> m_systemClock;
77};
78
Davide Pesaventoc407dee2022-07-21 23:56:05 -040079} // namespace ndn::tests
Davide Pesaventof91d1df2020-11-25 14:50:41 -050080
81#endif // PSYNC_TESTS_CLOCK_FIXTURE_HPP