Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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 | |
| 32 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace ndns { |
| 36 | namespace tests { |
| 37 | |
| 38 | /** \brief base test fixture |
| 39 | * |
| 40 | * Every test case should be based on this fixture, |
| 41 | * to have per test case io_service initialization. |
| 42 | */ |
| 43 | class BaseFixture |
| 44 | { |
| 45 | protected: |
| 46 | BaseFixture(); |
| 47 | |
| 48 | protected: |
| 49 | /** \brief reference to global io_service |
| 50 | */ |
| 51 | boost::asio::io_service m_io; |
| 52 | }; |
| 53 | |
| 54 | /** \brief a base test fixture that overrides steady clock and system clock |
| 55 | */ |
| 56 | class UnitTestTimeFixture : public virtual BaseFixture |
| 57 | { |
| 58 | protected: |
| 59 | UnitTestTimeFixture(); |
| 60 | |
| 61 | ~UnitTestTimeFixture(); |
| 62 | |
| 63 | /** \brief advance steady and system clocks |
| 64 | * |
| 65 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
| 66 | * After each tick, global io_service is polled to process pending I/O events. |
| 67 | * |
| 68 | * Exceptions thrown during I/O events are propagated to the caller. |
| 69 | * Clock advancing would stop in case of an exception. |
| 70 | */ |
| 71 | void |
| 72 | advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1); |
| 73 | |
| 74 | /** \brief advance steady and system clocks |
| 75 | * |
| 76 | * Clocks are advanced in increments of \p tick for \p total time. |
| 77 | * The last increment might be shorter than \p tick. |
| 78 | * After each tick, global io_service is polled to process pending I/O events. |
| 79 | * |
| 80 | * Exceptions thrown during I/O events are propagated to the caller. |
| 81 | * Clock advancing would stop in case of an exception. |
| 82 | */ |
| 83 | void |
| 84 | advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total); |
| 85 | |
| 86 | protected: |
| 87 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 88 | shared_ptr<time::UnitTestSystemClock> systemClock; |
| 89 | |
| 90 | friend class LimitedIo; |
| 91 | }; |
| 92 | |
| 93 | } // namespace tests |
| 94 | } // namespace ndns |
| 95 | } // namespace ndn |
| 96 | |
| 97 | #endif // NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP |