blob: 870299406ff1e43723a6b33fcf4b66435520323f [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
26#include "rib-io-fixture.hpp"
Davide Pesavento97e33022019-02-14 16:00:50 -050027
28#include <boost/exception/diagnostic_information.hpp>
Teng Liang952d6fd2018-05-29 21:09:52 -070029
30namespace nfd {
31namespace tests {
32
33RibIoFixture::RibIoFixture()
34{
35 std::mutex m;
36 std::condition_variable cv;
37
Teng Liangf59e58f2018-09-07 16:41:54 -070038 g_mainIo = &getGlobalIoService();
39 setMainIoService(g_mainIo);
40
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050041 g_ribThread = std::thread([&] {
Teng Liang952d6fd2018-05-29 21:09:52 -070042 {
43 std::lock_guard<std::mutex> lock(m);
44 g_ribIo = &getGlobalIoService();
45 setRibIoService(g_ribIo);
46 BOOST_ASSERT(&g_io != g_ribIo);
47 BOOST_ASSERT(g_ribIo == &getRibIoService());
48 }
49 cv.notify_all();
50
51 try {
52 while (true) {
53 {
54 std::unique_lock<std::mutex> lock(m_ribPollMutex);
55 m_ribPollStartCv.wait(lock, [this] { return m_shouldStopRibIo || m_shouldPollRibIo; });
56 if (m_shouldStopRibIo) {
57 break;
58 }
59 BOOST_ASSERT(m_shouldPollRibIo);
60 }
61
62 if (g_ribIo->stopped()) {
63 g_ribIo->reset();
64 }
65 while (g_ribIo->poll() > 0)
66 ;
67
68 {
69 std::lock_guard<std::mutex> lock(m_ribPollMutex);
70 m_shouldPollRibIo = false;
71 }
72 m_ribPollEndCv.notify_all();
73 }
74 }
Davide Pesavento97e33022019-02-14 16:00:50 -050075 catch (...) {
76 BOOST_WARN_MESSAGE(false, boost::current_exception_diagnostic_information());
77 NDN_THROW_NESTED(std::runtime_error("Fatal exception in RIB thread"));
Teng Liang952d6fd2018-05-29 21:09:52 -070078 }
79 });
80
81 {
82 std::unique_lock<std::mutex> lock(m);
83 cv.wait(lock, [this] { return g_ribIo != nullptr; });
84 }
85}
86
87RibIoFixture::~RibIoFixture()
88{
89 {
90 std::lock_guard<std::mutex> lock(m_ribPollMutex);
91 m_shouldStopRibIo = true;
92 }
93 m_ribPollStartCv.notify_all();
94 g_ribThread.join();
95}
96
97void
98RibIoFixture::poll()
99{
100 BOOST_ASSERT(&getGlobalIoService() == &g_io);
101
102 size_t nHandlersRun = 0;
103 do {
104 {
105 std::lock_guard<std::mutex> lock(m_ribPollMutex);
106 m_shouldPollRibIo = true;
107 }
108 m_ribPollStartCv.notify_all();
109
110 if (g_io.stopped()) {
111 g_io.reset();
112 }
113
114 nHandlersRun = g_io.poll();
115
116 {
117 std::unique_lock<std::mutex> lock(m_ribPollMutex);
118 m_ribPollEndCv.wait(lock, [this] { return !m_shouldPollRibIo; });
119 }
120 } while (nHandlersRun > 0);
121}
122
123void
124RibIoTimeFixture::advanceClocks(time::nanoseconds tick, time::nanoseconds total)
125{
126 BOOST_ASSERT(tick > time::nanoseconds::zero());
127 BOOST_ASSERT(total >= time::nanoseconds::zero());
128
129 time::nanoseconds remaining = total;
130 while (remaining > time::nanoseconds::zero()) {
131 if (remaining >= tick) {
132 steadyClock->advance(tick);
133 systemClock->advance(tick);
134 remaining -= tick;
135 }
136 else {
137 steadyClock->advance(remaining);
138 systemClock->advance(remaining);
139 remaining = time::nanoseconds::zero();
140 }
141
142 poll();
143 }
144}
145
146} // namespace tests
147} // namespace nfd