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
"
23
#include "
monotonic_deadline_timer.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
39
UnitTestClock<BaseClock>::getSince
()
const
40
{
41
return
" since unit test clock advancements"
;
42
}
43
44
template
<
class
BaseClock>
45
typename
BaseClock::time_point
46
UnitTestClock<BaseClock>::getNow
()
const
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
98
class
UnitTestClock<system_clock>
;
99
100
template
101
class
UnitTestClock<steady_clock>
;
102
103
}
// namespace time
104
}
// namespace ndn
ndn::time::UnitTestClock::getNow
virtual BaseClock::time_point getNow() const
Definition:
time-unit-test-clock.cpp:46
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition:
ndn-strategy-choice-helper.hpp:34
ndn::time::UnitTestClock::toPosixDuration
virtual boost::posix_time::time_duration toPosixDuration(const typename BaseClock::duration &duration) const
Definition:
time-unit-test-clock.cpp:53
time-unit-test-clock.hpp
ndn::time::UnitTestClock::advance
void advance(const nanoseconds &duration)
Advance unit test clock by duration.
Definition:
time-unit-test-clock.cpp:67
ndn::time
Definition:
time-custom-clock.hpp:28
ndn::time::UnitTestClock
Clock that can be used in unit tests for time-dependent tests independent of wall clock...
Definition:
time-unit-test-clock.hpp:74
ndn::time::UnitTestClock::UnitTestClock
UnitTestClock(const nanoseconds &startTime=UnitTestClockTraits< BaseClock >::getDefaultStartTime())
Definition:
time-unit-test-clock.cpp:32
ndn::time::UnitTestClock::setNow
void setNow(const nanoseconds &timeSinceEpoch)
Explicitly set clock to timeSinceEpoch.
Definition:
time-unit-test-clock.cpp:88
ndn::time::UnitTestClock::getSince
virtual std::string getSince() const
Definition:
time-unit-test-clock.cpp:39
ndn::time::SLEEP_AFTER_TIME_CHANGE
const std::chrono::microseconds SLEEP_AFTER_TIME_CHANGE(2)
monotonic_deadline_timer.hpp
ndnSIM
ndn-cxx
src
util
time-unit-test-clock.cpp
Generated on Sat Nov 12 2016 16:02:53 for ndnSIM by
1.8.12