PSync: use interest lifetime as initialRto in SegmentFetcher

refs: #4945

Change-Id: If9e3d268da3dbffda3f4a7d67039413954628d2b
diff --git a/PSync/consumer.cpp b/PSync/consumer.cpp
index 24483a7..ce4a65d 100644
--- a/PSync/consumer.cpp
+++ b/PSync/consumer.cpp
@@ -68,6 +68,8 @@
 void
 Consumer::stop()
 {
+  m_scheduler.cancelAllEvents();
+
   if (m_syncFetcher) {
     m_syncFetcher->stop();
     m_syncFetcher.reset();
@@ -93,6 +95,7 @@
   SegmentFetcher::Options options;
   options.interestLifetime = m_helloInterestLifetime;
   options.maxTimeout = m_helloInterestLifetime;
+  options.rttOptions.initialRto = m_syncInterestLifetime;
 
   m_helloFetcher = SegmentFetcher::start(m_face, helloInterest,
                                          ndn::security::v2::getAcceptAllValidator(), options);
@@ -179,6 +182,7 @@
   SegmentFetcher::Options options;
   options.interestLifetime = m_syncInterestLifetime;
   options.maxTimeout = m_syncInterestLifetime;;
+  options.rttOptions.initialRto = m_syncInterestLifetime;
 
   m_syncFetcher = SegmentFetcher::start(m_face, syncInterest,
                                         ndn::security::v2::getAcceptAllValidator(), options);
diff --git a/PSync/full-producer.cpp b/PSync/full-producer.cpp
index a7a0b24..c31504c 100644
--- a/PSync/full-producer.cpp
+++ b/PSync/full-producer.cpp
@@ -107,6 +107,7 @@
   SegmentFetcher::Options options;
   options.interestLifetime = m_syncInterestLifetime;
   options.maxTimeout = m_syncInterestLifetime;
+  options.rttOptions.initialRto = m_syncInterestLifetime;
 
   m_fetcher = SegmentFetcher::start(m_face, syncInterest,
                                     ndn::security::v2::getAcceptAllValidator(), options);
@@ -341,14 +342,10 @@
 void
 FullProducer::deletePendingInterests(const ndn::Name& interestName)
 {
-  for (auto it = m_pendingEntries.begin(); it != m_pendingEntries.end();) {
-    if (it->first == interestName) {
-      NDN_LOG_TRACE("Delete pending interest: " << interestName);
-      it = m_pendingEntries.erase(it);
-    }
-    else {
-      ++it;
-    }
+  auto it = m_pendingEntries.find(interestName);
+  if (it != m_pendingEntries.end()) {
+    NDN_LOG_TRACE("Delete pending interest: " << interestName);
+    it = m_pendingEntries.erase(it);
   }
 }
 
diff --git a/PSync/full-producer.hpp b/PSync/full-producer.hpp
index f51ea7d..82b7be6 100644
--- a/PSync/full-producer.hpp
+++ b/PSync/full-producer.hpp
@@ -97,7 +97,7 @@
   void
   publishName(const ndn::Name& prefix, ndn::optional<uint64_t> seq = ndn::nullopt);
 
-private:
+PSYNC_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   /**
    * @brief Send sync interest for full synchronization
    *
@@ -108,7 +108,6 @@
   void
   sendSyncInterest();
 
-PSYNC_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   /**
    * @brief Process sync interest from other parties
    *
diff --git a/tests/test-consumer.cpp b/tests/test-consumer.cpp
index d1e6115..67b524c 100644
--- a/tests/test-consumer.cpp
+++ b/tests/test-consumer.cpp
@@ -18,6 +18,7 @@
  **/
 
 #include "PSync/consumer.hpp"
+#include "unit-test-time-fixture.hpp"
 
 #include <boost/test/unit_test.hpp>
 #include <ndn-cxx/name.hpp>
@@ -56,6 +57,29 @@
   BOOST_CHECK(!consumer.addSubscription(subscription));
 }
 
+BOOST_FIXTURE_TEST_CASE(ConstantTimeoutForFirstSegment, ndn::tests::UnitTestTimeFixture)
+{
+  util::DummyClientFace face(io, {true, true});
+  Consumer consumer(Name("/psync"), face,
+                    [] (const vector<Name>&) {},
+                    [] (const vector<MissingDataInfo>&) {},
+                    40, 0.001,
+                    ndn::time::milliseconds(4000),
+                    ndn::time::milliseconds(4000));
+
+  consumer.sendHelloInterest();
+  advanceClocks(ndn::time::milliseconds(4000));
+  BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
+  face.sentInterests.clear();
+  consumer.stop();
+
+  consumer.m_iblt = ndn::Name("test");
+  consumer.sendSyncInterest();
+  advanceClocks(ndn::time::milliseconds(4000));
+  BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
+  consumer.stop();
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace psync
\ No newline at end of file
diff --git a/tests/test-full-producer.cpp b/tests/test-full-producer.cpp
index 8190446..2bd7db4 100644
--- a/tests/test-full-producer.cpp
+++ b/tests/test-full-producer.cpp
@@ -18,6 +18,7 @@
  **/
 
 #include "PSync/full-producer.hpp"
+#include "unit-test-time-fixture.hpp"
 
 #include <boost/test/unit_test.hpp>
 #include <ndn-cxx/name.hpp>
@@ -50,6 +51,20 @@
   BOOST_REQUIRE_NO_THROW(node.onSyncInterest(syncPrefix, Interest(syncInterestName)));
 }
 
+BOOST_FIXTURE_TEST_CASE(ConstantTimeoutForFirstSegment, ndn::tests::UnitTestTimeFixture)
+{
+  Name syncPrefix("/psync"), userNode("/testUser");
+  util::DummyClientFace face(io, {true, true});
+
+  FullProducer node(40, face, syncPrefix, userNode, nullptr, ndn::time::milliseconds(8000));
+  advanceClocks(ndn::time::milliseconds(10));
+  face.sentInterests.clear();
+
+  // full sync sends the next one in interest lifetime / 2 +- jitter
+  advanceClocks(ndn::time::milliseconds(6000));
+  BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace psync
\ No newline at end of file