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 | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, 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 | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 37 | /** |
| 38 | * \brief A base test fixture that provides both main and RIB io_context. |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 39 | */ |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 40 | class RibIoFixture : public GlobalIoFixture |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 41 | { |
| 42 | protected: |
| 43 | RibIoFixture(); |
| 44 | |
| 45 | ~RibIoFixture(); |
| 46 | |
| 47 | protected: |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 48 | /** |
| 49 | * \brief Poll main and RIB thread io_context to process all pending I/O events. |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 50 | * |
| 51 | * This call will execute all pending I/O events, including events that are posted |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 52 | * inside the processing event, i.e., main and RIB thread io_context will be polled |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 53 | * repeatedly until all pending events are processed. |
| 54 | * |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 55 | * \warning Must be called from the main thread. |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 56 | */ |
| 57 | void |
| 58 | poll(); |
| 59 | |
| 60 | protected: |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 61 | /** |
| 62 | * \brief Pointer to global main io_context. |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 63 | */ |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 64 | boost::asio::io_context* g_mainIo = nullptr; |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 65 | |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 66 | /** |
| 67 | * \brief Pointer to global RIB io_context. |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 68 | */ |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 69 | boost::asio::io_context* g_ribIo = nullptr; |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 70 | |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 71 | /** |
| 72 | * \brief Global RIB thread. |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 73 | */ |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 74 | std::thread g_ribThread; |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | bool m_shouldStopRibIo = false; |
| 78 | bool m_shouldPollRibIo = false; |
| 79 | std::mutex m_ribPollMutex; |
| 80 | std::condition_variable m_ribPollStartCv; |
| 81 | std::condition_variable m_ribPollEndCv; |
| 82 | }; |
| 83 | |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 84 | /** |
| 85 | * \brief RibIoFixture that also overrides steady clock and system clock. |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 86 | */ |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 87 | class RibIoTimeFixture : public ClockFixture, public RibIoFixture |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 88 | { |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 89 | private: |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 90 | void |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 91 | afterTick() final; |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 94 | } // namespace nfd::tests |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 95 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 96 | #endif // NFD_TESTS_DAEMON_RIB_IO_FIXTURE_HPP |