Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file, originally written as part of NFD (Named Data Networking Forwarding Daemon), |
| 12 | * is a part of ndncert, a certificate management system based on NDN. |
| 13 | * |
| 14 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 16 | * version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 19 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 20 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received copies of the GNU General Public License along with |
| 23 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 26 | */ |
| 27 | |
| 28 | #ifndef NDNCERT_TESTS_TEST_COMMON_HPP |
| 29 | #define NDNCERT_TESTS_TEST_COMMON_HPP |
| 30 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 31 | #include "boost-test.hpp" |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 32 | #include <boost/asio/io_service.hpp> |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 33 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace ndncert { |
| 37 | namespace tests { |
| 38 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 39 | /** \brief a test fixture that overrides steady clock and system clock |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 40 | */ |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 41 | class UnitTestTimeFixture |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 42 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 43 | public: |
| 44 | UnitTestTimeFixture() |
| 45 | : steadyClock(make_shared<time::UnitTestSteadyClock>()) |
| 46 | , systemClock(make_shared<time::UnitTestSystemClock>()) |
| 47 | { |
| 48 | time::setCustomClocks(steadyClock, systemClock); |
| 49 | } |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 50 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 51 | ~UnitTestTimeFixture() |
| 52 | { |
| 53 | time::setCustomClocks(nullptr, nullptr); |
| 54 | } |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 55 | |
| 56 | /** \brief advance steady and system clocks |
| 57 | * |
| 58 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 59 | * 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] | 60 | * |
| 61 | * Exceptions thrown during I/O events are propagated to the caller. |
| 62 | * Clock advancing would stop in case of an exception. |
| 63 | */ |
| 64 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 65 | advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1) |
| 66 | { |
| 67 | this->advanceClocks(tick, tick * nTicks); |
| 68 | } |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 69 | |
| 70 | /** \brief advance steady and system clocks |
| 71 | * |
| 72 | * Clocks are advanced in increments of \p tick for \p total time. |
| 73 | * The last increment might be shorter than \p tick. |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 74 | * 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] | 75 | * |
| 76 | * Exceptions thrown during I/O events are propagated to the caller. |
| 77 | * Clock advancing would stop in case of an exception. |
| 78 | */ |
| 79 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 80 | advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total) |
| 81 | { |
| 82 | BOOST_ASSERT(tick > time::nanoseconds::zero()); |
| 83 | BOOST_ASSERT(total >= time::nanoseconds::zero()); |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 84 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 85 | time::nanoseconds remaining = total; |
| 86 | while (remaining > time::nanoseconds::zero()) { |
| 87 | if (remaining >= tick) { |
| 88 | steadyClock->advance(tick); |
| 89 | systemClock->advance(tick); |
| 90 | remaining -= tick; |
| 91 | } |
| 92 | else { |
| 93 | steadyClock->advance(remaining); |
| 94 | systemClock->advance(remaining); |
| 95 | remaining = time::nanoseconds::zero(); |
| 96 | } |
| 97 | |
| 98 | if (m_io.stopped()) |
| 99 | m_io.reset(); |
| 100 | m_io.poll(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public: |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 105 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 106 | shared_ptr<time::UnitTestSystemClock> systemClock; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame^] | 107 | boost::asio::io_service m_io; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 110 | } // namespace tests |
| 111 | } // namespace ndncert |
| 112 | } // namespace ndn |
| 113 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 114 | #endif // NDNCERT_TESTS_TEST_COMMON_HPP |