blob: dced38254d6bebbb545235dbfef0f59768adfa3a [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 Pesavento2cae8ca2019-04-18 20:48:05 -040026#include "common/global.hpp"
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080027
28#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060030#include "tests/daemon/rib-io-fixture.hpp"
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080031
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050032#include <thread>
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080033
34namespace nfd {
35namespace tests {
36
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040037BOOST_FIXTURE_TEST_SUITE(TestGlobal, GlobalIoFixture)
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080038
Davide Pesavento3dade002019-03-19 11:29:56 -060039BOOST_AUTO_TEST_CASE(ThreadLocalIoService)
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080040{
41 boost::asio::io_service* s1 = &getGlobalIoService();
42 boost::asio::io_service* s2 = nullptr;
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080043
Davide Pesavento3dade002019-03-19 11:29:56 -060044 std::thread t([&s2] { s2 = &getGlobalIoService(); });
45 t.join();
46
47 BOOST_CHECK(s1 != nullptr);
48 BOOST_CHECK(s2 != nullptr);
49 BOOST_CHECK(s1 != s2);
50}
51
52BOOST_AUTO_TEST_CASE(ThreadLocalScheduler)
53{
54 scheduler::Scheduler* s1 = &getScheduler();
55 scheduler::Scheduler* s2 = nullptr;
56
57 std::thread t([&s2] { s2 = &getScheduler(); });
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080058 t.join();
59
60 BOOST_CHECK(s1 != nullptr);
61 BOOST_CHECK(s2 != nullptr);
62 BOOST_CHECK(s1 != s2);
63}
64
Teng Liangf59e58f2018-09-07 16:41:54 -070065BOOST_FIXTURE_TEST_CASE(MainRibIoService, RibIoFixture)
Teng Liang952d6fd2018-05-29 21:09:52 -070066{
67 boost::asio::io_service* mainIo = &g_io;
68 boost::asio::io_service* ribIo = g_ribIo;
69
70 BOOST_CHECK(mainIo != ribIo);
71 BOOST_CHECK(&getGlobalIoService() == mainIo);
Teng Liangf59e58f2018-09-07 16:41:54 -070072 BOOST_CHECK(&getMainIoService() == mainIo);
Teng Liang952d6fd2018-05-29 21:09:52 -070073 BOOST_CHECK(&getRibIoService() == ribIo);
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050074 auto mainThreadId = std::this_thread::get_id();
Teng Liang952d6fd2018-05-29 21:09:52 -070075
76 runOnRibIoService([&] {
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050077 BOOST_CHECK(mainThreadId != std::this_thread::get_id());
Teng Liang952d6fd2018-05-29 21:09:52 -070078 BOOST_CHECK(&getGlobalIoService() == ribIo);
Teng Liangf59e58f2018-09-07 16:41:54 -070079 BOOST_CHECK(&getMainIoService() == mainIo);
80 BOOST_CHECK(&getRibIoService() == ribIo);
81 });
82
83 runOnRibIoService([&] {
84 runOnMainIoService([&] {
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050085 BOOST_CHECK(mainThreadId == std::this_thread::get_id());
Teng Liangf59e58f2018-09-07 16:41:54 -070086 BOOST_CHECK(&getGlobalIoService() == mainIo);
87 BOOST_CHECK(&getMainIoService() == mainIo);
88 BOOST_CHECK(&getRibIoService() == ribIo);
89 });
Teng Liang952d6fd2018-05-29 21:09:52 -070090 });
91}
92
93BOOST_FIXTURE_TEST_CASE(PollInAllThreads, RibIoFixture)
94{
95 bool hasRibRun = false;
96 runOnRibIoService([&] { hasRibRun = true; });
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050097 std::this_thread::sleep_for(std::chrono::seconds(1));
Teng Liang952d6fd2018-05-29 21:09:52 -070098 BOOST_CHECK_EQUAL(hasRibRun, false);
99
100 poll();
101 BOOST_CHECK_EQUAL(hasRibRun, true);
102
103 hasRibRun = false;
104 bool hasMainRun = false;
Teng Liangf59e58f2018-09-07 16:41:54 -0700105 runOnMainIoService([&] {
106 hasMainRun = true;
107 runOnRibIoService([&] { hasRibRun = true; });
108 });
Teng Liang952d6fd2018-05-29 21:09:52 -0700109 BOOST_CHECK_EQUAL(hasMainRun, false);
110 BOOST_CHECK_EQUAL(hasRibRun, false);
111
112 poll();
113 BOOST_CHECK_EQUAL(hasMainRun, true);
114 BOOST_CHECK_EQUAL(hasRibRun, true);
115}
116
117BOOST_FIXTURE_TEST_CASE(AdvanceClocks, RibIoTimeFixture)
118{
119 bool hasRibRun = false;
120 runOnRibIoService([&] { hasRibRun = true; });
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500121 std::this_thread::sleep_for(std::chrono::seconds(1));
Teng Liang952d6fd2018-05-29 21:09:52 -0700122 BOOST_CHECK_EQUAL(hasRibRun, false);
123
124 advanceClocks(1_ns, 1);
125 BOOST_CHECK_EQUAL(hasRibRun, true);
126
127 hasRibRun = false;
128 bool hasMainRun = false;
Davide Pesavento3dade002019-03-19 11:29:56 -0600129 getScheduler().schedule(250_ms, [&] {
Teng Liangf59e58f2018-09-07 16:41:54 -0700130 hasMainRun = true;
131 runOnRibIoService([&] { hasRibRun = true; });
132 });
Teng Liang952d6fd2018-05-29 21:09:52 -0700133 BOOST_CHECK_EQUAL(hasMainRun, false);
134 BOOST_CHECK_EQUAL(hasRibRun, false);
135
136 advanceClocks(260_ms, 2);
137 BOOST_CHECK_EQUAL(hasMainRun, true);
138 BOOST_CHECK_EQUAL(hasRibRun, true);
139}
140
Davide Pesavento3dade002019-03-19 11:29:56 -0600141BOOST_AUTO_TEST_SUITE_END() // TestGlobal
Alexander Afanasyev5d57f972015-02-11 21:04:29 -0800142
143} // namespace tests
144} // namespace nfd