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