Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 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" |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 23 | #include "detail/steady-timer.hpp" |
| 24 | |
| 25 | #include <chrono> |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 26 | #include <thread> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace time { |
| 30 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 31 | template<class BaseClock, class ClockTraits> |
| 32 | UnitTestClock<BaseClock, ClockTraits>::UnitTestClock(nanoseconds startTime) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 33 | : m_currentTime(startTime) |
| 34 | { |
| 35 | } |
| 36 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 37 | template<class BaseClock, class ClockTraits> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 38 | std::string |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 39 | UnitTestClock<BaseClock, ClockTraits>::getSince() const |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 40 | { |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 41 | return " since unit test beginning"; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 44 | template<class BaseClock, class ClockTraits> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 45 | typename BaseClock::time_point |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 46 | UnitTestClock<BaseClock, ClockTraits>::getNow() const |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 47 | { |
| 48 | return typename BaseClock::time_point(duration_cast<typename BaseClock::duration>(m_currentTime)); |
| 49 | } |
| 50 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 51 | template<class BaseClock, class ClockTraits> |
| 52 | typename BaseClock::duration |
| 53 | UnitTestClock<BaseClock, ClockTraits>::toWaitDuration(typename BaseClock::duration) const |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 54 | { |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 55 | return typename BaseClock::duration(1); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 58 | template<class BaseClock, class ClockTraits> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 59 | void |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 60 | UnitTestClock<BaseClock, ClockTraits>::advance(nanoseconds duration) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 61 | { |
| 62 | m_currentTime += duration; |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 63 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 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. |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 68 | // |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 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(). |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 71 | // |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 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())); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 79 | template<class BaseClock, class ClockTraits> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 80 | void |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 81 | UnitTestClock<BaseClock, ClockTraits>::setNow(nanoseconds timeSinceEpoch) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 82 | { |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 83 | BOOST_ASSERT(!BaseClock::is_steady || timeSinceEpoch >= m_currentTime); |
| 84 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 85 | m_currentTime = timeSinceEpoch; |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame^] | 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())); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | template |
| 95 | class UnitTestClock<system_clock>; |
| 96 | |
| 97 | template |
| 98 | class UnitTestClock<steady_clock>; |
| 99 | |
| 100 | } // namespace time |
| 101 | } // namespace ndn |