NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017 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 
22 #include "time-unit-test-clock.hpp"
24 #include <thread>
25 
26 namespace ndn {
27 namespace time {
28 
29 const std::chrono::microseconds SLEEP_AFTER_TIME_CHANGE(2);
30 
31 template<class BaseClock>
32 UnitTestClock<BaseClock>::UnitTestClock(const nanoseconds& startTime)
33  : m_currentTime(startTime)
34 {
35 }
36 
37 template<class BaseClock>
38 std::string
40 {
41  return " since unit test clock advancements";
42 }
43 
44 template<class BaseClock>
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>
52 boost::posix_time::time_duration
53 UnitTestClock<BaseClock>::toPosixDuration(const typename BaseClock::duration& duration) const
54 {
55  return
56 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
57  boost::posix_time::nanoseconds(1)
58 #else
59  boost::posix_time::microseconds(1)
60 #endif
61  ;
62 }
63 
64 
65 template<class BaseClock>
66 void
67 UnitTestClock<BaseClock>::advance(const nanoseconds& duration)
68 {
69  m_currentTime += duration;
70 
71  // On some platforms, boost::asio::io_service for deadline_timer (e.g., the one used in
72  // Scheduler) will call time_traits<>::now() and will "sleep" for
73  // time_traits<>::to_posix_time(duration) period before calling time_traits<>::now()
74  // again. (Note that such "sleep" will occur even if there is no actual waiting and
75  // program is calling io_service.poll().)
76  //
77  // As a result, in order for the clock advancement to be effective, we must sleep for a
78  // period greater than time_traits<>::to_posix_time().
79  //
80  // See also http://blog.think-async.com/2007/08/time-travel.html
81  BOOST_ASSERT(boost::posix_time::microseconds(SLEEP_AFTER_TIME_CHANGE.count()) >
82  boost::asio::time_traits<steady_clock>::to_posix_duration(duration));
83  std::this_thread::sleep_for(SLEEP_AFTER_TIME_CHANGE);
84 }
85 
86 template<class BaseClock>
87 void
88 UnitTestClock<BaseClock>::setNow(const nanoseconds& timeSinceEpoch)
89 {
90  BOOST_ASSERT(boost::posix_time::microseconds(SLEEP_AFTER_TIME_CHANGE.count()) >
91  boost::asio::time_traits<steady_clock>::to_posix_duration(timeSinceEpoch -
92  m_currentTime));
93  m_currentTime = timeSinceEpoch;
94  std::this_thread::sleep_for(SLEEP_AFTER_TIME_CHANGE);
95 }
96 
97 template
99 
100 template
102 
103 } // namespace time
104 } // namespace ndn
std::string getSince() const override
Copyright (c) 2011-2015 Regents of the University of California.
void advance(const nanoseconds &duration)
Advance unit test clock by duration.
BaseClock::time_point getNow() const override
boost::posix_time::time_duration toPosixDuration(const typename BaseClock::duration &duration) const override
Clock that can be used in unit tests for time-dependent tests independent of wall clock...
UnitTestClock(const nanoseconds &startTime=UnitTestClockTraits< BaseClock >::getDefaultStartTime())
void setNow(const nanoseconds &timeSinceEpoch)
Explicitly set clock to timeSinceEpoch.
const std::chrono::microseconds SLEEP_AFTER_TIME_CHANGE(2)