ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
time-unit-test-clock.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2023 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
23#include "ndn-cxx/util/impl/steady-timer.hpp"
24
25#include <chrono>
26#include <thread>
27
28namespace ndn::time {
29
30template<class BaseClock, class ClockTraits>
32 : m_currentTime(startTime)
33{
34}
35
36template<class BaseClock, class ClockTraits>
37std::string
39{
40 return " since unit test beginning";
41}
42
43template<class BaseClock, class ClockTraits>
44typename BaseClock::time_point
46{
47 return typename BaseClock::time_point(duration_cast<typename BaseClock::duration>(m_currentTime));
48}
49
50template<class BaseClock, class ClockTraits>
51typename BaseClock::duration
53{
54 return typename BaseClock::duration(1);
55}
56
57template<class BaseClock, class ClockTraits>
58void
60{
61 m_currentTime += duration;
62
63 // When UnitTestClock is used with Asio timers (e.g. basic_waitable_timer), and
64 // an async wait operation on a timer is already in progress, Asio won't look
65 // at the clock again until the earliest timer has expired, so it won't know that
66 // the current time has changed.
67 //
68 // Therefore, in order for the clock advancement to be effective, we must sleep
69 // for a period greater than wait_traits::to_wait_duration().
70 //
71 // See also http://blog.think-async.com/2007/08/time-travel.html - "Jumping Through Time"
72 //
73 std::this_thread::sleep_for(std::chrono::nanoseconds(duration_cast<nanoseconds>(
74 boost::asio::wait_traits<steady_clock>::to_wait_duration(duration) +
75 typename BaseClock::duration(1)).count()));
76}
77
78template<class BaseClock, class ClockTraits>
79void
81{
82 BOOST_ASSERT(!BaseClock::is_steady || timeSinceEpoch >= m_currentTime);
83
84 m_currentTime = timeSinceEpoch;
85
86 // See comment in advance()
87 auto delta = timeSinceEpoch - m_currentTime;
88 std::this_thread::sleep_for(std::chrono::nanoseconds(duration_cast<nanoseconds>(
89 boost::asio::wait_traits<steady_clock>::to_wait_duration(delta) +
90 typename BaseClock::duration(1)).count()));
91}
92
93template
95
96template
98
99} // namespace ndn::time
Clock that can be used in unit tests for time-dependent tests independent of wall clock.
void setNow(nanoseconds timeSinceEpoch)
Explicitly set clock to timeSinceEpoch.
std::string getSince() const override
BaseClock::time_point getNow() const override
BaseClock::duration toWaitDuration(typename BaseClock::duration d) const override
void advance(nanoseconds duration)
Advance unit test clock by duration.
UnitTestClock(nanoseconds startTime=ClockTraits::getDefaultStartTime())
::boost::chrono::duration< Rep, Period > duration
Definition time.hpp:38
::boost::chrono::nanoseconds nanoseconds
Definition time.hpp:54