blob: f934a12add6377b00ca86db02907fa18386c00a2 [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07002/*
3 * Copyright (c) 2013-2019 Regents of the University of California.
Zhiyi Zhang8617a792017-01-17 16:45:56 -08004 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Zhiyi Zhang8617a792017-01-17 16:45:56 -08006 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07007 * 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.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080010 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070011 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
Zhiyi Zhang8617a792017-01-17 16:45:56 -080012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070013 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080014 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070015 * 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/>.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080018 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070019 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080020 */
21
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070022#ifndef NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP
23#define NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP
Zhiyi Zhang8617a792017-01-17 16:45:56 -080024
Zhiyi Zhang8617a792017-01-17 16:45:56 -080025#include <ndn-cxx/util/time-unit-test-clock.hpp>
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070026#include <boost/asio/io_service.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080027
28namespace ndn {
29namespace ndncert {
30namespace tests {
31
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070032/** \brief a test fixture that overrides steady clock and system clock
Zhiyi Zhang8617a792017-01-17 16:45:56 -080033 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070034class UnitTestTimeFixture
Zhiyi Zhang8617a792017-01-17 16:45:56 -080035{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070036public:
37 UnitTestTimeFixture()
38 : steadyClock(make_shared<time::UnitTestSteadyClock>())
39 , systemClock(make_shared<time::UnitTestSystemClock>())
40 {
41 time::setCustomClocks(steadyClock, systemClock);
42 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -080043
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044 ~UnitTestTimeFixture()
45 {
46 time::setCustomClocks(nullptr, nullptr);
47 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -080048
49 /** \brief advance steady and system clocks
50 *
51 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070052 * After each tick, io_service is polled to process pending I/O events.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080053 *
54 * Exceptions thrown during I/O events are propagated to the caller.
55 * Clock advancing would stop in case of an exception.
56 */
57 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1)
59 {
60 this->advanceClocks(tick, tick * nTicks);
61 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -080062
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.
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067 * After each tick, io_service is polled to process pending I/O events.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080068 *
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
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073 advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
74 {
75 BOOST_ASSERT(tick > time::nanoseconds::zero());
76 BOOST_ASSERT(total >= time::nanoseconds::zero());
Zhiyi Zhang8617a792017-01-17 16:45:56 -080077
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078 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 }
90
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070091 if (io.stopped())
92 io.reset();
93 io.poll();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070094 }
95 }
96
97public:
Zhiyi Zhang8617a792017-01-17 16:45:56 -080098 shared_ptr<time::UnitTestSteadyClock> steadyClock;
99 shared_ptr<time::UnitTestSystemClock> systemClock;
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700100 boost::asio::io_service io;
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800101};
102
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800103} // namespace tests
104} // namespace ndncert
105} // namespace ndn
106
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700107#endif // NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP