Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [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 | * 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 NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD 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, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_TESTS_RIB_IO_FIXTURE_HPP |
| 27 | #define NFD_TESTS_RIB_IO_FIXTURE_HPP |
| 28 | |
| 29 | #include "tests/test-common.hpp" |
| 30 | #include <boost/thread.hpp> |
| 31 | #include <condition_variable> |
| 32 | #include <mutex> |
| 33 | |
| 34 | namespace nfd { |
| 35 | namespace tests { |
| 36 | |
| 37 | /** \brief a base test fixture that provides both main and RIB io_service |
| 38 | */ |
| 39 | class RibIoFixture: public virtual BaseFixture |
| 40 | { |
| 41 | protected: |
| 42 | RibIoFixture(); |
| 43 | |
| 44 | ~RibIoFixture(); |
| 45 | |
| 46 | protected: |
| 47 | /** \brief Poll main and RIB thread io_service to process all pending I/O events |
| 48 | * |
| 49 | * This call will execute all pending I/O events, including events that are posted |
| 50 | * inside the processing event, i.e., main and RIB thread io_service will be polled |
| 51 | * repeatedly until all pending events are processed. |
| 52 | * |
| 53 | * \note Must be called from the main thread |
| 54 | */ |
| 55 | void |
| 56 | poll(); |
| 57 | |
| 58 | protected: |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 59 | /** \brief pointer to global main io_service |
| 60 | */ |
| 61 | boost::asio::io_service* g_mainIo = nullptr; |
| 62 | |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 63 | /** \brief pointer to global RIB io_service |
| 64 | */ |
| 65 | boost::asio::io_service* g_ribIo = nullptr; |
| 66 | |
| 67 | /** \brief global RIB thread |
| 68 | */ |
| 69 | boost::thread g_ribThread; |
| 70 | |
| 71 | private: |
| 72 | bool m_shouldStopRibIo = false; |
| 73 | bool m_shouldPollRibIo = false; |
| 74 | std::mutex m_ribPollMutex; |
| 75 | std::condition_variable m_ribPollStartCv; |
| 76 | std::condition_variable m_ribPollEndCv; |
| 77 | }; |
| 78 | |
| 79 | /** \brief RibIoFixture that also overrides steady clock and system clock |
| 80 | */ |
| 81 | class RibIoTimeFixture : public RibIoFixture, public UnitTestTimeFixture |
| 82 | { |
| 83 | protected: |
| 84 | using UnitTestTimeFixture::advanceClocks; |
| 85 | |
| 86 | /** \brief advance steady and system clocks in the main and RIB threads |
| 87 | * |
| 88 | * Clocks are advanced in increments of \p tick for \p total time. |
| 89 | * The last increment might be shorter than \p tick. |
| 90 | * After each tick, the main and RIB thread io_service is polled to process pending I/O events. |
| 91 | * |
| 92 | * Exceptions thrown during I/O events are propagated to the caller. |
| 93 | * Clock advancing would stop in case of an exception. |
| 94 | * |
| 95 | * \note Must be called from the main thread |
| 96 | */ |
| 97 | void |
| 98 | advanceClocks(time::nanoseconds tick, time::nanoseconds total) override; |
| 99 | }; |
| 100 | |
| 101 | } // namespace tests |
| 102 | } // namespace nfd |
| 103 | |
| 104 | #endif // NFD_TESTS_RIB_IO_FIXTURE_HPP |