fw: use UnitTestTimeFixture in NCC test case

This commit also changes StrategyTester<S>::onAction to use Signal.

refs #2163

Change-Id: Icd30cfa640972c53efdd8c7f7164b7eb9fc34c1e
diff --git a/tests/daemon/fw/ncc-strategy.cpp b/tests/daemon/fw/ncc-strategy.cpp
index 09d54be..ab3f641 100644
--- a/tests/daemon/fw/ncc-strategy.cpp
+++ b/tests/daemon/fw/ncc-strategy.cpp
@@ -33,7 +33,7 @@
 namespace nfd {
 namespace tests {
 
-BOOST_FIXTURE_TEST_SUITE(FwNccStrategy, BaseFixture)
+BOOST_FIXTURE_TEST_SUITE(FwNccStrategy, UnitTestTimeFixture)
 
 // NccStrategy is fairly complex.
 // The most important property is:
@@ -41,11 +41,11 @@
 // and favors this upstream in subsequent Interests.
 BOOST_AUTO_TEST_CASE(FavorRespondingUpstream)
 {
-  LimitedIo limitedIo;
+  LimitedIo limitedIo(this);
   Forwarder forwarder;
   typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
   shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
-  strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo);
+  strategy->onAction.connect(bind(&LimitedIo::afterOp, &limitedIo));
 
   shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
   shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
@@ -80,7 +80,7 @@
   BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
 
   // forwards to face2 because face1 doesn't respond
-  limitedIo.run(1, time::milliseconds(500));
+  limitedIo.run(1, time::milliseconds(500), time::milliseconds(10));
   BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2);
   BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2);
 
@@ -88,7 +88,7 @@
   shared_ptr<Data> data1p = makeData("ndn:/0Jm1ajrW/%00");
   Data& data1 = *data1p;
   strategy->beforeSatisfyInterest(pitEntry1, *face2, data1);
-  limitedIo.defer(time::milliseconds(500));
+  this->advanceClocks(time::milliseconds(10), time::milliseconds(500));
 
   // second Interest: strategy knows face2 is best
   shared_ptr<Interest> interest2p = makeInterest("ndn:/0Jm1ajrW/%00%01");
@@ -100,18 +100,16 @@
   strategy->afterReceiveInterest(*face3, interest2, fibEntry, pitEntry2);
 
   // forwards to face2 because it responds previously
-  limitedIo.defer(time::milliseconds(1));
+  this->advanceClocks(time::milliseconds(1));
   BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3);
   BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face2);
 }
 
 BOOST_AUTO_TEST_CASE(Bug1853)
 {
-  LimitedIo limitedIo;
   Forwarder forwarder;
   typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
   shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
-  strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo);
 
   shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
   shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
@@ -138,14 +136,14 @@
   pitEntry1->insertOrUpdateInRecord(face3, *interest1);
   strategy->afterReceiveInterest(*face3, *interest1, fibEntry, pitEntry1);
 
-  limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1));
+  this->advanceClocks(time::milliseconds(1));
   BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1);
   BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
 
   // face1 responds
   shared_ptr<Data> data1 = makeData("ndn:/nztwIvHX/%00");
   strategy->beforeSatisfyInterest(pitEntry1, *face1, *data1);
-  limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500));
+  this->advanceClocks(time::milliseconds(10), time::milliseconds(500));
 
   // second Interest: bestFace is face1, nUpstreams becomes 0,
   // therefore pitEntryInfo->maxInterval cannot be calculated from deferRange and nUpstreams
@@ -158,16 +156,16 @@
 
   // FIB entry is changed before doPropagate executes
   fibEntry->addNextHop(face2, 20);
-  limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1000));// should not crash
+  this->advanceClocks(time::milliseconds(10), time::milliseconds(1000));// should not crash
 }
 
 BOOST_AUTO_TEST_CASE(Bug1961)
 {
-  LimitedIo limitedIo;
+  LimitedIo limitedIo(this);
   Forwarder forwarder;
   typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
   shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
-  strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo);
+  strategy->onAction.connect(bind(&LimitedIo::afterOp, &limitedIo));
 
   shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
   shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
@@ -194,7 +192,8 @@
 
   pitEntry1->insertOrUpdateInRecord(face3, *interest1);
   strategy->afterReceiveInterest(*face3, *interest1, fibEntry, pitEntry1);
-  limitedIo.run(2 - strategy->m_sendInterestHistory.size(), time::milliseconds(2000));
+  limitedIo.run(2 - strategy->m_sendInterestHistory.size(),
+                time::milliseconds(2000), time::milliseconds(10));
   BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2);
   BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
   BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2);
@@ -203,10 +202,10 @@
   shared_ptr<Data> data1 = makeData("ndn:/seRMz5a6/%00");
   strategy->beforeSatisfyInterest(pitEntry1, *face1, *data1);
   pitEntry1->deleteInRecords();
-  limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(10));
+  this->advanceClocks(time::milliseconds(10));
   // face2 also responds
   strategy->beforeSatisfyInterest(pitEntry1, *face2, *data1);
-  limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(10));
+  this->advanceClocks(time::milliseconds(10));
 
   // second Interest: bestFace should be face 1
   shared_ptr<Interest> interest2 = makeInterest("ndn:/seRMz5a6/%01");
@@ -215,7 +214,8 @@
 
   pitEntry2->insertOrUpdateInRecord(face3, *interest2);
   strategy->afterReceiveInterest(*face3, *interest2, fibEntry, pitEntry2);
-  limitedIo.run(3 - strategy->m_sendInterestHistory.size(), time::milliseconds(2000));
+  limitedIo.run(3 - strategy->m_sendInterestHistory.size(),
+                time::milliseconds(2000), time::milliseconds(10));
 
   BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3);
   BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face1);
@@ -223,11 +223,11 @@
 
 BOOST_AUTO_TEST_CASE(Bug1971)
 {
-  LimitedIo limitedIo;
+  LimitedIo limitedIo(this);
   Forwarder forwarder;
   typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
   shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
-  strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo);
+  strategy->onAction.connect(bind(&LimitedIo::afterOp, &limitedIo));
 
   shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
   shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
@@ -251,7 +251,8 @@
 
   pitEntry1->insertOrUpdateInRecord(face1, *interest1);
   strategy->afterReceiveInterest(*face1, *interest1, fibEntry, pitEntry1);
-  limitedIo.run(1 - strategy->m_sendInterestHistory.size(), time::milliseconds(2000));
+  limitedIo.run(1 - strategy->m_sendInterestHistory.size(),
+                time::milliseconds(2000), time::milliseconds(10));
   BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1);
   BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face2);
 
@@ -261,12 +262,13 @@
   strategy->beforeSatisfyInterest(pitEntry1, *face2, *data1);
   pitEntry1->deleteOutRecord(*face2);
   pitEntry1->deleteInRecords();
-  limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(10));
+  this->advanceClocks(time::milliseconds(10));
 
   // similar Interest: strategy should still forward it
   pitEntry1->insertOrUpdateInRecord(face1, *interest1);
   strategy->afterReceiveInterest(*face1, *interest1, fibEntry, pitEntry1);
-  limitedIo.run(2 - strategy->m_sendInterestHistory.size(), time::milliseconds(2000));
+  limitedIo.run(2 - strategy->m_sendInterestHistory.size(),
+                time::milliseconds(2000), time::milliseconds(10));
   BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2);
   BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2);
 }
diff --git a/tests/daemon/fw/strategy-tester.hpp b/tests/daemon/fw/strategy-tester.hpp
index a1b920d..980144d 100644
--- a/tests/daemon/fw/strategy-tester.hpp
+++ b/tests/daemon/fw/strategy-tester.hpp
@@ -48,7 +48,7 @@
   }
 
   /// fires after each Action
-  EventEmitter<> onAction;
+  signal::Signal<StrategyTester<S>> onAction;
 
 protected:
   virtual void
diff --git a/tests/limited-io.cpp b/tests/limited-io.cpp
index d6280c9..4bfd802 100644
--- a/tests/limited-io.cpp
+++ b/tests/limited-io.cpp
@@ -35,13 +35,21 @@
 const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min();
 
 LimitedIo::LimitedIo()
