blob: 768c5cc2544c2bd6ff53bb77ee3338bf6b89d2ae [file] [log] [blame]
Teng Liang952d6fd2018-05-29 21:09:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento97e33022019-02-14 16:00:50 -05003 * Copyright (c) 2014-2019, Regents of the University of California,
Teng Liang952d6fd2018-05-29 21:09:52 -07004 * 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 Pesaventob7703ad2019-03-23 21:12:56 -040026#include "tests/daemon/rib-io-fixture.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040027#include "tests/test-common.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060028#include "daemon/global.hpp"
Davide Pesavento97e33022019-02-14 16:00:50 -050029
30#include <boost/exception/diagnostic_information.hpp>
Teng Liang952d6fd2018-05-29 21:09:52 -070031
32namespace nfd {
33namespace tests {
34
35RibIoFixture::RibIoFixture()
36{
37 std::mutex m;
38 std::condition_variable cv;
39
Teng Liangf59e58f2018-09-07 16:41:54 -070040 g_mainIo = &getGlobalIoService();
41 setMainIoService(g_mainIo);
42
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050043 g_ribThread = std::thread([&] {
Teng Liang952d6fd2018-05-29 21:09:52 -070044 {
45 std::lock_guard<std::mutex> lock(m);
46 g_ribIo = &getGlobalIoService();
47 setRibIoService(g_ribIo);
48 BOOST_ASSERT(&g_io != g_ribIo);
49 BOOST_ASSERT(g_ribIo == &getRibIoService());
50 }
51 cv.notify_all();
52
53 try {
54 while (true) {
55 {
56 std::unique_lock<std::mutex> lock(m_ribPollMutex);
57 m_ribPollStartCv.wait(lock, [this] { return m_shouldStopRibIo || m_shouldPollRibIo; });
58 if (m_shouldStopRibIo) {
59 break;
60 }
61 BOOST_ASSERT(m_shouldPollRibIo);
62 }
63
64 if (g_ribIo->stopped()) {
65 g_ribIo->reset();
66 }
67 while (g_ribIo->poll() > 0)
68 ;
69
70 {
71 std::lock_guard<std::mutex> lock(m_ribPollMutex);
72 m_shouldPollRibIo = false;
73 }
74 m_ribPollEndCv.notify_all();
75 }
76 }
Davide Pesavento97e33022019-02-14 16:00:50 -050077 catch (...) {
78 BOOST_WARN_MESSAGE(false, boost::current_exception_diagnostic_information());
79 NDN_THROW_NESTED(std::runtime_error("Fatal exception in RIB thread"));
Teng Liang952d6fd2018-05-29 21:09:52 -070080 }
81 });
82
83 {
84 std::unique_lock<std::mutex> lock(m);
85 cv.wait(lock, [this] { return g_ribIo != nullptr; });
86 }
87}
88
89RibIoFixture::~RibIoFixture()
90{
91 {
92 std::lock_guard<std::mutex> lock(m_ribPollMutex);
93 m_shouldStopRibIo = true;
94 }
95 m_ribPollStartCv.notify_all();
96 g_ribThread.join();
97}
98
99void
100RibIoFixture::poll()
101{
102 BOOST_ASSERT(&getGlobalIoService() == &g_io);
103
104 size_t nHandlersRun = 0;
105 do {
106 {
107 std::lock_guard<std::mutex> lock(m_ribPollMutex);
108 m_shouldPollRibIo = true;
109 }
110 m_ribPollStartCv.notify_all();
111
112 if (g_io.stopped()) {
113 g_io.reset();
114 }
115
116 nHandlersRun = g_io.poll();
117
118 {
119 std::unique_lock<std::mutex> lock(m_ribPollMutex);
120 m_ribPollEndCv.wait(lock, [this] { return !m_shouldPollRibIo; });
121 }
122 } while (nHandlersRun > 0);
123}
124
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400125RibIoTimeFixture::RibIoTimeFixture()
126 : ClockFixture(g_io)
127{
128}
129
Teng Liang952d6fd2018-05-29 21:09:52 -0700130void
Davide Pesaventob7703ad2019-03-23 21:12:56 -0400131RibIoTimeFixture::pollAfterClockTick()
Teng Liang952d6fd2018-05-29 21:09:52 -0700132{
Davide Pesaventob7703ad2019-03-23 21:12:56 -0400133 poll();
Teng Liang952d6fd2018-05-29 21:09:52 -0700134}
135
136} // namespace tests
137} // namespace nfd