time: Add ability to specialize system and steady clocks
Default implementation is "optimized" to use boost::chrono clocks. When
specialization is used, now() calls are redirected to the virtual
methods of the specified specialization.
This commit also includes an implementation of UnitTestClock that
can be used in unit tests to eliminate dependency on the wall clock.
Change-Id: I7ced774634d90ce32714a71e2a66129cba91b9ae
Refs: #2158
diff --git a/src/util/time-unit-test-clock.hpp b/src/util/time-unit-test-clock.hpp
new file mode 100644
index 0000000..d1bbe7d
--- /dev/null
+++ b/src/util/time-unit-test-clock.hpp
@@ -0,0 +1,114 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_TIME_UNIT_TEST_CLOCK_HPP
+#define NDN_TIME_UNIT_TEST_CLOCK_HPP
+
+#include "time-custom-clock.hpp"
+
+namespace ndn {
+namespace time {
+
+
+/**
+ * @brief Traits for UnitTestClock, defining default behavior for different clocks
+ *
+ * The only behavior that is currently controlled by the traits is default start
+ * time. The generic implementation assumes start time to be zero.
+ */
+template<class BaseClock>
+class UnitTestClockTraits
+{
+public:
+ static nanoseconds
+ getDefaultStartTime()
+ {
+ return nanoseconds::zero();
+ };
+};
+
+/**
+ * @brief Specialization of UnitTestClockTraits for system_clock
+ *
+ * This specialization sets the default start time to 1415684132 seconds
+ * (equivalent to Tue, 11 Nov 2014 05:35:32 UTC if unix epoch is assumed).
+ */
+template<>
+class UnitTestClockTraits<system_clock>
+{
+public:
+ static nanoseconds
+ getDefaultStartTime()
+ {
+ return seconds(1415684132);
+ };
+};
+
+/**
+ * @brief Clock that can be used in unit tests for time-dependent tests independent of wall clock
+ *
+ * This clock should be explicitly advanced with UnitTestClock<BaseClock>::advance() or set
+ * with UnitTestClock<BaseClock>::setNow() methods.
+ *
+ * @note Default start time is determined by UnitTestClockTraits
+ */
+template<class BaseClock>
+class UnitTestClock : public CustomClock<BaseClock>
+{
+public:
+ explicit
+ UnitTestClock(const nanoseconds& startTime =
+ UnitTestClockTraits<BaseClock>::getDefaultStartTime());
+
+ /**
+ * @brief Advance unit test clock by @p duration
+ */
+ void
+ advance(const nanoseconds& duration);
+
+ /**
+ * @brief Explicitly set clock to @p timeSinceEpoch
+ */
+ void
+ setNow(const nanoseconds& timeSinceEpoch);
+
+public: // CustomClock<BaseClock>
+
+ virtual std::string
+ getSince() const;
+
+ virtual typename BaseClock::time_point
+ getNow() const;
+
+ virtual boost::posix_time::time_duration
+ toPosixDuration(const typename BaseClock::duration& duration) const;
+
+private:
+ nanoseconds m_currentTime;
+};
+
+typedef UnitTestClock<system_clock> UnitTestSystemClock;
+typedef UnitTestClock<steady_clock> UnitTestSteadyClock;
+
+} // namespace time
+} // namespace ndn
+
+#endif // NDN_TIME_UNIT_TEST_CLOCK_HPP