blob: 9e1c9ac3ff6b7c0092806da389ab9738ef5d5067 [file] [log] [blame]
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yumin Xia2c509c22017-02-09 14:37:36 -08003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08004 * 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 NDNS (Named Data Networking Domain Name Service) and is
12 * based on the code written as part of NFD (Named Data Networking Daemon).
13 * See AUTHORS.md for complete list of NDNS authors and contributors.
14 *
15 * NDNS is free software: you can redistribute it and/or modify it under the terms
16 * of the GNU General Public License as published by the Free Software Foundation,
17 * either version 3 of the License, or (at your option) any later version.
18 *
19 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21 * PURPOSE. See the GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along with
24 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27#ifndef NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP
28#define NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP
29
30#include "boost-test.hpp"
31
Yumin Xia2c509c22017-02-09 14:37:36 -080032#include <boost/asio.hpp>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080033#include <ndn-cxx/util/time-unit-test-clock.hpp>
34
35namespace ndn {
36namespace ndns {
37namespace tests {
38
39/** \brief base test fixture
40 *
41 * Every test case should be based on this fixture,
42 * to have per test case io_service initialization.
43 */
44class BaseFixture
45{
46protected:
47 BaseFixture();
48
49protected:
50 /** \brief reference to global io_service
51 */
52 boost::asio::io_service m_io;
53};
54
55/** \brief a base test fixture that overrides steady clock and system clock
56 */
57class UnitTestTimeFixture : public virtual BaseFixture
58{
59protected:
60 UnitTestTimeFixture();
61
62 ~UnitTestTimeFixture();
63
64 /** \brief advance steady and system clocks
65 *
66 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
67 * After each tick, global io_service is polled to process pending I/O events.
68 *
69 * Exceptions thrown during I/O events are propagated to the caller.
70 * Clock advancing would stop in case of an exception.
71 */
72 void
73 advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1);
74
75 /** \brief advance steady and system clocks
76 *
77 * Clocks are advanced in increments of \p tick for \p total time.
78 * The last increment might be shorter than \p tick.
79 * After each tick, global io_service is polled to process pending I/O events.
80 *
81 * Exceptions thrown during I/O events are propagated to the caller.
82 * Clock advancing would stop in case of an exception.
83 */
84 void
85 advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total);
86
87protected:
88 shared_ptr<time::UnitTestSteadyClock> steadyClock;
89 shared_ptr<time::UnitTestSystemClock> systemClock;
90
91 friend class LimitedIo;
92};
93
94} // namespace tests
95} // namespace ndns
96} // namespace ndn
97
98#endif // NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP