blob: 11d7febe43930bb4c9edede401df657595df724d [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2014-2019, Regents of the University of California,
Zhiyi Zhang8617a792017-01-17 16:45:56 -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, originally written as part of NFD (Named Data Networking Forwarding Daemon),
12 * is a part of ndncert, a certificate management system based on NDN.
13 *
14 * ndncert 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, either
16 * version 3 of the License, or (at your option) any later version.
17 *
18 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
20 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received copies of the GNU General Public License along with
23 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndncert authors and contributors.
26 */
27
28#ifndef NDNCERT_TESTS_TEST_COMMON_HPP
29#define NDNCERT_TESTS_TEST_COMMON_HPP
30
Zhiyi Zhang8617a792017-01-17 16:45:56 -080031#include "boost-test.hpp"
Zhiyi Zhang8617a792017-01-17 16:45:56 -080032#include <boost/asio/io_service.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080033#include <ndn-cxx/util/time-unit-test-clock.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080034
35namespace ndn {
36namespace ndncert {
37namespace tests {
38
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070039/** \brief a test fixture that overrides steady clock and system clock
Zhiyi Zhang8617a792017-01-17 16:45:56 -080040 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070041class UnitTestTimeFixture
Zhiyi Zhang8617a792017-01-17 16:45:56 -080042{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070043public:
44 UnitTestTimeFixture()
45 : steadyClock(make_shared<time::UnitTestSteadyClock>())
46 , systemClock(make_shared<time::UnitTestSystemClock>())
47 {
48 time::setCustomClocks(steadyClock, systemClock);
49 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -080050
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070051 ~UnitTestTimeFixture()
52 {
53 time::setCustomClocks(nullptr, nullptr);
54 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -080055
56 /** \brief advance steady and system clocks
57 *
58 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 * After each tick, io_service is polled to process pending I/O events.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080060 *
61 * Exceptions thrown during I/O events are propagated to the caller.
62 * Clock advancing would stop in case of an exception.
63 */
64 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070065 advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1)
66 {
67 this->advanceClocks(tick, tick * nTicks);
68 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -080069
70 /** \brief advance steady and system clocks
71 *
72 * Clocks are advanced in increments of \p tick for \p total time.
73 * The last increment might be shorter than \p tick.
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074 * After each tick, io_service is polled to process pending I/O events.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080075 *
76 * Exceptions thrown during I/O events are propagated to the caller.
77 * Clock advancing would stop in case of an exception.
78 */
79 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080 advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
81 {
82 BOOST_ASSERT(tick > time::nanoseconds::zero());
83 BOOST_ASSERT(total >= time::nanoseconds::zero());
Zhiyi Zhang8617a792017-01-17 16:45:56 -080084
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070085 time::nanoseconds remaining = total;
86 while (remaining > time::nanoseconds::zero()) {
87 if (remaining >= tick) {
88 steadyClock->advance(tick);
89 systemClock->advance(tick);
90 remaining -= tick;
91 }
92 else {
93 steadyClock->advance(remaining);
94 systemClock->advance(remaining);
95 remaining = time::nanoseconds::zero();
96 }
97
98 if (m_io.stopped())
99 m_io.reset();
100 m_io.poll();
101 }
102 }
103
104public:
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800105 shared_ptr<time::UnitTestSteadyClock> steadyClock;
106 shared_ptr<time::UnitTestSystemClock> systemClock;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700107 boost::asio::io_service m_io;
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800108};
109
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800110} // namespace tests
111} // namespace ndncert
112} // namespace ndn
113
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800114#endif // NDNCERT_TESTS_TEST_COMMON_HPP