Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 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 "time-unit-test-clock.hpp" |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 23 | #include "monotonic_deadline_timer.hpp" |
| 24 | #include <thread> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace time { |
| 28 | |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 29 | const std::chrono::microseconds SLEEP_AFTER_TIME_CHANGE(2); |
| 30 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 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; |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 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); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | template<class BaseClock> |
| 87 | void |
| 88 | UnitTestClock<BaseClock>::setNow(const nanoseconds& timeSinceEpoch) |
| 89 | { |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 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)); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 93 | m_currentTime = timeSinceEpoch; |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 94 | std::this_thread::sleep_for(SLEEP_AFTER_TIME_CHANGE); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | template |
| 98 | class UnitTestClock<system_clock>; |
| 99 | |
| 100 | template |
| 101 | class UnitTestClock<steady_clock>; |
| 102 | |
| 103 | } // namespace time |
| 104 | } // namespace ndn |