util: make SegmentFetcher test suite run faster

UnitTestTimeFixture::advanceClocks is invoked with less ticks of
larger duration when feasible, so that test cases run faster.

UnitTestTimeFixture is synchronized from NFD codebase.

refs #2734

Change-Id: Ifd338f1008af8662bb9ea8f798f6cc2a922fb865
diff --git a/tests/unit-tests/util/segment-fetcher.t.cpp b/tests/unit-tests/util/segment-fetcher.t.cpp
index 8fbbb45..462e759 100644
--- a/tests/unit-tests/util/segment-fetcher.t.cpp
+++ b/tests/unit-tests/util/segment-fetcher.t.cpp
@@ -38,7 +38,8 @@
 
 using namespace ndn::tests;
 
-BOOST_AUTO_TEST_SUITE(UtilSegmentFetcher)
+BOOST_AUTO_TEST_SUITE(Util)
+BOOST_AUTO_TEST_SUITE(TestSegmentFetcher)
 
 class Fixture : public IdentityManagementTimeFixture
 {
@@ -46,7 +47,7 @@
   Fixture()
     : face(io, m_keyChain)
     , nErrors(0)
-    , nDatas(0)
+    , nData(0)
     , dataSize(0)
   {
   }
@@ -56,7 +57,7 @@
   {
     const uint8_t buffer[] = "Hello, world!";
 
-    shared_ptr<Data> data = make_shared<Data>(Name(baseName).appendSegment(segment));
+    auto data = make_shared<Data>(Name(baseName).appendSegment(segment));
     data->setContent(buffer, sizeof(buffer));
 
     if (isFinal) {
@@ -77,7 +78,7 @@
   void
   onComplete(const ConstBufferPtr& data)
   {
-    ++nDatas;
+    ++nData;
     dataSize = data->size();
     dataString = std::string(reinterpret_cast<const char*>(data->get()));
   }
@@ -88,7 +89,7 @@
     const Interest& lastInterest = face.sentInterests.back();
     lp::Nack nack = makeNack(lastInterest, nackReason);
     face.receive(nack);
-    advanceClocks(time::milliseconds(1), 10);
+    advanceClocks(time::milliseconds(10));
   }
 
 public:
@@ -96,7 +97,7 @@
 
   uint32_t nErrors;
   uint32_t lastError;
-  uint32_t nDatas;
+  uint32_t nData;
   size_t dataSize;
   std::string dataString;
 };
@@ -109,20 +110,20 @@
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
 
-  advanceClocks(time::milliseconds(1), 99);
-
-  BOOST_CHECK_EQUAL(nErrors, 0);
-  BOOST_CHECK_EQUAL(nDatas, 0);
+  advanceClocks(time::milliseconds(1));
   BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
-  BOOST_CHECK_EQUAL(face.sentData.size(), 0);
 
   const Interest& interest = face.sentInterests[0];
   BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
   BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
   BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
 
-  advanceClocks(time::milliseconds(1), 2);
+  advanceClocks(time::milliseconds(98));
+  BOOST_CHECK_EQUAL(nErrors, 0);
+  BOOST_CHECK_EQUAL(nData, 0);
+  BOOST_CHECK_EQUAL(face.sentData.size(), 0);
 
+  advanceClocks(time::milliseconds(1), 2);
   BOOST_CHECK_EQUAL(nErrors, 1);
   BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
   BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
@@ -138,13 +139,13 @@
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   face.receive(*makeDataSegment("/hello/world/version0", 0, true));
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   BOOST_CHECK_EQUAL(nErrors, 0);
-  BOOST_CHECK_EQUAL(nDatas, 1);
+  BOOST_CHECK_EQUAL(nData, 1);
 
   BOOST_CHECK_EQUAL(dataSize, 14);
 
@@ -170,7 +171,7 @@
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   const uint8_t buffer[] = "Hello, world!";
 
@@ -179,11 +180,11 @@
   data->setContent(buffer, sizeof(buffer));
 
   face.receive(*data);
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   BOOST_CHECK_EQUAL(nErrors, 1);
   BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
-  BOOST_CHECK_EQUAL(nDatas, 0);
+  BOOST_CHECK_EQUAL(nData, 0);
 }
 
 BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture)
@@ -194,13 +195,13 @@
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   face.receive(*makeDataSegment("/hello/world/version0", 0, true));
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   BOOST_CHECK_EQUAL(nErrors, 1);
   BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VALIDATION_FAIL));
-  BOOST_CHECK_EQUAL(nDatas, 0);
+  BOOST_CHECK_EQUAL(nData, 0);
 }
 
 BOOST_FIXTURE_TEST_CASE(Triple, Fixture)
@@ -211,19 +212,19 @@
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   face.receive(*makeDataSegment("/hello/world/version0", 0, false));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   face.receive(*makeDataSegment("/hello/world/version0", 1, false));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   face.receive(*makeDataSegment("/hello/world/version0", 2, true));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   BOOST_CHECK_EQUAL(nErrors, 0);
-  BOOST_CHECK_EQUAL(nDatas, 1);
+  BOOST_CHECK_EQUAL(nData, 1);
 
   BOOST_CHECK_EQUAL(dataSize, 42);
 
@@ -260,22 +261,22 @@
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   face.receive(*makeDataSegment("/hello/world/version0", 1, false));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   face.receive(*makeDataSegment("/hello/world/version0", 0, false));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   face.receive(*makeDataSegment("/hello/world/version0", 1, false));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   face.receive(*makeDataSegment("/hello/world/version0", 2, true));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   BOOST_CHECK_EQUAL(nErrors, 0);
-  BOOST_CHECK_EQUAL(nDatas, 1);
+  BOOST_CHECK_EQUAL(nData, 1);
 
   BOOST_CHECK_EQUAL(dataSize, 42);
 
@@ -319,19 +320,19 @@
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   for (uint64_t i = 0; i < 400; i++) {
-    advanceClocks(time::milliseconds(1), 10);
+    advanceClocks(time::milliseconds(10));
     face.receive(*makeDataSegment("/hello/world/version0", i, false));
   }
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   face.receive(*makeDataSegment("/hello/world/version0", 400, true));
 
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   BOOST_CHECK_EQUAL(nErrors, 0);
-  BOOST_CHECK_EQUAL(nDatas, 1);
+  BOOST_CHECK_EQUAL(nData, 1);
 }
 
 BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture)
@@ -340,7 +341,7 @@
                         make_shared<ValidatorNull>(),
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   // receive nack for the original interest
   nackLastInterest(lp::NackReason::DUPLICATE);
@@ -360,7 +361,7 @@
                         make_shared<ValidatorNull>(),
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
 
   // receive nack for the original interest
   nackLastInterest(lp::NackReason::CONGESTION);
@@ -385,19 +386,19 @@
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
 
-  advanceClocks(time::milliseconds(1), 1000);
+  advanceClocks(time::milliseconds(1000));
 
   for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) {
     if (segmentNo == 1) {
       while (nNacks--) {
         nackLastInterest(lp::NackReason::CONGESTION);
-        advanceClocks(time::milliseconds(1), 10);
+        advanceClocks(time::milliseconds(10));
       }
     }
 
     auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3);
     face.receive(*data);
-    advanceClocks(time::milliseconds(1), 10);
+    advanceClocks(time::milliseconds(10));
   }
 
   // Total number of sent interests should be 6: one interest for segment zero and segment one each,
@@ -419,17 +420,18 @@
                         make_shared<ValidatorNull>(),
                         bind(&Fixture::onComplete, this, _1),
                         bind(&Fixture::onError, this, _1));
-  advanceClocks(time::milliseconds(1), 10);
+  advanceClocks(time::milliseconds(10));
   nackLastInterest(lp::NackReason::DUPLICATE);
   face.receive(*makeDataSegment("/hello/world", 0, true));
 
   BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
   BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/"));
   BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/"));
-  BOOST_REQUIRE_EQUAL(nDatas, 1);
+  BOOST_REQUIRE_EQUAL(nData, 1);
 }
 
-BOOST_AUTO_TEST_SUITE_END()
+BOOST_AUTO_TEST_SUITE_END() // TestSegmentFetcher
+BOOST_AUTO_TEST_SUITE_END() // Util
 
 } // namespace tests
 } // namespace util