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