rib: change FIB update mocking method
Previously, FIB update mocking occurs in Rib class. Test coverage
for FibUpdater class is limited to update computation only.
Now, mocking is moved to a subclass of FibUpdater. Test coverage
for FibUpdater class extends to include FIB update queuing.
Previously, mocked FIB update succeeds synchronously.
Now, mocked FIB update completes via io.post, which better
reflects reality that FIB commands are asynchronous.
refs #4731
Change-Id: I563edda5d398eb915f22fecd4b987f32c610d323
diff --git a/daemon/rib/fib-updater.cpp b/daemon/rib/fib-updater.cpp
index 2ee14fd..6e600e4 100644
--- a/daemon/rib/fib-updater.cpp
+++ b/daemon/rib/fib-updater.cpp
@@ -35,8 +35,8 @@
using ndn::nfd::ControlParameters;
-const unsigned int FibUpdater::MAX_NUM_TIMEOUTS = 10;
-const uint32_t FibUpdater::ERROR_FACE_NOT_FOUND = 410;
+constexpr int MAX_NUM_TIMEOUTS = 10;
+constexpr uint32_t ERROR_FACE_NOT_FOUND = 410;
FibUpdater::FibUpdater(Rib& rib, ndn::nfd::Controller& controller)
: m_rib(rib)
diff --git a/daemon/rib/fib-updater.hpp b/daemon/rib/fib-updater.hpp
index a404853..518b9c7 100644
--- a/daemon/rib/fib-updater.hpp
+++ b/daemon/rib/fib-updater.hpp
@@ -48,13 +48,15 @@
};
public:
- typedef std::list<FibUpdate> FibUpdateList;
-
- typedef std::function<void(RibUpdateList inheritedRoutes)> FibUpdateSuccessCallback;
- typedef std::function<void(uint32_t code, const std::string& error)> FibUpdateFailureCallback;
+ using FibUpdateList = std::list<FibUpdate>;
+ using FibUpdateSuccessCallback = std::function<void(RibUpdateList inheritedRoutes)>;
+ using FibUpdateFailureCallback = std::function<void(uint32_t code, const std::string& error)>;
FibUpdater(Rib& rib, ndn::nfd::Controller& controller);
+ VIRTUAL_WITH_TESTS
+ ~FibUpdater() = default;
+
/** \brief computes FibUpdates using the provided RibUpdateBatch and then sends the
* updates to NFD's FIB
*
@@ -66,7 +68,7 @@
const FibUpdateSuccessCallback& onSuccess,
const FibUpdateFailureCallback& onFailure);
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+private:
/** \brief determines the type of action that will be performed on the RIB and calls the
* corresponding computation method
*/
@@ -100,12 +102,13 @@
sendUpdatesForNonBatchFaceId(const FibUpdateSuccessCallback& onSuccess,
const FibUpdateFailureCallback& onFailure);
+PROTECTED_WITH_TESTS_ELSE_PRIVATE:
/** \brief sends a FibAddNextHopCommand to NFD using the parameters supplied by
* the passed update
*
* \param nTimeouts the number of times this FibUpdate has failed due to timeout
*/
- void
+ VIRTUAL_WITH_TESTS void
sendAddNextHopUpdate(const FibUpdate& update,
const FibUpdateSuccessCallback& onSuccess,
const FibUpdateFailureCallback& onFailure,
@@ -116,7 +119,7 @@
*
* \param nTimeouts the number of times this FibUpdate has failed due to timeout
*/
- void
+ VIRTUAL_WITH_TESTS void
sendRemoveNextHopUpdate(const FibUpdate& update,
const FibUpdateSuccessCallback& onSuccess,
const FibUpdateFailureCallback& onFailure,
@@ -133,7 +136,7 @@
void
computeUpdatesForUnregistration(const RibUpdate& update);
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+PROTECTED_WITH_TESTS_ELSE_PRIVATE:
/** \brief callback used by NfdController when a FibAddNextHopCommand or FibRemoveNextHopCommand
* is successful.
*
@@ -239,7 +242,6 @@
void
traverseSubTree(const RibEntry& entry, Rib::RouteSet routesToAdd, Rib::RouteSet routesToRemove);
-private:
/** \brief creates a record of a calculated inherited route that should be added to the entry
*/
void
@@ -263,10 +265,6 @@
* passed to the RIB when updates are completed successfully
*/
RibUpdateList m_inheritedRoutes;
-
-private:
- static const unsigned int MAX_NUM_TIMEOUTS;
- static const uint32_t ERROR_FACE_NOT_FOUND;
};
} // namespace rib
diff --git a/daemon/rib/rib.cpp b/daemon/rib/rib.cpp
index 0f099d6..2d1db7f 100644
--- a/daemon/rib/rib.cpp
+++ b/daemon/rib/rib.cpp
@@ -45,12 +45,6 @@
return lhs.faceId < rhs.faceId;
}
-Rib::Rib()
- : m_nItems(0)
- , m_isUpdateInProgress(false)
-{
-}
-
void
Rib::setFibUpdater(FibUpdater* updater)
{
@@ -435,23 +429,6 @@
auto fibSuccessCb = bind(&Rib::onFibUpdateSuccess, this, batch, _1, item.managerSuccessCallback);
auto fibFailureCb = bind(&Rib::onFibUpdateFailure, this, item.managerFailureCallback, _1, _2);
-#ifdef WITH_TESTS
- if (mockFibResponse != nullptr) {
- m_fibUpdater->computeAndSendFibUpdates(batch, bind([]{}), bind([]{}));
- bool shouldFibSucceed = mockFibResponse(batch);
- if (wantMockFibResponseOnce) {
- mockFibResponse = nullptr;
- }
- if (shouldFibSucceed) {
- fibSuccessCb(m_fibUpdater->m_inheritedRoutes);
- }
- else {
- fibFailureCb(504, "mocked failure");
- }
- return;
- }
-#endif
-
m_fibUpdater->computeAndSendFibUpdates(batch, fibSuccessCb, fibFailureCb);
}
diff --git a/daemon/rib/rib.hpp b/daemon/rib/rib.hpp
index 529350c..010ea89 100644
--- a/daemon/rib/rib.hpp
+++ b/daemon/rib/rib.hpp
@@ -59,13 +59,9 @@
class Rib : noncopyable
{
public:
- typedef std::list<shared_ptr<RibEntry>> RibEntryList;
- typedef std::map<Name, shared_ptr<RibEntry>> RibTable;
- typedef RibTable::const_iterator const_iterator;
- typedef bool (*RouteComparePredicate)(const Route&, const Route&);
- typedef std::set<Route, RouteComparePredicate> RouteSet;
-
- Rib();
+ using RibEntryList = std::list<shared_ptr<RibEntry>>;
+ using RibTable = std::map<Name, shared_ptr<RibEntry>>;
+ using const_iterator = RibTable::const_iterator;
void
setFibUpdater(FibUpdater* updater);
@@ -80,16 +76,28 @@
findLongestPrefix(const Name& prefix, const Route& route) const;
const_iterator
- begin() const;
+ begin() const
+ {
+ return m_rib.begin();
+ }
const_iterator
- end() const;
+ end() const
+ {
+ return m_rib.end();
+ }
size_t
- size() const;
+ size() const
+ {
+ return m_nItems;
+ }
bool
- empty() const;
+ empty() const
+ {
+ return m_rib.empty();
+ }
shared_ptr<RibEntry>
findParent(const Name& prefix) const;
@@ -153,21 +161,13 @@
uint32_t code, const std::string& error);
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-#ifdef WITH_TESTS
- /** \brief In unit tests, mock FIB update result.
- *
- * If the callback is not nullptr, sendBatchFromQueue() immediately succeeds or fails according
- * to the return value of callback function.
- */
- std::function<bool(const RibUpdateBatch&)> mockFibResponse;
-
- bool wantMockFibResponseOnce = false; ///< if true, mockFibResponse is cleared after every use.
-#endif
-
void
erase(const Name& prefix, const Route& route);
private:
+ using RouteComparePredicate = bool (*)(const Route&, const Route&);
+ using RouteSet = std::set<Route, RouteComparePredicate>;
+
/** \brief find entries under \p prefix
* \pre a RIB entry exists at \p prefix
*/
@@ -214,34 +214,29 @@
* A RIB entry is inserted when the first route associated with a
* certain namespace is added.
*/
- ndn::util::signal::Signal<Rib, Name> afterInsertEntry;
+ signal::Signal<Rib, Name> afterInsertEntry;
/** \brief signals after a RIB entry is erased
*
* A RIB entry is erased when the last route associated with a
* certain namespace is removed.
*/
-
- ndn::util::signal::Signal<Rib, Name> afterEraseEntry;
+ signal::Signal<Rib, Name> afterEraseEntry;
/** \brief signals after a Route is added
*/
- ndn::util::signal::Signal<Rib, RibRouteRef> afterAddRoute;
+ signal::Signal<Rib, RibRouteRef> afterAddRoute;
/** \brief signals before a route is removed
*/
- ndn::util::signal::Signal<Rib, RibRouteRef> beforeRemoveRoute;
+ signal::Signal<Rib, RibRouteRef> beforeRemoveRoute;
private:
RibTable m_rib;
std::multimap<uint64_t, shared_ptr<RibEntry>> m_faceEntries; ///< FaceId => Entry with Route on this face
- FibUpdater* m_fibUpdater;
+ size_t m_nItems = 0;
+ FibUpdater* m_fibUpdater = nullptr;
- size_t m_nItems;
-
- friend class FibUpdater;
-
-private:
struct UpdateQueueItem
{
RibUpdateBatch batch;
@@ -249,38 +244,13 @@
const Rib::UpdateFailureCallback managerFailureCallback;
};
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
- typedef std::list<UpdateQueueItem> UpdateQueue;
+ using UpdateQueue = std::list<UpdateQueueItem>;
UpdateQueue m_updateBatches;
+ bool m_isUpdateInProgress = false;
-private:
- bool m_isUpdateInProgress;
+ friend class FibUpdater;
};
-inline Rib::const_iterator
-Rib::begin() const
-{
- return m_rib.begin();
-}
-
-inline Rib::const_iterator
-Rib::end() const
-{
- return m_rib.end();
-}
-
-inline size_t
-Rib::size() const
-{
- return m_nItems;
-}
-
-inline bool
-Rib::empty() const
-{
- return m_rib.empty();
-}
-
std::ostream&
operator<<(std::ostream& os, const Rib& rib);