Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2021 Regents of the University of California. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 4 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 6 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 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. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 10 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 14 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 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/>. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 18 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 22 | #ifndef NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP |
| 23 | #define NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 25 | #include "detail/ndncert-common.hpp" |
| 26 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 27 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 28 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 29 | #include <boost/asio/io_service.hpp> |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 30 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 31 | namespace ndncert { |
| 32 | namespace tests { |
| 33 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 34 | /** \brief a test fixture that overrides steady clock and system clock |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 35 | */ |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 36 | class UnitTestTimeFixture |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 37 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 38 | public: |
| 39 | UnitTestTimeFixture() |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 40 | : steadyClock(std::make_shared<time::UnitTestSteadyClock>()) |
| 41 | , systemClock(std::make_shared<time::UnitTestSystemClock>()) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 42 | { |
| 43 | time::setCustomClocks(steadyClock, systemClock); |
| 44 | } |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 45 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 46 | ~UnitTestTimeFixture() |
| 47 | { |
| 48 | time::setCustomClocks(nullptr, nullptr); |
| 49 | } |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 50 | |
| 51 | /** \brief advance steady and system clocks |
| 52 | * |
| 53 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 54 | * After each tick, io_service is polled to process pending I/O events. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 55 | * |
| 56 | * Exceptions thrown during I/O events are propagated to the caller. |
| 57 | * Clock advancing would stop in case of an exception. |
| 58 | */ |
| 59 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 60 | advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1) |
| 61 | { |
| 62 | this->advanceClocks(tick, tick * nTicks); |
| 63 | } |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 64 | |
| 65 | /** \brief advance steady and system clocks |
| 66 | * |
| 67 | * Clocks are advanced in increments of \p tick for \p total time. |
| 68 | * The last increment might be shorter than \p tick. |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 69 | * After each tick, io_service is polled to process pending I/O events. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 70 | * |
| 71 | * Exceptions thrown during I/O events are propagated to the caller. |
| 72 | * Clock advancing would stop in case of an exception. |
| 73 | */ |
| 74 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 75 | advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total) |
| 76 | { |
| 77 | BOOST_ASSERT(tick > time::nanoseconds::zero()); |
| 78 | BOOST_ASSERT(total >= time::nanoseconds::zero()); |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 79 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 80 | time::nanoseconds remaining = total; |
| 81 | while (remaining > time::nanoseconds::zero()) { |
| 82 | if (remaining >= tick) { |
| 83 | steadyClock->advance(tick); |
| 84 | systemClock->advance(tick); |
| 85 | remaining -= tick; |
| 86 | } |
| 87 | else { |
| 88 | steadyClock->advance(remaining); |
| 89 | systemClock->advance(remaining); |
| 90 | remaining = time::nanoseconds::zero(); |
| 91 | } |
| 92 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 93 | if (io.stopped()) |
| 94 | io.reset(); |
| 95 | io.poll(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | public: |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 100 | std::shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 101 | std::shared_ptr<time::UnitTestSystemClock> systemClock; |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 102 | boost::asio::io_service io; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 105 | } // namespace tests |
| 106 | } // namespace ndncert |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 107 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 108 | #endif // NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP |