Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -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 is part of NDNS (Named Data Networking Domain Name Service) and is |
| 12 | * based on the code written as part of NFD (Named Data Networking Daemon). |
| 13 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 14 | * |
| 15 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 16 | * of the GNU General Public License as published by the Free Software Foundation, |
| 17 | * either version 3 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 20 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 21 | * PURPOSE. See the GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along with |
| 24 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 25 | */ |
| 26 | |
| 27 | #ifndef NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP |
| 28 | #define NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP |
| 29 | |
| 30 | #include "boost-test.hpp" |
| 31 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 32 | #include <boost/asio.hpp> |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 33 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
| 34 | |
| 35 | namespace ndn { |
| 36 | namespace ndns { |
| 37 | namespace tests { |
| 38 | |
| 39 | /** \brief base test fixture |
| 40 | * |
| 41 | * Every test case should be based on this fixture, |
| 42 | * to have per test case io_service initialization. |
| 43 | */ |
| 44 | class BaseFixture |
| 45 | { |
| 46 | protected: |
| 47 | BaseFixture(); |
| 48 | |
| 49 | protected: |
| 50 | /** \brief reference to global io_service |
| 51 | */ |
| 52 | boost::asio::io_service m_io; |
| 53 | }; |
| 54 | |
| 55 | /** \brief a base test fixture that overrides steady clock and system clock |
| 56 | */ |
| 57 | class UnitTestTimeFixture : public virtual BaseFixture |
| 58 | { |
| 59 | protected: |
| 60 | UnitTestTimeFixture(); |
| 61 | |
| 62 | ~UnitTestTimeFixture(); |
| 63 | |
| 64 | /** \brief advance steady and system clocks |
| 65 | * |
| 66 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
| 67 | * After each tick, global 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, size_t nTicks = 1); |
| 74 | |
| 75 | /** \brief advance steady and system clocks |
| 76 | * |
| 77 | * Clocks are advanced in increments of \p tick for \p total time. |
| 78 | * The last increment might be shorter than \p tick. |
| 79 | * After each tick, global io_service is polled to process pending I/O events. |
| 80 | * |
| 81 | * Exceptions thrown during I/O events are propagated to the caller. |
| 82 | * Clock advancing would stop in case of an exception. |
| 83 | */ |
| 84 | void |
| 85 | advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total); |
| 86 | |
| 87 | protected: |
| 88 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 89 | shared_ptr<time::UnitTestSystemClock> systemClock; |
| 90 | |
| 91 | friend class LimitedIo; |
| 92 | }; |
| 93 | |
| 94 | } // namespace tests |
| 95 | } // namespace ndns |
| 96 | } // namespace ndn |
| 97 | |
| 98 | #endif // NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP |