tests: Fix issue with UnitTestClock-based event scheduling (extra sleep required)

Change-Id: I77f24f630697b6e41b3d935f0f1416e806516412
Refs: #2152
diff --git a/src/security/certificate-cache-ttl.cpp b/src/security/certificate-cache-ttl.cpp
new file mode 100644
index 0000000..2ab13c9
--- /dev/null
+++ b/src/security/certificate-cache-ttl.cpp
@@ -0,0 +1,105 @@
+/* -*- 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.
+ *
+ * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
+ */
+
+#include "certificate-cache-ttl.hpp"
+
+namespace ndn {
+
+CertificateCacheTtl::CertificateCacheTtl(boost::asio::io_service& io,
+                                         const time::seconds& defaultTtl/* = time::seconds(3600)*/)
+  : m_defaultTtl(defaultTtl)
+  , m_io(io)
+  , m_scheduler(m_io)
+{
+}
+
+CertificateCacheTtl::~CertificateCacheTtl()
+{
+}
+
+void
+CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
+{
+  m_io.dispatch([this, certificate] { this->insert(certificate); });
+}
+
+shared_ptr<const IdentityCertificate>
+CertificateCacheTtl::getCertificate(const Name& certificateName)
+{
+  Cache::iterator it = m_cache.find(certificateName);
+  if (it != m_cache.end())
+    return it->second.first;
+  else
+    return shared_ptr<IdentityCertificate>();
+}
+
+void
+CertificateCacheTtl::reset()
+{
+  m_io.dispatch([this] { this->removeAll(); });
+}
+
+size_t
+CertificateCacheTtl::getSize()
+{
+  return m_cache.size();
+}
+
+void
+CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
+{
+  time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
+                               certificate->getFreshnessPeriod() : m_defaultTtl);
+
+  Name index = certificate->getName().getPrefix(-1);
+
+  Cache::iterator it = m_cache.find(index);
+  if (it != m_cache.end())
+    m_scheduler.cancelEvent(it->second.second);
+
+  EventId eventId = m_scheduler.scheduleEvent(expire,
+                                              bind(&CertificateCacheTtl::remove,
+                                                   this, certificate->getName()));
+
+  m_cache[index] = std::make_pair(certificate, eventId);
+}
+
+void
+CertificateCacheTtl::remove(const Name& certificateName)
+{
+  Name name = certificateName.getPrefix(-1);
+  Cache::iterator it = m_cache.find(name);
+  if (it != m_cache.end())
+    m_cache.erase(it);
+}
+
+void
+CertificateCacheTtl::removeAll()
+{
+  for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++)
+    m_scheduler.cancelEvent(it->second.second);
+
+  m_cache.clear();
+}
+
+} // namespace ndn
diff --git a/src/security/certificate-cache-ttl.hpp b/src/security/certificate-cache-ttl.hpp
index 650a9e7..55cbc5a 100644
--- a/src/security/certificate-cache-ttl.hpp
+++ b/src/security/certificate-cache-ttl.hpp
@@ -30,42 +30,42 @@
 
 namespace ndn {
 
+/**
+ * @brief Cache of validated certificates with freshness-based eviction policy
+ *
+ * Validated certificates will stay in cache for the duration of their freshness period.
+ * The lifetime of the certificate in cache can be extended by "re-inserting" it in the cache.
+ */
 class CertificateCacheTtl : public CertificateCache
 {
 public:
   explicit
   CertificateCacheTtl(boost::asio::io_service& io,
-                      const time::seconds& defaultTtl = time::seconds(3600))
-    : m_defaultTtl(defaultTtl)
-    , m_scheduler(io)
-  {
-  }
+                      const time::seconds& defaultTtl = time::seconds(3600));
 
   virtual
-  ~CertificateCacheTtl()
-  {
-  }
+  ~CertificateCacheTtl();
 
-  virtual inline void
+  virtual void
   insertCertificate(shared_ptr<const IdentityCertificate> certificate);
 
-  virtual inline shared_ptr<const IdentityCertificate>
+  virtual shared_ptr<const IdentityCertificate>
   getCertificate(const Name& certificateNameWithoutVersion);
 
-  virtual inline void
+  virtual void
   reset();
 
-  virtual inline size_t
+  virtual size_t
   getSize();
 
 private:
-  inline void
+  void
   insert(shared_ptr<const IdentityCertificate> certificate);
 
-  inline void
+  void
   remove(const Name& certificateName);
 
-  inline void
+  void
   removeAll();
 
 protected:
@@ -73,77 +73,10 @@
 
   time::seconds m_defaultTtl;
   Cache m_cache;
+  boost::asio::io_service& m_io;
   Scheduler m_scheduler;
 };
 
-inline void
-CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
-{
-  m_scheduler.scheduleEvent(time::seconds(0),
-                            bind(&CertificateCacheTtl::insert, this, certificate));
-}
-
-inline shared_ptr<const IdentityCertificate>
-CertificateCacheTtl::getCertificate(const Name& certificateName)
-{
-  Cache::iterator it = m_cache.find(certificateName);
-  if (it != m_cache.end())
-    return it->second.first;
-  else
-    return shared_ptr<IdentityCertificate>();
-}
-
-inline void
-CertificateCacheTtl::reset()
-{
-  m_scheduler.scheduleEvent(time::seconds(0),
-                            bind(&CertificateCacheTtl::removeAll, this));
-}
-
-inline size_t
-CertificateCacheTtl::getSize()
-{
-  return m_cache.size();
-}
-
-inline void
-CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
-{
-  time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
-                               certificate->getFreshnessPeriod() : m_defaultTtl);
-
-  Name index = certificate->getName().getPrefix(-1);
-
-  Cache::iterator it = m_cache.find(index);
-  if (it != m_cache.end())
-    m_scheduler.cancelEvent(it->second.second);
-
-  EventId eventId = m_scheduler.scheduleEvent(expire,
-                                              bind(&CertificateCacheTtl::remove,
-                                                   this, certificate->getName()));
-
-  m_cache[index] = std::make_pair(certificate, eventId);
-}
-
-inline void
-CertificateCacheTtl::remove(const Name& certificateName)
-{
-  Name name = certificateName.getPrefix(-1);
-  Cache::iterator it = m_cache.find(name);
-  if (it != m_cache.end())
-    m_cache.erase(it);
-}
-
-inline void
-CertificateCacheTtl::removeAll()
-{
-  for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++)
-    m_scheduler.cancelEvent(it->second.second);
-
-  m_cache.clear();
-}
-
-
 } // namespace ndn
 
-#endif //NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP
+#endif // NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP
diff --git a/src/security/certificate-cache.hpp b/src/security/certificate-cache.hpp
index 31296a0..42f3b88 100644
--- a/src/security/certificate-cache.hpp
+++ b/src/security/certificate-cache.hpp
@@ -29,7 +29,10 @@
 
 namespace ndn {
 
-class CertificateCache
+/**
+ * @brief Interface for the cache of validated certificates
+ */
+class CertificateCache : noncopyable
 {
 public:
   virtual
@@ -58,4 +61,4 @@
 
 } // namespace ndn
 
-#endif //NDN_SECURITY_CERTIFICATE_CACHE_HPP
+#endif // NDN_SECURITY_CERTIFICATE_CACHE_HPP
diff --git a/src/util/time-unit-test-clock.cpp b/src/util/time-unit-test-clock.cpp
index 73c30f7..7ed848d 100644
--- a/src/util/time-unit-test-clock.cpp
+++ b/src/util/time-unit-test-clock.cpp
@@ -20,10 +20,14 @@
  */
 
 #include "time-unit-test-clock.hpp"
+#include "monotonic_deadline_timer.hpp"
+#include <thread>
 
 namespace ndn {
 namespace time {
 
+const std::chrono::microseconds SLEEP_AFTER_TIME_CHANGE(2);
+
 template<class BaseClock>
 UnitTestClock<BaseClock>::UnitTestClock(const nanoseconds& startTime)
   : m_currentTime(startTime)
@@ -63,13 +67,31 @@
 UnitTestClock<BaseClock>::advance(const nanoseconds& duration)
 {
   m_currentTime += duration;
+
+  // On some platforms, boost::asio::io_service for deadline_timer (e.g., the one used in
+  // Scheduler) will call time_traits<>::now() and will "sleep" for
+  // time_traits<>::to_posix_time(duration) period before calling time_traits<>::now()
+  // again. (Note that such "sleep" will occur even if there is no actual waiting and
+  // program is calling io_service.poll().)
+  //
+  // As a result, in order for the clock advancement to be effective, we must sleep for a
+  // period greater than time_traits<>::to_posix_time().
+  //
+  // See also http://blog.think-async.com/2007/08/time-travel.html
+  BOOST_ASSERT(boost::posix_time::microseconds(SLEEP_AFTER_TIME_CHANGE.count()) >
+               boost::asio::time_traits<steady_clock>::to_posix_duration(duration));
+  std::this_thread::sleep_for(SLEEP_AFTER_TIME_CHANGE);
 }
 
 template<class BaseClock>
 void
 UnitTestClock<BaseClock>::setNow(const nanoseconds& timeSinceEpoch)
 {
+  BOOST_ASSERT(boost::posix_time::microseconds(SLEEP_AFTER_TIME_CHANGE.count()) >
+               boost::asio::time_traits<steady_clock>::to_posix_duration(timeSinceEpoch -
+                                                                         m_currentTime));
   m_currentTime = timeSinceEpoch;
+  std::this_thread::sleep_for(SLEEP_AFTER_TIME_CHANGE);
 }
 
 template
diff --git a/tests/unit-tests/security/test-certificate-cache.cpp b/tests/unit-tests/security/test-certificate-cache.cpp
index d98dd4a..54454e1 100644
--- a/tests/unit-tests/security/test-certificate-cache.cpp
+++ b/tests/unit-tests/security/test-certificate-cache.cpp
@@ -110,6 +110,7 @@
 BOOST_FIXTURE_TEST_CASE(TtlRefresh, UnitTestTimeFixture)
 {
   cache->insertCertificate(cert1); // 500ms
+
   io.poll();
   BOOST_CHECK_EQUAL(cache->getSize(), 1);
 
@@ -119,6 +120,7 @@
 
     // Refresh certificate in cache
   cache->insertCertificate(cert1); // +500ms
+
   io.poll();
   BOOST_CHECK_EQUAL(cache->getSize(), 1);
 
@@ -140,6 +142,7 @@
   BOOST_CHECK_EQUAL(cache->getSize(), 2);
 
   cache->reset();
+
   io.poll();
   BOOST_CHECK_EQUAL(cache->getSize(), 0);
 }
diff --git a/tests/unit-tests/util/test-time-unit-test-clock.cpp b/tests/unit-tests/util/test-time-unit-test-clock.cpp
index f9b121d..e43d4ed 100644
--- a/tests/unit-tests/util/test-time-unit-test-clock.cpp
+++ b/tests/unit-tests/util/test-time-unit-test-clock.cpp
@@ -24,6 +24,7 @@
 
 #include "boost-test.hpp"
 #include <boost/lexical_cast.hpp>
+#include <thread>
 
 namespace ndn {
 namespace tests {
@@ -54,7 +55,8 @@
 {
   BOOST_CHECK_EQUAL(time::system_clock::now().time_since_epoch(),
                     time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime());
-  usleep(1000000);
+  std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
   BOOST_CHECK_EQUAL(time::system_clock::now().time_since_epoch(),
                     time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime());
 
@@ -109,7 +111,7 @@
   BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(),
                     time::steady_clock::duration::zero());
 
-  usleep(1000000);
+  std::this_thread::sleep_for(std::chrono::milliseconds(100));
   BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(),
                     time::steady_clock::duration::zero());
 
@@ -138,13 +140,12 @@
   bool hasFired = false;
   scheduler.scheduleEvent(time::seconds(100), [&] { hasFired = true; });
 
-  for (size_t i = 0; i < 10; ++i)
-    io.poll();
+  io.poll();
   BOOST_CHECK_EQUAL(hasFired, false);
 
   steadyClock->advance(time::seconds(100));
-  for (size_t i = 0; i < 10; ++i)
-    io.poll();
+
+  io.poll();
   BOOST_CHECK_EQUAL(hasFired, true);
 }