blob: fcbaa77f60a3301336abdb98707bc9e1440641e7 [file] [log] [blame]
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -08004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#ifndef NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP
23#define NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/util/time-unit-test-clock.hpp"
Davide Pesavento537dc3a2016-02-18 19:35:26 +010026
27#include <boost/asio/io_service.hpp>
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080028
29namespace ndn {
30namespace tests {
31
Junxiao Shid5827ce2016-07-14 20:49:37 +000032/** \brief a test fixture that overrides steady clock and system clock
33 */
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080034class UnitTestTimeFixture
35{
36public:
37 UnitTestTimeFixture()
38 : steadyClock(make_shared<time::UnitTestSteadyClock>())
39 , systemClock(make_shared<time::UnitTestSystemClock>())
40 {
41 time::setCustomClocks(steadyClock, systemClock);
42 }
43
44 ~UnitTestTimeFixture()
45 {
46 time::setCustomClocks(nullptr, nullptr);
47 }
48
Junxiao Shid5827ce2016-07-14 20:49:37 +000049 /** \brief advance steady and system clocks
50 *
51 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
52 * After each tick, io_service is polled to process pending I/O events.
53 *
54 * Exceptions thrown during I/O events are propagated to the caller.
55 * Clock advancing would stop in case of an exception.
56 */
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080057 void
58 advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1)
59 {
Junxiao Shid5827ce2016-07-14 20:49:37 +000060 this->advanceClocks(tick, tick * nTicks);
61 }
62
63 /** \brief advance steady and system clocks
64 *
65 * Clocks are advanced in increments of \p tick for \p total time.
66 * The last increment might be shorter than \p tick.
67 * After each tick, io_service is polled to process pending I/O events.
68 *
69 * Exceptions thrown during I/O events are propagated to the caller.
70 * Clock advancing would stop in case of an exception.
71 */
72 void
73 advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
74 {
75 BOOST_ASSERT(tick > time::nanoseconds::zero());
76 BOOST_ASSERT(total >= time::nanoseconds::zero());
77
78 time::nanoseconds remaining = total;
79 while (remaining > time::nanoseconds::zero()) {
80 if (remaining >= tick) {
81 steadyClock->advance(tick);
82 systemClock->advance(tick);
83 remaining -= tick;
84 }
85 else {
86 steadyClock->advance(remaining);
87 systemClock->advance(remaining);
88 remaining = time::nanoseconds::zero();
89 }
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080090
91 if (io.stopped())
92 io.reset();
93 io.poll();
94 }
95 }
96
97public:
98 shared_ptr<time::UnitTestSteadyClock> steadyClock;
99 shared_ptr<time::UnitTestSystemClock> systemClock;
100 boost::asio::io_service io;
101};
102
103} // namespace tests
104} // namespace ndn
105
Davide Pesavento7e780642018-11-24 15:51:34 -0500106#endif // NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP