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