NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2018 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 
24 
25 #include <chrono>
26 #include <thread>
27 
28 namespace ndn {
29 namespace time {
30 
31 template<class BaseClock, class ClockTraits>
33  : m_currentTime(startTime)
34 {
35 }
36 
37 template<class BaseClock, class ClockTraits>
38 std::string
40 {
41  return " since unit test beginning";
42 }
43 
44 template<class BaseClock, class ClockTraits>
45 typename BaseClock::time_point
47 {
48  return typename BaseClock::time_point(duration_cast<typename BaseClock::duration>(m_currentTime));
49 }
50 
51 template<class BaseClock, class ClockTraits>
52 typename BaseClock::duration
54 {
55  return typename BaseClock::duration(1);
56 }
57 
58 template<class BaseClock, class ClockTraits>
59 void
61 {
62  m_currentTime += duration;
63 
64  // When UnitTestClock is used with Asio timers (e.g. basic_waitable_timer), and
65  // an async wait operation on a timer is already in progress, Asio won't look
66  // at the clock again until the earliest timer has expired, so it won't know that
67  // the current time has changed.
68  //
69  // Therefore, in order for the clock advancement to be effective, we must sleep
70  // for a period greater than wait_traits::to_wait_duration().
71  //
72  // See also http://blog.think-async.com/2007/08/time-travel.html - "Jumping Through Time"
73  //
74  std::this_thread::sleep_for(std::chrono::nanoseconds(duration_cast<nanoseconds>(
75  boost::asio::wait_traits<steady_clock>::to_wait_duration(duration) +
76  typename BaseClock::duration(1)).count()));
77 }
78 
79 template<class BaseClock, class ClockTraits>
80 void
82 {
83  BOOST_ASSERT(!BaseClock::is_steady || timeSinceEpoch >= m_currentTime);
84 
85  m_currentTime = timeSinceEpoch;
86 
87  // See comment in advance()
88  auto delta = timeSinceEpoch - m_currentTime;
89  std::this_thread::sleep_for(std::chrono::nanoseconds(duration_cast<nanoseconds>(
90  boost::asio::wait_traits<steady_clock>::to_wait_duration(delta) +
91  typename BaseClock::duration(1)).count()));
92 }
93 
94 template
96 
97 template
99 
100 } // namespace time
101 } // namespace ndn
void setNow(nanoseconds timeSinceEpoch)
Explicitly set clock to timeSinceEpoch.
Copyright (c) 2011-2015 Regents of the University of California.
boost::chrono::duration< Rep, Period > duration
Definition: time.hpp:34
void advance(nanoseconds duration)
Advance unit test clock by duration.
UnitTestClock(nanoseconds startTime=ClockTraits::getDefaultStartTime())
Clock that can be used in unit tests for time-dependent tests independent of wall clock...
BaseClock::time_point getNow() const override
BaseClock::duration toWaitDuration(typename BaseClock::duration d) const override
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
std::string getSince() const override