blob: 0c6b90fde88635b3a0d5de793b9ae73fe5caec8d [file] [log] [blame]
Alexander Afanasyev85b17b82014-11-10 16:22:05 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento88a0d812017-08-19 21:31:42 -04002/*
Davide Pesavento5afbb0b2018-01-01 17:24:18 -05003 * Copyright (c) 2013-2018 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
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050022#ifndef NDN_UTIL_TIME_UNIT_TEST_CLOCK_HPP
23#define NDN_UTIL_TIME_UNIT_TEST_CLOCK_HPP
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080024
25#include "time-custom-clock.hpp"
26
27namespace ndn {
28namespace time {
29
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080030/**
31 * @brief Traits for UnitTestClock, defining default behavior for different clocks
32 *
33 * The only behavior that is currently controlled by the traits is default start
34 * time. The generic implementation assumes start time to be zero.
35 */
36template<class BaseClock>
37class UnitTestClockTraits
38{
39public:
40 static nanoseconds
41 getDefaultStartTime()
42 {
43 return nanoseconds::zero();
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020044 }
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080045};
46
47/**
48 * @brief Specialization of UnitTestClockTraits for system_clock
49 *
50 * This specialization sets the default start time to 1415684132 seconds
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050051 * (equivalent to Tue 11 Nov 2014 05:35:32 UTC, if Unix epoch is assumed).
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080052 */
53template<>
54class UnitTestClockTraits<system_clock>
55{
56public:
57 static nanoseconds
58 getDefaultStartTime()
59 {
60 return seconds(1415684132);
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020061 }
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080062};
63
64/**
65 * @brief Clock that can be used in unit tests for time-dependent tests independent of wall clock
66 *
67 * This clock should be explicitly advanced with UnitTestClock<BaseClock>::advance() or set
68 * with UnitTestClock<BaseClock>::setNow() methods.
69 *
70 * @note Default start time is determined by UnitTestClockTraits
71 */
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050072template<class BaseClock, class ClockTraits = UnitTestClockTraits<BaseClock>>
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080073class UnitTestClock : public CustomClock<BaseClock>
74{
75public:
76 explicit
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050077 UnitTestClock(nanoseconds startTime = ClockTraits::getDefaultStartTime());
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080078
79 /**
80 * @brief Advance unit test clock by @p duration
81 */
82 void
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050083 advance(nanoseconds duration);
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080084
85 /**
86 * @brief Explicitly set clock to @p timeSinceEpoch
87 */
88 void
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050089 setNow(nanoseconds timeSinceEpoch);
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080090
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050091public:
Davide Pesavento57c07df2016-12-11 18:41:45 -050092 std::string
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020093 getSince() const override;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080094
Davide Pesavento57c07df2016-12-11 18:41:45 -050095 typename BaseClock::time_point
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020096 getNow() const override;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080097
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050098 typename BaseClock::duration
99 toWaitDuration(typename BaseClock::duration d) const override;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800100
101private:
102 nanoseconds m_currentTime;
103};
104
Davide Pesavento88a0d812017-08-19 21:31:42 -0400105extern template class UnitTestClock<system_clock>;
106extern template class UnitTestClock<steady_clock>;
107
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500108using UnitTestSystemClock = UnitTestClock<system_clock>;
109using UnitTestSteadyClock = UnitTestClock<steady_clock>;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800110
111} // namespace time
112} // namespace ndn
113
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500114#endif // NDN_UTIL_TIME_UNIT_TEST_CLOCK_HPP