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 | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 54 | BOOST_FIXTURE_TEST_CASE(RibIoService, RibIoFixture) |
| 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); |
| 61 | BOOST_CHECK(&getRibIoService() == ribIo); |
| 62 | auto mainThreadId = boost::this_thread::get_id(); |
| 63 | |
| 64 | runOnRibIoService([&] { |
| 65 | BOOST_CHECK(mainThreadId != boost::this_thread::get_id()); |
| 66 | BOOST_CHECK(&getRibIoService() == ribIo); |
| 67 | BOOST_CHECK(&getGlobalIoService() == ribIo); |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | BOOST_FIXTURE_TEST_CASE(PollInAllThreads, RibIoFixture) |
| 72 | { |
| 73 | bool hasRibRun = false; |
| 74 | runOnRibIoService([&] { hasRibRun = true; }); |
| 75 | boost::this_thread::sleep_for(1_s); |
| 76 | BOOST_CHECK_EQUAL(hasRibRun, false); |
| 77 | |
| 78 | poll(); |
| 79 | BOOST_CHECK_EQUAL(hasRibRun, true); |
| 80 | |
| 81 | hasRibRun = false; |
| 82 | bool hasMainRun = false; |
| 83 | g_io.post([&] { |
| 84 | hasMainRun = true; |
| 85 | runOnRibIoService([&] { hasRibRun = true; }); |
| 86 | }); |
| 87 | BOOST_CHECK_EQUAL(hasMainRun, false); |
| 88 | BOOST_CHECK_EQUAL(hasRibRun, false); |
| 89 | |
| 90 | poll(); |
| 91 | BOOST_CHECK_EQUAL(hasMainRun, true); |
| 92 | BOOST_CHECK_EQUAL(hasRibRun, true); |
| 93 | } |
| 94 | |
| 95 | BOOST_FIXTURE_TEST_CASE(AdvanceClocks, RibIoTimeFixture) |
| 96 | { |
| 97 | bool hasRibRun = false; |
| 98 | runOnRibIoService([&] { hasRibRun = true; }); |
| 99 | boost::this_thread::sleep_for(1_s); |
| 100 | BOOST_CHECK_EQUAL(hasRibRun, false); |
| 101 | |
| 102 | advanceClocks(1_ns, 1); |
| 103 | BOOST_CHECK_EQUAL(hasRibRun, true); |
| 104 | |
| 105 | hasRibRun = false; |
| 106 | bool hasMainRun = false; |
| 107 | scheduler::schedule(250_ms, [&] { |
| 108 | hasMainRun = true; |
| 109 | runOnRibIoService([&] { hasRibRun = true; }); |
| 110 | }); |
| 111 | BOOST_CHECK_EQUAL(hasMainRun, false); |
| 112 | BOOST_CHECK_EQUAL(hasRibRun, false); |
| 113 | |
| 114 | advanceClocks(260_ms, 2); |
| 115 | BOOST_CHECK_EQUAL(hasMainRun, true); |
| 116 | BOOST_CHECK_EQUAL(hasRibRun, true); |
| 117 | } |
| 118 | |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 119 | BOOST_AUTO_TEST_SUITE_END() |
| 120 | |
| 121 | } // namespace tests |
| 122 | } // namespace nfd |