Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [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 | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 26 | #include "common/global.hpp" |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "tests/daemon/global-io-fixture.hpp" |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 30 | #include "tests/daemon/rib-io-fixture.hpp" |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 32 | #include <boost/asio/post.hpp> |
| 33 | |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 34 | #include <thread> |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::tests { |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 37 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestGlobal, GlobalIoFixture) |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 39 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(ThreadLocalIoService) |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 41 | { |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 42 | boost::asio::io_context* s1 = &getGlobalIoService(); |
| 43 | boost::asio::io_context* s2 = nullptr; |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 44 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 45 | std::thread t([&s2] { s2 = &getGlobalIoService(); }); |
| 46 | t.join(); |
| 47 | |
| 48 | BOOST_CHECK(s1 != nullptr); |
| 49 | BOOST_CHECK(s2 != nullptr); |
| 50 | BOOST_CHECK(s1 != s2); |
| 51 | } |
| 52 | |
| 53 | BOOST_AUTO_TEST_CASE(ThreadLocalScheduler) |
| 54 | { |
| 55 | scheduler::Scheduler* s1 = &getScheduler(); |
| 56 | scheduler::Scheduler* s2 = nullptr; |
| 57 | |
| 58 | std::thread t([&s2] { s2 = &getScheduler(); }); |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 59 | t.join(); |
| 60 | |
| 61 | BOOST_CHECK(s1 != nullptr); |
| 62 | BOOST_CHECK(s2 != nullptr); |
| 63 | BOOST_CHECK(s1 != s2); |
| 64 | } |
| 65 | |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 66 | BOOST_FIXTURE_TEST_CASE(MainRibIoService, RibIoFixture) |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 67 | { |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 68 | boost::asio::io_context* mainIo = &g_io; |
| 69 | boost::asio::io_context* ribIo = g_ribIo; |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 70 | |
| 71 | BOOST_CHECK(mainIo != ribIo); |
| 72 | BOOST_CHECK(&getGlobalIoService() == mainIo); |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 73 | BOOST_CHECK(&getMainIoService() == mainIo); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 74 | BOOST_CHECK(&getRibIoService() == ribIo); |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 75 | auto mainThreadId = std::this_thread::get_id(); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 76 | |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 77 | boost::asio::post(getRibIoService(), [&] { |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 78 | BOOST_CHECK(mainThreadId != std::this_thread::get_id()); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 79 | BOOST_CHECK(&getGlobalIoService() == ribIo); |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 80 | BOOST_CHECK(&getMainIoService() == mainIo); |
| 81 | BOOST_CHECK(&getRibIoService() == ribIo); |
| 82 | }); |
| 83 | |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 84 | boost::asio::post(getRibIoService(), [&] { |
| 85 | boost::asio::post(getMainIoService(), [&] { |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 86 | BOOST_CHECK(mainThreadId == std::this_thread::get_id()); |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 87 | BOOST_CHECK(&getGlobalIoService() == mainIo); |
| 88 | BOOST_CHECK(&getMainIoService() == mainIo); |
| 89 | BOOST_CHECK(&getRibIoService() == ribIo); |
| 90 | }); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 91 | }); |
| 92 | } |
| 93 | |
| 94 | BOOST_FIXTURE_TEST_CASE(PollInAllThreads, RibIoFixture) |
| 95 | { |
| 96 | bool hasRibRun = false; |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 97 | boost::asio::post(getRibIoService(), [&] { hasRibRun = true; }); |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 98 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(hasRibRun, false); |
| 100 | |
| 101 | poll(); |
| 102 | BOOST_CHECK_EQUAL(hasRibRun, true); |
| 103 | |
| 104 | hasRibRun = false; |
| 105 | bool hasMainRun = false; |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 106 | boost::asio::post(getMainIoService(), [&] { |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 107 | hasMainRun = true; |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 108 | boost::asio::post(getRibIoService(), [&] { hasRibRun = true; }); |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 109 | }); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(hasMainRun, false); |
| 111 | BOOST_CHECK_EQUAL(hasRibRun, false); |
| 112 | |
| 113 | poll(); |
| 114 | BOOST_CHECK_EQUAL(hasMainRun, true); |
| 115 | BOOST_CHECK_EQUAL(hasRibRun, true); |
| 116 | } |
| 117 | |
| 118 | BOOST_FIXTURE_TEST_CASE(AdvanceClocks, RibIoTimeFixture) |
| 119 | { |
| 120 | bool hasRibRun = false; |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 121 | boost::asio::post(getRibIoService(), [&] { hasRibRun = true; }); |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 122 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(hasRibRun, false); |
| 124 | |
| 125 | advanceClocks(1_ns, 1); |
| 126 | BOOST_CHECK_EQUAL(hasRibRun, true); |
| 127 | |
| 128 | hasRibRun = false; |
| 129 | bool hasMainRun = false; |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 130 | getScheduler().schedule(250_ms, [&] { |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 131 | hasMainRun = true; |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame^] | 132 | boost::asio::post(getRibIoService(), [&] { hasRibRun = true; }); |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 133 | }); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(hasMainRun, false); |
| 135 | BOOST_CHECK_EQUAL(hasRibRun, false); |
| 136 | |
| 137 | advanceClocks(260_ms, 2); |
| 138 | BOOST_CHECK_EQUAL(hasMainRun, true); |
| 139 | BOOST_CHECK_EQUAL(hasRibRun, true); |
| 140 | } |
| 141 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 142 | BOOST_AUTO_TEST_SUITE_END() // TestGlobal |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 143 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 144 | } // namespace nfd::tests |