blob: 0ea363596d41afe20a5737cf2689defe1b4ac82c [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/*
Davide Pesavento0d1d11c2022-04-11 22:11:34 -04003 * Copyright (c) 2013-2022 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
Davide Pesavento0dc02012021-11-23 22:55:03 -050025#include "detail/ndncert-common.hpp"
26
Zhiyi Zhang8617a792017-01-17 16:45:56 -080027#include <ndn-cxx/util/time-unit-test-clock.hpp>
Davide Pesavento0dc02012021-11-23 22:55:03 -050028
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070029#include <boost/asio/io_service.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080030
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040031namespace ndncert::tests {
Zhiyi Zhang8617a792017-01-17 16:45:56 -080032
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070033/** \brief a test fixture that overrides steady clock and system clock
Zhiyi Zhang8617a792017-01-17 16:45:56 -080034 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070035class UnitTestTimeFixture
Zhiyi Zhang8617a792017-01-17 16:45:56 -080036{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070037public:
38 UnitTestTimeFixture()
Davide Pesavento0dc02012021-11-23 22:55:03 -050039 : steadyClock(std::make_shared<time::UnitTestSteadyClock>())
40 , systemClock(std::make_shared<time::UnitTestSystemClock>())
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070041 {
42 time::setCustomClocks(steadyClock, systemClock);
43 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -080044
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070045 ~UnitTestTimeFixture()
46 {
47 time::setCustomClocks(nullptr, nullptr);
48 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -080049
50 /** \brief advance steady and system clocks
51 *
52 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070053 * After each tick, io_service is polled to process pending I/O events.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080054 *
55 * Exceptions thrown during I/O events are propagated to the caller.
56 * Clock advancing would stop in case of an exception.
57 */
58 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1)
60 {
61 this->advanceClocks(tick, tick * nTicks);
62 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -080063
64 /** \brief advance steady and system clocks
65 *
66 * Clocks are advanced in increments of \p tick for \p total time.
67 * The last increment might be shorter than \p tick.
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070068 * After each tick, io_service is polled to process pending I/O events.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080069 *
70 * Exceptions thrown during I/O events are propagated to the caller.
71 * Clock advancing would stop in case of an exception.
72 */
73 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074 advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
75 {
76 BOOST_ASSERT(tick > time::nanoseconds::zero());
77 BOOST_ASSERT(total >= time::nanoseconds::zero());
Zhiyi Zhang8617a792017-01-17 16:45:56 -080078
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079 time::nanoseconds remaining = total;
80 while (remaining > time::nanoseconds::zero()) {
81 if (remaining >= tick) {
82 steadyClock->advance(tick);
83 systemClock->advance(tick);
84 remaining -= tick;
85 }
86 else {
87 steadyClock->advance(remaining);
88 systemClock->advance(remaining);
89 remaining = time::nanoseconds::zero();
90 }
91
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070092 if (io.stopped())
93 io.reset();
94 io.poll();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070095 }
96 }
97
98public:
Davide Pesavento0dc02012021-11-23 22:55:03 -050099 std::shared_ptr<time::UnitTestSteadyClock> steadyClock;
100 std::shared_ptr<time::UnitTestSystemClock> systemClock;
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700101 boost::asio::io_service io;
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800102};
103
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400104} // namespace ndncert::tests
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800105
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700106#endif // NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP