chunks: fix memory error in Consumer::run

Change-Id: Ic137b88d4ab46ade788d2ae5c0aba59902167f7b
Refs: #3636
diff --git a/tests/chunks/consumer.t.cpp b/tests/chunks/consumer.t.cpp
index 0ce7290..158148a 100644
--- a/tests/chunks/consumer.t.cpp
+++ b/tests/chunks/consumer.t.cpp
@@ -24,6 +24,8 @@
  */
 
 #include "tools/chunks/catchunks/consumer.hpp"
+#include "tools/chunks/catchunks/discover-version.hpp"
+#include "tools/chunks/catchunks/pipeline-interests.hpp"
 
 #include "tests/test-common.hpp"
 #include <ndn-cxx/util/dummy-client-face.hpp>
@@ -65,7 +67,7 @@
   util::DummyClientFace face;
   ValidatorNull validator;
   output_test_stream output("");
-  Consumer cons(face, validator, false, output);
+  Consumer cons(validator, false, output);
 
   auto interest = makeInterest(name);
 
@@ -105,7 +107,7 @@
   util::DummyClientFace face;
   ValidatorNull validator;
   output_test_stream output("");
-  Consumer cons(face, validator, false, output);
+  Consumer cons(validator, false, output);
 
   auto interest = makeInterest(name);
   std::vector<shared_ptr<Data>> dataStore;
@@ -134,6 +136,90 @@
   BOOST_CHECK(output.is_equal(testStrings[2]));
 }
 
+class DiscoverVersionDummy : public DiscoverVersion
+{
+public:
+  DiscoverVersionDummy(const Name& prefix, Face& face, const Options& options)
+    : DiscoverVersion(prefix, face, options)
+    , isDiscoverRunning(false)
+    , m_prefix(prefix)
+  {
+  }
+
+  void
+  run() final
+  {
+    isDiscoverRunning = true;
+
+    auto interest = makeInterest(m_prefix);
+    expressInterest(*interest, 1, 1);
+  }
+
+private:
+  void
+  handleData(const Interest& interest, const Data& data) final
+  {
+    this->emitSignal(onDiscoverySuccess, data);
+  }
+
+public:
+  bool isDiscoverRunning;
+
+private:
+  Name m_prefix;
+};
+
+class PipelineInterestsDummy : public PipelineInterests
+{
+public:
+  PipelineInterestsDummy(Face& face)
+    : PipelineInterests(face)
+    , isPipelineRunning(false)
+  {
+  }
+
+private:
+  void
+  doRun() final
+  {
+    isPipelineRunning = true;
+  }
+
+  void
+  doCancel() final
+  {
+  }
+
+public:
+  bool isPipelineRunning;
+};
+
+BOOST_FIXTURE_TEST_CASE(RunBasic, UnitTestTimeFixture)
+{
+  boost::asio::io_service io;
+  util::DummyClientFace face(io);
+  ValidatorNull validator;
+  Consumer consumer(validator, false);
+
+  Name prefix("/ndn/chunks/test");
+  auto discover = make_unique<DiscoverVersionDummy>(prefix, face, Options());
+  auto pipeline = make_unique<PipelineInterestsDummy>(face);
+  auto discoverPtr = discover.get();
+  auto pipelinePtr = pipeline.get();
+
+  consumer.run(std::move(discover), std::move(pipeline));
+  BOOST_CHECK_EQUAL(discoverPtr->isDiscoverRunning, true);
+
+  this->advanceClocks(io, time::nanoseconds(1));
+  BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
+
+  auto data = makeData(prefix.appendSegment(0));
+  face.receive(*data);
+
+  this->advanceClocks(io, time::nanoseconds(1));
+  BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, true);
+}
+
 BOOST_AUTO_TEST_SUITE_END() // TestConsumer
 BOOST_AUTO_TEST_SUITE_END() // Chunks
 
diff --git a/tools/chunks/catchunks/consumer.cpp b/tools/chunks/catchunks/consumer.cpp
index fe30f75..6ecd630 100644
--- a/tools/chunks/catchunks/consumer.cpp
+++ b/tools/chunks/catchunks/consumer.cpp
@@ -30,9 +30,8 @@
 namespace ndn {
 namespace chunks {
 
-Consumer::Consumer(Face& face, Validator& validator, bool isVerbose, std::ostream& os)
-  : m_face(face)
-  , m_validator(validator)
+Consumer::Consumer(Validator& validator, bool isVerbose, std::ostream& os)
+  : m_validator(validator)
   , m_outputStream(os)
   , m_nextToPrint(0)
   , m_isVerbose(isVerbose)
@@ -47,11 +46,9 @@
   m_nextToPrint = 0;
   m_bufferedData.clear();
 
-  discover->onDiscoverySuccess.connect(bind(&Consumer::startPipeline, this, _1));
-  discover->onDiscoveryFailure.connect(bind(&Consumer::onFailure, this, _1));
-  discover->run();
-
-  m_face.processEvents();
+  m_discover->onDiscoverySuccess.connect(bind(&Consumer::startPipeline, this, _1));
+  m_discover->onDiscoveryFailure.connect(bind(&Consumer::onFailure, this, _1));
+  m_discover->run();
 }
 
 void
diff --git a/tools/chunks/catchunks/consumer.hpp b/tools/chunks/catchunks/consumer.hpp
index e2451be..9996c14 100644
--- a/tools/chunks/catchunks/consumer.hpp
+++ b/tools/chunks/catchunks/consumer.hpp
@@ -59,7 +59,7 @@
   /**
    * @brief Create the consumer
    */
-  Consumer(Face& face, Validator& validator, bool isVerbose, std::ostream& os = std::cout);
+  Consumer(Validator& validator, bool isVerbose, std::ostream& os = std::cout);
 
   /**
    * @brief Run the consumer
@@ -85,7 +85,6 @@
   writeInOrderData();
 
 private:
-  Face& m_face;
   Validator& m_validator;
   std::ostream& m_outputStream;
   unique_ptr<DiscoverVersion> m_discover;
diff --git a/tools/chunks/catchunks/ndncatchunks.cpp b/tools/chunks/catchunks/ndncatchunks.cpp
index 98839b3..c877eaa 100644
--- a/tools/chunks/catchunks/ndncatchunks.cpp
+++ b/tools/chunks/catchunks/ndncatchunks.cpp
@@ -164,11 +164,12 @@
     }
 
     ValidatorNull validator;
-    Consumer consumer(face, validator, options.isVerbose);
+    Consumer consumer(validator, options.isVerbose);
 
     BOOST_ASSERT(discover != nullptr);
     BOOST_ASSERT(pipeline != nullptr);
     consumer.run(std::move(discover), std::move(pipeline));
+    face.processEvents();
   }
   catch (const Consumer::ApplicationNackError& e) {
     std::cerr << "ERROR: " << e.what() << std::endl;