face: Fix bug with removePendingInterest

Change-Id: I364c1441da2cc0042ba461bc3a6cb4b70e53b166
Refs: #1917
diff --git a/src/detail/pending-interest.hpp b/src/detail/pending-interest.hpp
index 791f59b..0a66dca 100644
--- a/src/detail/pending-interest.hpp
+++ b/src/detail/pending-interest.hpp
@@ -57,13 +57,13 @@
   }
 
   const shared_ptr<const Interest>&
-  getInterest()
+  getInterest() const
   {
     return m_interest;
   }
 
   const OnData&
-  getOnData()
+  getOnData() const
   {
     return m_onData;
   }
@@ -73,7 +73,7 @@
    * @return true if this interest timed out, otherwise false.
    */
   bool
-  isTimedOut(const time::steady_clock::TimePoint& now)
+  isTimedOut(const time::steady_clock::TimePoint& now) const
   {
     return now >= m_timeout;
   }
@@ -82,7 +82,7 @@
    * Call m_onTimeout (if defined).  This ignores exceptions from the m_onTimeout.
    */
   void
-  callTimeout()
+  callTimeout() const
   {
     if (m_onTimeout) {
       m_onTimeout(*m_interest);
@@ -114,7 +114,8 @@
   bool
   operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
   {
-    return (reinterpret_cast<const PendingInterestId*>(pendingInterest.get()) == m_id);
+    return (reinterpret_cast<const PendingInterestId*>(
+              pendingInterest->getInterest().get()) == m_id);
   }
 private:
   const PendingInterestId* m_id;
diff --git a/src/face.cpp b/src/face.cpp
index 79d8dc1..42fc522 100644
--- a/src/face.cpp
+++ b/src/face.cpp
@@ -193,6 +193,12 @@
   m_ioService->post(bind(&Impl::asyncRemovePendingInterest, m_impl, pendingInterestId));
 }
 
+size_t
+Face::getNPendingInterests() const
+{
+  return m_impl->m_pendingInterestTable.size();
+}
+
 const RegisteredPrefixId*
 Face::setInterestFilter(const InterestFilter& interestFilter,
                         const OnInterest& onInterest,
diff --git a/src/face.hpp b/src/face.hpp
index 974aa69..3407d92 100644
--- a/src/face.hpp
+++ b/src/face.hpp
@@ -225,6 +225,12 @@
   removePendingInterest(const PendingInterestId* pendingInterestId);
 
   /**
+   * @brief Get number of pending Interests
+   */
+  size_t
+  getNPendingInterests() const;
+
+  /**
    * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
    * callback and register the filtered prefix with the connected NDN forwarder
    *
diff --git a/tests/unit-tests/face.cpp b/tests/unit-tests/face.cpp
new file mode 100644
index 0000000..dcbff0b
--- /dev/null
+++ b/tests/unit-tests/face.cpp
@@ -0,0 +1,95 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "face.hpp"
+#include "security/key-chain.hpp"
+#include "dummy-client-face.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(TestsFace)
+
+class Fixture
+{
+public:
+  Fixture()
+    : face(makeDummyClientFace())
+    , nData(0)
+    , nTimeouts(0)
+  {
+  }
+
+  void
+  onData()
+  {
+    ++nData;
+  }
+
+  void
+  onTimeout()
+  {
+    ++nTimeouts;
+  }
+
+public:
+  shared_ptr<DummyClientFace> face;
+  uint32_t nData;
+  uint32_t nTimeouts;
+};
+
+BOOST_FIXTURE_TEST_CASE(ExpressInterest, Fixture)
+{
+  face->expressInterest(Interest("/test/interest", time::seconds(10)),
+                        bind(&Fixture::onData, this),
+                        bind(&Fixture::onTimeout, this));
+  face->processEvents(time::milliseconds(100));
+
+  BOOST_CHECK_EQUAL(face->m_sentInterests.size(), 1);
+  BOOST_CHECK_EQUAL(face->m_sentDatas.size(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(RemovePendingInterest, Fixture)
+{
+  const PendingInterestId* interestId =
+    face->expressInterest(Interest("/test/interest", time::seconds(10)),
+                          bind(&Fixture::onData, this),
+                          bind(&Fixture::onTimeout, this));
+
+  shared_ptr<Data> data = make_shared<Data>("/test/interest/replied");
+  const uint8_t buffer[] = "Hello, world!";
+  data->setContent(buffer, sizeof(buffer));
+  KeyChain keyChain;
+  keyChain.sign(*data);
+  face->receive(*data);
+
+  face->removePendingInterest(interestId);
+  face->processEvents(time::milliseconds(100));
+  BOOST_CHECK_EQUAL(nData, 0);
+  BOOST_CHECK_EQUAL(nTimeouts, 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace ndn