blob: 98563a97d722bcf1e47616600915d71f1f3ba0a7 [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 Pesavento3dade002019-03-19 11:29:56 -060027#include "daemon/global.hpp"
Davide Pesavento97e33022019-02-14 16:00:50 -050028
29#include <boost/exception/diagnostic_information.hpp>
Teng Liang952d6fd2018-05-29 21:09:52 -070030
31namespace nfd {
32namespace tests {
33
34RibIoFixture::RibIoFixture()
35{
36 std::mutex m;
37 std::condition_variable cv;
38
Teng Liangf59e58f2018-09-07 16:41:54 -070039 g_mainIo = &getGlobalIoService();
40 setMainIoService(g_mainIo);
41
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050042 g_ribThread = std::thread([&] {
Teng Liang952d6fd2018-05-29 21:09:52 -070043 {
44 std::lock_guard<std::mutex> lock(m);
45 g_ribIo = &getGlobalIoService();
46 setRibIoService(g_ribIo);
47 BOOST_ASSERT(&g_io != g_ribIo);
48 BOOST_ASSERT(g_ribIo == &getRibIoService());
49 }
50 cv.notify_all();
51
52 try {
53 while (true) {
54 {
55 std::unique_lock<std::mutex> lock(m_ribPollMutex);
56 m_ribPollStartCv.wait(lock, [this] { return m_shouldStopRibIo || m_shouldPollRibIo; });
57 if (m_shouldStopRibIo) {
58 break;
59 }
60 BOOST_ASSERT(m_shouldPollRibIo);
61 }
62
63 if (g_ribIo->stopped()) {
64 g_ribIo->reset();
65 }
66 while (g_ribIo->poll() > 0)
67 ;
68
69 {
70 std::lock_guard<std::mutex> lock(m_ribPollMutex);
71 m_shouldPollRibIo = false;
72 }
73 m_ribPollEndCv.notify_all();
74 }
75 }
Davide Pesavento97e33022019-02-14 16:00:50 -050076 catch (...) {
77 BOOST_WARN_MESSAGE(false, boost::current_exception_diagnostic_information());
78 NDN_THROW_NESTED(std::runtime_error("Fatal exception in RIB thread"));
Teng Liang952d6fd2018-05-29 21:09:52 -070079 }
80 });
81
82 {
83 std::unique_lock<std::mutex> lock(m);
84 cv.wait(lock, [this] { return g_ribIo != nullptr; });
85 }
86}
87
88RibIoFixture::~RibIoFixture()
89{
90 {
91 std::lock_guard<std::mutex> lock(m_ribPollMutex);
92 m_shouldStopRibIo = true;
93 }
94 m_ribPollStartCv.notify_all();
95 g_ribThread.join();
96}
97
98void
99RibIoFixture::poll()
100{
101 BOOST_ASSERT(&getGlobalIoService() == &g_io);
102
103 size_t nHandlersRun = 0;
104 do {
105 {
106 std::lock_guard<std::mutex> lock(m_ribPollMutex);
107 m_shouldPollRibIo = true;
108 }
109 m_ribPollStartCv.notify_all();
110
111 if (g_io.stopped()) {
112 g_io.reset();
113 }
114
115 nHandlersRun = g_io.poll();
116
117 {
118 std::unique_lock<std::mutex> lock(m_ribPollMutex);
119 m_ribPollEndCv.wait(lock, [this] { return !m_shouldPollRibIo; });
120 }
121 } while (nHandlersRun > 0);
122}
123
124void
Davide Pesaventob7703ad2019-03-23 21:12:56 -0400125RibIoTimeFixture::pollAfterClockTick()
Teng Liang952d6fd2018-05-29 21:09:52 -0700126{
Davide Pesaventob7703ad2019-03-23 21:12:56 -0400127 poll();
Teng Liang952d6fd2018-05-29 21:09:52 -0700128}
129
130} // namespace tests
131} // namespace nfd