blob: 5aa3bc6bfa34c77e7c9dd1c62db6392763cb1b67 [file] [log] [blame]
Alexander Afanasyev5d57f972015-02-11 21:04:29 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Teng Liang952d6fd2018-05-29 21:09:52 -07002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Alexander Afanasyev5d57f972015-02-11 21:04:29 -08004 * 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 Liang952d6fd2018-05-29 21:09:52 -070027#include "core/scheduler.hpp"
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080028
Teng Liang952d6fd2018-05-29 21:09:52 -070029#include "tests/rib-io-fixture.hpp"
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080030#include "tests/test-common.hpp"
31
32#include <boost/thread.hpp>
33
34namespace nfd {
35namespace tests {
36
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080037BOOST_FIXTURE_TEST_SUITE(TestGlobalIo, BaseFixture)
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080038
39BOOST_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 Liangf59e58f2018-09-07 16:41:54 -070054BOOST_FIXTURE_TEST_CASE(MainRibIoService, RibIoFixture)
Teng Liang952d6fd2018-05-29 21:09:52 -070055{
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 Liangf59e58f2018-09-07 16:41:54 -070061 BOOST_CHECK(&getMainIoService() == mainIo);
Teng Liang952d6fd2018-05-29 21:09:52 -070062 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 Liang952d6fd2018-05-29 21:09:52 -070067 BOOST_CHECK(&getGlobalIoService() == ribIo);
Teng Liangf59e58f2018-09-07 16:41:54 -070068 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 Liang952d6fd2018-05-29 21:09:52 -070079 });
80}
81
82BOOST_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 Liangf59e58f2018-09-07 16:41:54 -070094 runOnMainIoService([&] {
95 hasMainRun = true;
96 runOnRibIoService([&] { hasRibRun = true; });
97 });
Teng Liang952d6fd2018-05-29 21:09:52 -070098 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
106BOOST_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 Liangf59e58f2018-09-07 16:41:54 -0700119 hasMainRun = true;
120 runOnRibIoService([&] { hasRibRun = true; });
121 });
Teng Liang952d6fd2018-05-29 21:09:52 -0700122 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 Afanasyev5d57f972015-02-11 21:04:29 -0800130BOOST_AUTO_TEST_SUITE_END()
131
132} // namespace tests
133} // namespace nfd