blob: 99d4387970ec21869de173599c5c626794ba1c60 [file] [log] [blame]
Teng Liang952d6fd2018-05-29 21:09:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento2bdf60c2019-02-19 18:23:45 -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 Pesavento3dade002019-03-19 11:29:56 -060026#ifndef NFD_TESTS_DAEMON_RIB_IO_FIXTURE_HPP
27#define NFD_TESTS_DAEMON_RIB_IO_FIXTURE_HPP
Teng Liang952d6fd2018-05-29 21:09:52 -070028
29#include "tests/test-common.hpp"
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050030
Teng Liang952d6fd2018-05-29 21:09:52 -070031#include <condition_variable>
32#include <mutex>
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050033#include <thread>
Teng Liang952d6fd2018-05-29 21:09:52 -070034
35namespace nfd {
36namespace tests {
37
38/** \brief a base test fixture that provides both main and RIB io_service
39 */
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040040class RibIoFixture : public virtual BaseFixture
Teng Liang952d6fd2018-05-29 21:09:52 -070041{
42protected:
43 RibIoFixture();
44
45 ~RibIoFixture();
46
47protected:
48 /** \brief Poll main and RIB thread io_service to process all pending I/O events
49 *
50 * This call will execute all pending I/O events, including events that are posted
51 * inside the processing event, i.e., main and RIB thread io_service will be polled
52 * repeatedly until all pending events are processed.
53 *
54 * \note Must be called from the main thread
55 */
56 void
57 poll();
58
59protected:
Teng Liangf59e58f2018-09-07 16:41:54 -070060 /** \brief pointer to global main io_service
61 */
62 boost::asio::io_service* g_mainIo = nullptr;
63
Teng Liang952d6fd2018-05-29 21:09:52 -070064 /** \brief pointer to global RIB io_service
65 */
66 boost::asio::io_service* g_ribIo = nullptr;
67
68 /** \brief global RIB thread
69 */
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050070 std::thread g_ribThread;
Teng Liang952d6fd2018-05-29 21:09:52 -070071
72private:
73 bool m_shouldStopRibIo = false;
74 bool m_shouldPollRibIo = false;
75 std::mutex m_ribPollMutex;
76 std::condition_variable m_ribPollStartCv;
77 std::condition_variable m_ribPollEndCv;
78};
79
80/** \brief RibIoFixture that also overrides steady clock and system clock
81 */
82class RibIoTimeFixture : public RibIoFixture, public UnitTestTimeFixture
83{
84protected:
85 using UnitTestTimeFixture::advanceClocks;
86
87 /** \brief advance steady and system clocks in the main and RIB threads
88 *
89 * Clocks are advanced in increments of \p tick for \p total time.
90 * The last increment might be shorter than \p tick.
91 * After each tick, the main and RIB thread io_service is polled to process pending I/O events.
92 *
93 * Exceptions thrown during I/O events are propagated to the caller.
94 * Clock advancing would stop in case of an exception.
95 *
96 * \note Must be called from the main thread
97 */
98 void
99 advanceClocks(time::nanoseconds tick, time::nanoseconds total) override;
100};
101
102} // namespace tests
103} // namespace nfd
104
Davide Pesavento3dade002019-03-19 11:29:56 -0600105#endif // NFD_TESTS_DAEMON_RIB_IO_FIXTURE_HPP