blob: ef228d5d1e557048fd7ac96626b6e9143484851f [file] [log] [blame]
Teng Liang952d6fd2018-05-29 21:09:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
4 * 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"
27#include "core/extended-error-message.hpp"
28#include <iostream>
29
30namespace nfd {
31namespace tests {
32
33RibIoFixture::RibIoFixture()
34{
35 std::mutex m;
36 std::condition_variable cv;
37
38 g_ribThread = boost::thread([&] {
39 {
40 std::lock_guard<std::mutex> lock(m);
41 g_ribIo = &getGlobalIoService();
42 setRibIoService(g_ribIo);
43 BOOST_ASSERT(&g_io != g_ribIo);
44 BOOST_ASSERT(g_ribIo == &getRibIoService());
45 }
46 cv.notify_all();
47
48 try {
49 while (true) {
50 {
51 std::unique_lock<std::mutex> lock(m_ribPollMutex);
52 m_ribPollStartCv.wait(lock, [this] { return m_shouldStopRibIo || m_shouldPollRibIo; });
53 if (m_shouldStopRibIo) {
54 break;
55 }
56 BOOST_ASSERT(m_shouldPollRibIo);
57 }
58
59 if (g_ribIo->stopped()) {
60 g_ribIo->reset();
61 }
62 while (g_ribIo->poll() > 0)
63 ;
64
65 {
66 std::lock_guard<std::mutex> lock(m_ribPollMutex);
67 m_shouldPollRibIo = false;
68 }
69 m_ribPollEndCv.notify_all();
70 }
71 }
72 catch (const std::exception& e) {
73 std::cerr << "Exception in RIB thread: " << getExtendedErrorMessage(e) << std::endl;
74 throw;
75 }
76 });
77
78 {
79 std::unique_lock<std::mutex> lock(m);
80 cv.wait(lock, [this] { return g_ribIo != nullptr; });
81 }
82}
83
84RibIoFixture::~RibIoFixture()
85{
86 {
87 std::lock_guard<std::mutex> lock(m_ribPollMutex);
88 m_shouldStopRibIo = true;
89 }
90 m_ribPollStartCv.notify_all();
91 g_ribThread.join();
92}
93
94void
95RibIoFixture::poll()
96{
97 BOOST_ASSERT(&getGlobalIoService() == &g_io);
98
99 size_t nHandlersRun = 0;
100 do {
101 {
102 std::lock_guard<std::mutex> lock(m_ribPollMutex);
103 m_shouldPollRibIo = true;
104 }
105 m_ribPollStartCv.notify_all();
106
107 if (g_io.stopped()) {
108 g_io.reset();
109 }
110
111 nHandlersRun = g_io.poll();
112
113 {
114 std::unique_lock<std::mutex> lock(m_ribPollMutex);
115 m_ribPollEndCv.wait(lock, [this] { return !m_shouldPollRibIo; });
116 }
117 } while (nHandlersRun > 0);
118}
119
120void
121RibIoTimeFixture::advanceClocks(time::nanoseconds tick, time::nanoseconds total)
122{
123 BOOST_ASSERT(tick > time::nanoseconds::zero());
124 BOOST_ASSERT(total >= time::nanoseconds::zero());
125
126 time::nanoseconds remaining = total;
127 while (remaining > time::nanoseconds::zero()) {
128 if (remaining >= tick) {
129 steadyClock->advance(tick);
130 systemClock->advance(tick);
131 remaining -= tick;
132 }
133 else {
134 steadyClock->advance(remaining);
135 systemClock->advance(remaining);
136 remaining = time::nanoseconds::zero();
137 }
138
139 poll();
140 }
141}
142
143} // namespace tests
144} // namespace nfd