fw: use UnitTestClock in Forwarder persistent loop test case

refs #2162

Change-Id: Ibbf1d5fa615e4275a86caf64f79890d40cb09a77
diff --git a/tests/daemon/fw/forwarder.cpp b/tests/daemon/fw/forwarder.cpp
index 930de00..e824514 100644
--- a/tests/daemon/fw/forwarder.cpp
+++ b/tests/daemon/fw/forwarder.cpp
@@ -424,36 +424,40 @@
   BOOST_CHECK_EQUAL(face4->m_sentDatas.size(), 1);
 }
 
-static inline void
-delayedInterestLoop(const time::nanoseconds& delay, DummyFace& face, const Interest& interest)
+BOOST_FIXTURE_TEST_CASE(InterestLoopWithShortLifetime, UnitTestTimeFixture) // Bug 1953
 {
-  scheduler::schedule(delay, bind(&DummyFace::receiveInterest, &face, cref(interest)));
-}
-
-BOOST_AUTO_TEST_CASE(Bug1953) // persistent loop with short InterestLifetime
-{
-  LimitedIo limitedIo;
   Forwarder forwarder;
-  shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
-  shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
+  auto face1 = make_shared<DummyFace>();
+  auto face2 = make_shared<DummyFace>();
   forwarder.addFace(face1);
   forwarder.addFace(face2);
 
   // cause an Interest sent out of face2 to loop back into face1 after a delay
-  face2->onSendInterest += bind(&delayedInterestLoop, time::milliseconds(170), ref(*face1), _1);
+  face2->onSendInterest += [&face1] (const Interest& interest) {
+    scheduler::schedule(time::milliseconds(170), [&] { face1->receiveInterest(interest); });
+  };
 
   Fib& fib = forwarder.getFib();
   shared_ptr<fib::Entry> fibEntry = fib.insert(Name("ndn:/A")).first;
   fibEntry->addNextHop(face2, 0);
 
+  // receive an Interest
   shared_ptr<Interest> interest = makeInterest("ndn:/A/1");
   interest->setNonce(82101183);
   interest->setInterestLifetime(time::milliseconds(50));
   face1->receiveInterest(*interest);
 
-  limitedIo.defer(time::milliseconds(1000));
+  // interest should be forwarded only once, as long as Nonce is in Dead Nonce List
+  BOOST_ASSERT(time::milliseconds(25) * 40 < forwarder.getDeadNonceList().getLifetime());
+  this->advanceClocks(time::milliseconds(25), 40);
 
   BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 1);
+
+  // It's unnecessary to check that Interest with duplicate Nonce can be forwarded again
+  // after it's gone from Dead Nonce List, because the entry lifetime of Dead Nonce List
+  // is an implementation decision. NDN protocol requires Name+Nonce to be unique,
+  // without specifying when Name+Nonce could repeat. Forwarder is permitted to suppress
+  // an Interest if its Name+Nonce has appeared any point in the past.
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/limited-io.cpp b/tests/limited-io.cpp
index 9df197c..d6280c9 100644
--- a/tests/limited-io.cpp
+++ b/tests/limited-io.cpp
@@ -41,7 +41,7 @@
 }
 
 LimitedIo::StopReason
-LimitedIo::run(int nOpsLimit, const time::nanoseconds& nTimeLimit)
+LimitedIo::run(int nOpsLimit, const time::nanoseconds& timeLimit)
 {
   BOOST_ASSERT(!m_isRunning);
 
@@ -53,8 +53,8 @@
 
   m_reason = NO_WORK;
   m_nOpsRemaining = nOpsLimit;
-  if (nTimeLimit >= time::nanoseconds::zero()) {
-    m_timeout = scheduler::schedule(nTimeLimit, bind(&LimitedIo::afterTimeout, this));
+  if (timeLimit >= time::nanoseconds::zero()) {
+    m_timeout = scheduler::schedule(timeLimit, bind(&LimitedIo::afterTimeout, this));
   }
 
   try {
diff --git a/tests/limited-io.hpp b/tests/limited-io.hpp
index 92aef02..431032a 100644
--- a/tests/limited-io.hpp
+++ b/tests/limited-io.hpp
@@ -55,10 +55,13 @@
   /** \brief g_io.run() with operation count and/or time limit
    *
    *  \param nOpsLimit operation count limit, pass UNLIMITED_OPS for no limit
-   *  \param nTimeLimit time limit, pass UNLIMITED_TIME for no limit
+   *  \param timeLimit time limit, pass UNLIMITED_TIME for no limit
+   *
+   *  \warning if timeLimit is used with UnitTestTimeFixture,
+   *           some other code must advance steady clock in order to exceed time limit
    */
   StopReason
-  run(int nOpsLimit, const time::nanoseconds& nTimeLimit);
+  run(int nOpsLimit, const time::nanoseconds& timeLimit);
 
   /// count an operation
   void
diff --git a/tests/test-common.hpp b/tests/test-common.hpp
index 9c60c77..d122189 100644
--- a/tests/test-common.hpp
+++ b/tests/test-common.hpp
@@ -31,6 +31,7 @@
 #include "core/global-io.hpp"
 #include "core/logger.hpp"
 
+#include <ndn-cxx/util/time-unit-test-clock.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
 
 namespace nfd {
@@ -59,6 +60,48 @@
   boost::asio::io_service& g_io;
 };
 
+/** \brief a base test fixture that overrides steady clock and system clock
+ */
+class UnitTestTimeFixture : public BaseFixture
+{
+public:
+  UnitTestTimeFixture()
+    : steadyClock(make_shared<time::UnitTestSteadyClock>())
+    , systemClock(make_shared<time::UnitTestSystemClock>())
+  {
+    time::setCustomClocks(steadyClock, systemClock);
+  }
+
+  ~UnitTestTimeFixture()
+  {
+    time::setCustomClocks(nullptr, nullptr);
+  }
+
+  /** \brief advance steady and system clocks
+   *
+   *  Clocks are advanced in increments of \p tick for \p nTicks ticks.
+   *  After each tick, global io_service is polled to process pending I/O events.
+   *
+   *  Exceptions thrown during I/O events are propagated to the caller.
+   *  Clock advancing would stop in case of an exception.
+   */
+  void
+  advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1)
+  {
+    for (size_t i = 0; i < nTicks; ++i) {
+      steadyClock->advance(tick);
+      systemClock->advance(tick);
+
+      if (g_io.stopped())
+        g_io.reset();
+      g_io.poll();
+    }
+  }
+
+protected:
+  shared_ptr<time::UnitTestSteadyClock> steadyClock;
+  shared_ptr<time::UnitTestSystemClock> systemClock;
+};
 
 inline shared_ptr<Interest>
 makeInterest(const Name& name)
@@ -71,7 +114,7 @@
 {
   ndn::SignatureSha256WithRsa fakeSignature;
   fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
-                                        reinterpret_cast<const uint8_t*>(0), 0));
+                                        static_cast<const uint8_t*>(nullptr), 0));
   data->setSignature(fakeSignature);
   data->wireEncode();