Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [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 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 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 26 | #ifndef NFD_TESTS_DAEMON_RIB_IO_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_RIB_IO_FIXTURE_HPP |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "tests/daemon/global-io-fixture.hpp" |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 30 | |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 31 | #include <condition_variable> |
| 32 | #include <mutex> |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 33 | #include <thread> |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 35 | namespace nfd::tests { |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 37 | /** \brief A base test fixture that provides both main and RIB io_service. |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 38 | */ |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 39 | class RibIoFixture : public GlobalIoFixture |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 40 | { |
| 41 | protected: |
| 42 | RibIoFixture(); |
| 43 | |
| 44 | ~RibIoFixture(); |
| 45 | |
| 46 | protected: |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 47 | /** \brief Poll main and RIB thread io_service to process all pending I/O events. |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 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 | * |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 53 | * \warning Must be called from the main thread |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 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 | */ |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 69 | std::thread g_ribThread; |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 79 | /** \brief RibIoFixture that also overrides steady clock and system clock. |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 80 | */ |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 81 | class RibIoTimeFixture : public ClockFixture, public RibIoFixture |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 82 | { |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 83 | private: |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 84 | void |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 85 | afterTick() final; |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 88 | } // namespace nfd::tests |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 89 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 90 | #endif // NFD_TESTS_DAEMON_RIB_IO_FIXTURE_HPP |