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