blob: 61556f7fd64651aa1eeb8db2374dfda8884b3eb0 [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 Pesaventoa9e1ab22023-10-02 22:10:45 -04003 * Copyright (c) 2014-2023, 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 Pesaventoa9e1ab22023-10-02 22:10:45 -040032#include <boost/asio/post.hpp>
33
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050034#include <thread>
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tests {
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080037
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040038BOOST_FIXTURE_TEST_SUITE(TestGlobal, GlobalIoFixture)
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080039
Davide Pesavento3dade002019-03-19 11:29:56 -060040BOOST_AUTO_TEST_CASE(ThreadLocalIoService)
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080041{
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040042 boost::asio::io_context* s1 = &getGlobalIoService();
43 boost::asio::io_context* s2 = nullptr;
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080044
Davide Pesavento3dade002019-03-19 11:29:56 -060045 std::thread t([&s2] { s2 = &getGlobalIoService(); });
46 t.join();
47
48 BOOST_CHECK(s1 != nullptr);
49 BOOST_CHECK(s2 != nullptr);
50 BOOST_CHECK(s1 != s2);
51}
52
53BOOST_AUTO_TEST_CASE(ThreadLocalScheduler)
54{
55 scheduler::Scheduler* s1 = &getScheduler();
56 scheduler::Scheduler* s2 = nullptr;
57
58 std::thread t([&s2] { s2 = &getScheduler(); });
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080059 t.join();
60
61 BOOST_CHECK(s1 != nullptr);
62 BOOST_CHECK(s2 != nullptr);
63 BOOST_CHECK(s1 != s2);
64}
65
Teng Liangf59e58f2018-09-07 16:41:54 -070066BOOST_FIXTURE_TEST_CASE(MainRibIoService, RibIoFixture)
Teng Liang952d6fd2018-05-29 21:09:52 -070067{
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040068 boost::asio::io_context* mainIo = &g_io;
69 boost::asio::io_context* ribIo = g_ribIo;
Teng Liang952d6fd2018-05-29 21:09:52 -070070
71 BOOST_CHECK(mainIo != ribIo);
72 BOOST_CHECK(&getGlobalIoService() == mainIo);
Teng Liangf59e58f2018-09-07 16:41:54 -070073 BOOST_CHECK(&getMainIoService() == mainIo);
Teng Liang952d6fd2018-05-29 21:09:52 -070074 BOOST_CHECK(&getRibIoService() == ribIo);
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050075 auto mainThreadId = std::this_thread::get_id();
Teng Liang952d6fd2018-05-29 21:09:52 -070076
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040077 boost::asio::post(getRibIoService(), [&] {
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050078 BOOST_CHECK(mainThreadId != std::this_thread::get_id());
Teng Liang952d6fd2018-05-29 21:09:52 -070079 BOOST_CHECK(&getGlobalIoService() == ribIo);
Teng Liangf59e58f2018-09-07 16:41:54 -070080 BOOST_CHECK(&getMainIoService() == mainIo);
81 BOOST_CHECK(&getRibIoService() == ribIo);
82 });
83
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040084 boost::asio::post(getRibIoService(), [&] {
85 boost::asio::post(getMainIoService(), [&] {
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050086 BOOST_CHECK(mainThreadId == std::this_thread::get_id());
Teng Liangf59e58f2018-09-07 16:41:54 -070087 BOOST_CHECK(&getGlobalIoService() == mainIo);
88 BOOST_CHECK(&getMainIoService() == mainIo);
89 BOOST_CHECK(&getRibIoService() == ribIo);
90 });
Teng Liang952d6fd2018-05-29 21:09:52 -070091 });
92}
93
94BOOST_FIXTURE_TEST_CASE(PollInAllThreads, RibIoFixture)
95{
96 bool hasRibRun = false;
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040097 boost::asio::post(getRibIoService(), [&] { hasRibRun = true; });
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050098 std::this_thread::sleep_for(std::chrono::seconds(1));
Teng Liang952d6fd2018-05-29 21:09:52 -070099 BOOST_CHECK_EQUAL(hasRibRun, false);
100
101 poll();
102 BOOST_CHECK_EQUAL(hasRibRun, true);
103
104 hasRibRun = false;
105 bool hasMainRun = false;
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400106 boost::asio::post(getMainIoService(), [&] {
Teng Liangf59e58f2018-09-07 16:41:54 -0700107 hasMainRun = true;
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400108 boost::asio::post(getRibIoService(), [&] { hasRibRun = true; });
Teng Liangf59e58f2018-09-07 16:41:54 -0700109 });
Teng Liang952d6fd2018-05-29 21:09:52 -0700110 BOOST_CHECK_EQUAL(hasMainRun, false);
111 BOOST_CHECK_EQUAL(hasRibRun, false);
112
113 poll();
114 BOOST_CHECK_EQUAL(hasMainRun, true);
115 BOOST_CHECK_EQUAL(hasRibRun, true);
116}
117
118BOOST_FIXTURE_TEST_CASE(AdvanceClocks, RibIoTimeFixture)
119{
120 bool hasRibRun = false;
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400121 boost::asio::post(getRibIoService(), [&] { hasRibRun = true; });
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500122 std::this_thread::sleep_for(std::chrono::seconds(1));
Teng Liang952d6fd2018-05-29 21:09:52 -0700123 BOOST_CHECK_EQUAL(hasRibRun, false);
124
125 advanceClocks(1_ns, 1);
126 BOOST_CHECK_EQUAL(hasRibRun, true);
127
128 hasRibRun = false;
129 bool hasMainRun = false;
Davide Pesavento3dade002019-03-19 11:29:56 -0600130 getScheduler().schedule(250_ms, [&] {
Teng Liangf59e58f2018-09-07 16:41:54 -0700131 hasMainRun = true;
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400132 boost::asio::post(getRibIoService(), [&] { hasRibRun = true; });
Teng Liangf59e58f2018-09-07 16:41:54 -0700133 });
Teng Liang952d6fd2018-05-29 21:09:52 -0700134 BOOST_CHECK_EQUAL(hasMainRun, false);
135 BOOST_CHECK_EQUAL(hasRibRun, false);
136
137 advanceClocks(260_ms, 2);
138 BOOST_CHECK_EQUAL(hasMainRun, true);
139 BOOST_CHECK_EQUAL(hasRibRun, true);
140}
141
Davide Pesavento3dade002019-03-19 11:29:56 -0600142BOOST_AUTO_TEST_SUITE_END() // TestGlobal
Alexander Afanasyev5d57f972015-02-11 21:04:29 -0800143
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400144} // namespace nfd::tests