-  : m_isRunning(false)
+  : m_uttf(nullptr)
+  , m_isRunning(false)
+  , m_nOpsRemaining(0)
+{
+}
+
+LimitedIo::LimitedIo(UnitTestTimeFixture* uttf)
+  : m_uttf(uttf)
+  , m_isRunning(false)
   , m_nOpsRemaining(0)
 {
 }
 
 LimitedIo::StopReason
-LimitedIo::run(int nOpsLimit, const time::nanoseconds& timeLimit)
+LimitedIo::run(int nOpsLimit, const time::nanoseconds& timeLimit, const time::nanoseconds& tick)
 {
   BOOST_ASSERT(!m_isRunning);
 
@@ -58,7 +66,15 @@
   }
 
   try {
-    getGlobalIoService().run();
+    if (m_uttf == nullptr) {
+      getGlobalIoService().run();
+    }
+    else {
+      // timeLimit is enforced by afterTimeout
+      m_uttf->advanceClocks(tick, time::nanoseconds::max());
+    }
+  }
+  catch (StopException&) {
   }
   catch (std::exception& ex) {
     m_reason = EXCEPTION;
@@ -77,7 +93,6 @@
 {
   if (!m_isRunning) {
     // Do not proceed further if .afterOp() is invoked out of .run(),
-    // because io_service.stop() without io_service.reset() would leave it unusable.
     return;
   }
 
@@ -85,6 +100,9 @@
   if (m_nOpsRemaining <= 0) {
     m_reason = EXCEED_OPS;
     getGlobalIoService().stop();
+    if (m_uttf != nullptr) {
+      throw StopException();
+    }
   }
 }
 
@@ -93,6 +111,9 @@
 {
   m_reason = EXCEED_TIME;
   getGlobalIoService().stop();
+  if (m_uttf != nullptr) {
+    throw StopException();
+  }
 }
 
 const std::exception&
diff --git a/tests/limited-io.hpp b/tests/limited-io.hpp
index 431032a..ee358b0 100644
--- a/tests/limited-io.hpp
+++ b/tests/limited-io.hpp
@@ -26,6 +26,7 @@
 #ifndef NFD_TESTS_LIMITED_IO_HPP
 #define NFD_TESTS_LIMITED_IO_HPP
 
+#include "test-common.hpp"
 #include "core/global-io.hpp"
 #include "core/scheduler.hpp"
 
@@ -39,6 +40,10 @@
 public:
   LimitedIo();
 
+  /** \brief construct with UnitTestTimeFixture
+   */
+  LimitedIo(UnitTestTimeFixture* uttf);
+
   /// indicates why .run returns
   enum StopReason
   {
@@ -53,15 +58,14 @@
   };
 
   /** \brief g_io.run() with operation count and/or time limit
-   *
    *  \param nOpsLimit operation count limit, pass UNLIMITED_OPS 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
+   *  \param tick if this LimitedIo is constructed with UnitTestTimeFixture,
+   *              this is passed to .advanceClocks(), otherwise ignored
    */
   StopReason
-  run(int nOpsLimit, const time::nanoseconds& timeLimit);
+  run(int nOpsLimit, const time::nanoseconds& timeLimit,
+      const time::nanoseconds& tick = time::milliseconds(1));
 
   /// count an operation
   void
@@ -81,6 +85,12 @@
   }
 
 private:
+  /** \brief an exception to stop IO operation
+   */
+  class StopException
+  {
+  };
+
   void
   afterTimeout();
 
@@ -89,6 +99,7 @@
   static const time::nanoseconds UNLIMITED_TIME;
 
 private:
+  UnitTestTimeFixture* m_uttf;
   bool m_isRunning;
   int m_nOpsRemaining;
   EventId m_timeout;
diff --git a/tests/test-common.hpp b/tests/test-common.hpp
index e3bc0b5..247337d 100644
--- a/tests/test-common.hpp
+++ b/tests/test-common.hpp
@@ -127,6 +127,8 @@
     }
   }
 
+  friend class LimitedIo;
+
 protected:
   shared_ptr<time::UnitTestSteadyClock> steadyClock;
   shared_ptr<time::UnitTestSystemClock> systemClock;