blob: e89ba440fd738f3b5159f48032634a156af8f693 [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#ifndef NFD_TESTS_RIB_IO_FIXTURE_HPP
27#define NFD_TESTS_RIB_IO_FIXTURE_HPP
28
29#include "tests/test-common.hpp"
30#include <boost/thread.hpp>
31#include <condition_variable>
32#include <mutex>
33
34namespace nfd {
35namespace tests {
36
37/** \brief a base test fixture that provides both main and RIB io_service
38 */
39class RibIoFixture: public virtual BaseFixture
40{
41protected:
42 RibIoFixture();
43
44 ~RibIoFixture();
45
46protected:
47 /** \brief Poll main and RIB thread io_service to process all pending I/O events
48 *
49 * This call will execute all pending I/O events, including events that are posted
50 * inside the processing event, i.e., main and RIB thread io_service will be polled
51 * repeatedly until all pending events are processed.
52 *
53 * \note Must be called from the main thread
54 */
55 void
56 poll();
57
58protected:
59 /** \brief pointer to global RIB io_service
60 */
61 boost::asio::io_service* g_ribIo = nullptr;
62
63 /** \brief global RIB thread
64 */
65 boost::thread g_ribThread;
66
67private:
68 bool m_shouldStopRibIo = false;
69 bool m_shouldPollRibIo = false;
70 std::mutex m_ribPollMutex;
71 std::condition_variable m_ribPollStartCv;
72 std::condition_variable m_ribPollEndCv;
73};
74
75/** \brief RibIoFixture that also overrides steady clock and system clock
76 */
77class RibIoTimeFixture : public RibIoFixture, public UnitTestTimeFixture
78{
79protected:
80 using UnitTestTimeFixture::advanceClocks;
81
82 /** \brief advance steady and system clocks in the main and RIB threads
83 *
84 * Clocks are advanced in increments of \p tick for \p total time.
85 * The last increment might be shorter than \p tick.
86 * After each tick, the main and RIB thread io_service is polled to process pending I/O events.
87 *
88 * Exceptions thrown during I/O events are propagated to the caller.
89 * Clock advancing would stop in case of an exception.
90 *
91 * \note Must be called from the main thread
92 */
93 void
94 advanceClocks(time::nanoseconds tick, time::nanoseconds total) override;
95};
96
97} // namespace tests
98} // namespace nfd
99
100#endif // NFD_TESTS_RIB_IO_FIXTURE_HPP