blob: bdde1ba5857dcc26f7d8be413acbb16df4bd3240 [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/*
Davide Pesavento2bdf60c2019-02-19 18:23:45 -05003 * Copyright (c) 2014-2019, 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
Davide Pesavento3dade002019-03-19 11:29:56 -060026#include "daemon/global.hpp"
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080027
28#include "tests/test-common.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060029#include "tests/daemon/rib-io-fixture.hpp"
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080030
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050031#include <thread>
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080032
33namespace nfd {
34namespace tests {
35
Davide Pesavento3dade002019-03-19 11:29:56 -060036BOOST_FIXTURE_TEST_SUITE(TestGlobal, BaseFixture)
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080037
Davide Pesavento3dade002019-03-19 11:29:56 -060038BOOST_AUTO_TEST_CASE(ThreadLocalIoService)
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080039{
40 boost::asio::io_service* s1 = &getGlobalIoService();
41 boost::asio::io_service* s2 = nullptr;
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080042
Davide Pesavento3dade002019-03-19 11:29:56 -060043 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
51BOOST_AUTO_TEST_CASE(ThreadLocalScheduler)
52{
53 scheduler::Scheduler* s1 = &getScheduler();
54 scheduler::Scheduler* s2 = nullptr;
55
56 std::thread t([&s2] { s2 = &getScheduler(); });
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080057 t.join();
58
59 BOOST_CHECK(s1 != nullptr);
60 BOOST_CHECK(s2 != nullptr);
61 BOOST_CHECK(s1 != s2);
62}
63
Teng Liangf59e58f2018-09-07 16:41:54 -070064BOOST_FIXTURE_TEST_CASE(MainRibIoService, RibIoFixture)
Teng Liang952d6fd2018-05-29 21:09:52 -070065{
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 Liangf59e58f2018-09-07 16:41:54 -070071 BOOST_CHECK(&getMainIoService() == mainIo);
Teng Liang952d6fd2018-05-29 21:09:52 -070072 BOOST_CHECK(&getRibIoService() == ribIo);
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050073 auto mainThreadId = std::this_thread::get_id();
Teng Liang952d6fd2018-05-29 21:09:52 -070074
75 runOnRibIoService([&] {
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050076 BOOST_CHECK(mainThreadId != std::this_thread::get_id());
Teng Liang952d6fd2018-05-29 21:09:52 -070077 BOOST_CHECK(&getGlobalIoService() == ribIo);
Teng Liangf59e58f2018-09-07 16:41:54 -070078 BOOST_CHECK(&getMainIoService() == mainIo);
79 BOOST_CHECK(&getRibIoService() == ribIo);
80 });
81
82 runOnRibIoService([&] {
83 runOnMainIoService([&] {
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050084 BOOST_CHECK(mainThreadId == std::this_thread::get_id());
Teng Liangf59e58f2018-09-07 16:41:54 -070085 BOOST_CHECK(&getGlobalIoService() == mainIo);
86 BOOST_CHECK(&getMainIoService() == mainIo);
87 BOOST_CHECK(&getRibIoService() == ribIo);
88 });
Teng Liang952d6fd2018-05-29 21:09:52 -070089 });
90}
91
92BOOST_FIXTURE_TEST_CASE(PollInAllThreads, RibIoFixture)
93{
94 bool hasRibRun = false;
95 runOnRibIoService([&] { hasRibRun = true; });
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050096 std::this_thread::sleep_for(std::chrono::seconds(1));
Teng Liang952d6fd2018-05-29 21:09:52 -070097 BOOST_CHECK_EQUAL(hasRibRun, false);
98
99 poll();
100 BOOST_CHECK_EQUAL(hasRibRun, true);
101
102 hasRibRun = false;
103 bool hasMainRun = false;
Teng Liangf59e58f2018-09-07 16:41:54 -0700104 runOnMainIoService([&] {
105 hasMainRun = true;
106 runOnRibIoService([&] { hasRibRun = true; });
107 });
Teng Liang952d6fd2018-05-29 21:09:52 -0700108 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
116BOOST_FIXTURE_TEST_CASE(AdvanceClocks, RibIoTimeFixture)
117{
118 bool hasRibRun = false;
119 runOnRibIoService([&] { hasRibRun = true; });
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500120 std::this_thread::sleep_for(std::chrono::seconds(1));
Teng Liang952d6fd2018-05-29 21:09:52 -0700121 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 Pesavento3dade002019-03-19 11:29:56 -0600128 getScheduler().schedule(250_ms, [&] {
Teng Liangf59e58f2018-09-07 16:41:54 -0700129 hasMainRun = true;
130 runOnRibIoService([&] { hasRibRun = true; });
131 });
Teng Liang952d6fd2018-05-29 21:09:52 -0700132 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 Pesavento3dade002019-03-19 11:29:56 -0600140BOOST_AUTO_TEST_SUITE_END() // TestGlobal
Alexander Afanasyev5d57f972015-02-11 21:04:29 -0800141
142} // namespace tests
143} // namespace nfd