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
22
#include "
ndn-cxx/util/time-unit-test-clock.hpp
"
23
#include "
ndn-cxx/util/impl/steady-timer.hpp
"
24
25
#include <chrono>
26
#include <thread>
27
28
namespace
ndn
{
29
namespace
time
{
30
31
template
<
class
BaseClock,
class
ClockTraits>
32
UnitTestClock<BaseClock, ClockTraits>::UnitTestClock
(nanoseconds startTime)
33
: m_currentTime(startTime)
34
{
35
}
36
37
template
<
class
BaseClock,
class
ClockTraits>
38
std::string
39
UnitTestClock<BaseClock, ClockTraits>::getSince
()
const
40
{
41
return
" since unit test beginning"
;
42
}
43
44
template
<
class
BaseClock,
class
ClockTraits>
45
typename
BaseClock::time_point
46
UnitTestClock<BaseClock, ClockTraits>::getNow
()
const
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
53
UnitTestClock<BaseClock, ClockTraits>::toWaitDuration
(
typename
BaseClock::duration)
const
54
{
55
return
typename
BaseClock::duration(1);
56
}
57
58
template
<
class
BaseClock,
class
ClockTraits>
59
void
60
UnitTestClock<BaseClock, ClockTraits>::advance
(nanoseconds duration)
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
81
UnitTestClock<BaseClock, ClockTraits>::setNow
(nanoseconds timeSinceEpoch)
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
95
class
UnitTestClock<system_clock>
;
96
97
template
98
class
UnitTestClock<steady_clock>
;
99
100
}
// namespace time
101
}
// namespace ndn
time-unit-test-clock.hpp
ndn::time::UnitTestClock::toWaitDuration
BaseClock::duration toWaitDuration(typename BaseClock::duration d) const override
Definition:
time-unit-test-clock.cpp:53
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::getSince
std::string getSince() const override
Definition:
time-unit-test-clock.cpp:39
ndn::time::UnitTestClock::UnitTestClock
UnitTestClock(nanoseconds startTime=ClockTraits::getDefaultStartTime())
Definition:
time-unit-test-clock.cpp:32
ndn::time
Definition:
time-custom-clock.hpp:28
steady-timer.hpp
ndn::time::UnitTestClock::setNow
void setNow(nanoseconds timeSinceEpoch)
Explicitly set clock to timeSinceEpoch.
Definition:
time-unit-test-clock.cpp:81
ndn::time::UnitTestClock::advance
void advance(nanoseconds duration)
Advance unit test clock by duration.
Definition:
time-unit-test-clock.cpp:60
ndn::time::UnitTestClock::getNow
BaseClock::time_point getNow() const override
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
ndnSIM
ndn-cxx
ndn-cxx
util
time-unit-test-clock.cpp
Generated on Mon Jun 1 2020 22:32:15 for ndnSIM by
1.8.18