blob: 58e1f0379e0cd5d2a3ad6156d568c9bcc03dbee3 [file] [log] [blame]
Alexander Afanasyev85b17b82014-11-10 16:22:05 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5bcab562017-07-16 17:19:14 +00002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyev85b17b82014-11-10 16:22:05 -08004 *
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"
Junxiao Shi5bcab562017-07-16 17:19:14 +000023#include "detail/monotonic-deadline-timer.hpp"
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -080024#include <thread>
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080025
26namespace ndn {
27namespace time {
28
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -080029const std::chrono::microseconds SLEEP_AFTER_TIME_CHANGE(2);
30
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080031template<class BaseClock>
32UnitTestClock<BaseClock>::UnitTestClock(const nanoseconds& startTime)
33 : m_currentTime(startTime)
34{
35}
36
37template<class BaseClock>
38std::string
39UnitTestClock<BaseClock>::getSince() const
40{
41 return " since unit test clock advancements";
42}
43
44template<class BaseClock>
45typename BaseClock::time_point
46UnitTestClock<BaseClock>::getNow() const
47{
48 return typename BaseClock::time_point(duration_cast<typename BaseClock::duration>(m_currentTime));
49}
50
51template<class BaseClock>
52boost::posix_time::time_duration
53UnitTestClock<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
65template<class BaseClock>
66void
67UnitTestClock<BaseClock>::advance(const nanoseconds& duration)
68{
69 m_currentTime += duration;
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -080070
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);
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080084}
85
86template<class BaseClock>
87void
88UnitTestClock<BaseClock>::setNow(const nanoseconds& timeSinceEpoch)
89{
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -080090 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));
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080093 m_currentTime = timeSinceEpoch;
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -080094 std::this_thread::sleep_for(SLEEP_AFTER_TIME_CHANGE);
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080095}
96
97template
98class UnitTestClock<system_clock>;
99
100template
101class UnitTestClock<steady_clock>;
102
103} // namespace time
104} // namespace ndn