NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
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
virtual BaseClock::time_point getNow() const
Copyright (c) 2011-2015 Regents of the University of California.
virtual boost::posix_time::time_duration toPosixDuration(const typename BaseClock::duration &duration) const
void advance(const nanoseconds &duration)
Advance unit test clock by duration.
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.
virtual std::string getSince() const
const std::chrono::microseconds SLEEP_AFTER_TIME_CHANGE(2)