blob: b9ed2ffe193f99fa2bfc58727afbbdf07a1beb7c [file] [log] [blame]
Junxiao Shi727ed292014-02-19 23:26:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
ashiqopud3ae85d2019-02-17 02:29:55 +00002/*
Alexander Afanasyev4400e422021-02-17 11:17:33 -05003 * Copyright (c) 2014-2021, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shi727ed292014-02-19 23:26:45 -070025
Davide Pesavento97210d52016-10-14 15:45:48 +020026#ifndef NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP
27#define NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP
Junxiao Shi727ed292014-02-19 23:26:45 -070028
Junxiao Shi727ed292014-02-19 23:26:45 -070029#include "fw/strategy.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060030
31#include "tests/daemon/limited-io.hpp"
Junxiao Shi727ed292014-02-19 23:26:45 -070032
33namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080034namespace fw {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035namespace tests {
Junxiao Shi727ed292014-02-19 23:26:45 -070036
Davide Pesavento14e71f02019-03-28 17:35:25 -040037using namespace nfd::tests;
38
39/** \brief Extends strategy S for unit testing.
Junxiao Shi727ed292014-02-19 23:26:45 -070040 *
Davide Pesavento14e71f02019-03-28 17:35:25 -040041 * Actions invoked by S are recorded but not passed to forwarder.
Junxiao Shi890afe92016-12-15 14:34:34 +000042 *
43 * StrategyTester should be registered into the strategy registry prior to use.
44 * \code
45 * // appears in or included by every .cpp MyStrategyTester is used
Davide Pesavento14e71f02019-03-28 17:35:25 -040046 * using MyStrategyTester = StrategyTester<MyStrategy>;
Junxiao Shi890afe92016-12-15 14:34:34 +000047 *
48 * // appears in only one .cpp
49 * NFD_REGISTER_STRATEGY(MyStrategyTester);
50 * \endcode
Junxiao Shi727ed292014-02-19 23:26:45 -070051 */
52template<typename S>
53class StrategyTester : public S
54{
55public:
Davide Pesavento58091582021-03-05 19:02:48 -050056 using S::S;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070057
Junxiao Shi890afe92016-12-15 14:34:34 +000058 static Name
59 getStrategyName()
60 {
61 Name name = S::getStrategyName();
62 if (!name.empty() && name[-1].isVersion()) {
63 // insert "tester" before version component
64 name::Component versionComp = name[-1];
65 name = name.getPrefix(-1);
66 name.append("tester");
67 name.append(versionComp);
68 }
69 else {
70 name.append("tester");
71 }
72 return name;
73 }
74
Davide Pesavento58091582021-03-05 19:02:48 -050075 /** \brief Signal emitted after each action
Junxiao Shi890afe92016-12-15 14:34:34 +000076 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -070077 signal::Signal<StrategyTester<S>> afterAction;
Junxiao Shi727ed292014-02-19 23:26:45 -070078
Junxiao Shi99540072017-01-27 19:57:33 +000079 /** \brief execute f and wait for a number of strategy actions
80 * \note The actions may occur either during f() invocation or afterwards.
81 * \return whether expected number of actions have occurred
82 */
Davide Pesavento14e71f02019-03-28 17:35:25 -040083 template<typename F>
Junxiao Shi99540072017-01-27 19:57:33 +000084 bool
Davide Pesavento14e71f02019-03-28 17:35:25 -040085 waitForAction(F&& f, LimitedIo& limitedIo, int nExpectedActions = 1)
Junxiao Shi99540072017-01-27 19:57:33 +000086 {
87 int nActions = 0;
88
89 signal::ScopedConnection conn = afterAction.connect([&] {
90 limitedIo.afterOp();
91 ++nActions;
92 });
93
Davide Pesavento58091582021-03-05 19:02:48 -050094 std::forward<F>(f)();
Junxiao Shi99540072017-01-27 19:57:33 +000095
96 if (nActions < nExpectedActions) {
Alexander Afanasyev4400e422021-02-17 11:17:33 -050097 // If strategy doesn't forward anything (e.g., decides not to forward an Interest), the number
Davide Pesavento58091582021-03-05 19:02:48 -050098 // of expected actions should be 0; otherwise the test will get stuck.
Davide Pesavento14e71f02019-03-28 17:35:25 -040099 return limitedIo.run(nExpectedActions - nActions, LimitedIo::UNLIMITED_TIME) == LimitedIo::EXCEED_OPS;
Junxiao Shi99540072017-01-27 19:57:33 +0000100 }
101 return nActions == nExpectedActions;
102 }
103
Junxiao Shi727ed292014-02-19 23:26:45 -0700104protected:
Eric Newberry2377ada2020-09-28 22:40:14 -0700105 pit::OutRecord*
Teng Liangebc20f62020-06-23 16:55:20 -0700106 sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& egress,
Junxiao Shic5f651f2016-11-17 22:58:12 +0000107 const Interest& interest) override
Junxiao Shib9420cf2016-08-13 04:38:52 +0000108 {
Teng Liangebc20f62020-06-23 16:55:20 -0700109 sendInterestHistory.push_back({pitEntry->getInterest(), egress.getId(), interest});
Eric Newberry2377ada2020-09-28 22:40:14 -0700110 auto it = pitEntry->insertOrUpdateOutRecord(egress, interest);
111 BOOST_ASSERT(it != pitEntry->out_end());
Junxiao Shib9420cf2016-08-13 04:38:52 +0000112 afterAction();
Eric Newberry2377ada2020-09-28 22:40:14 -0700113 return &*it;
Junxiao Shib9420cf2016-08-13 04:38:52 +0000114 }
Junxiao Shi727ed292014-02-19 23:26:45 -0700115
Junxiao Shi890afe92016-12-15 14:34:34 +0000116 void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000117 rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry) override
118 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000119 rejectPendingInterestHistory.push_back({pitEntry->getInterest()});
Junxiao Shib9420cf2016-08-13 04:38:52 +0000120 afterAction();
121 }
Junxiao Shi727ed292014-02-19 23:26:45 -0700122
Eric Newberry2377ada2020-09-28 22:40:14 -0700123 bool
Teng Liangebc20f62020-06-23 16:55:20 -0700124 sendNack(const shared_ptr<pit::Entry>& pitEntry, Face& egress,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000125 const lp::NackHeader& header) override
126 {
Teng Liangebc20f62020-06-23 16:55:20 -0700127 sendNackHistory.push_back({pitEntry->getInterest(), egress.getId(), header});
128 pitEntry->deleteInRecord(egress);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000129 afterAction();
Eric Newberry2377ada2020-09-28 22:40:14 -0700130 return true;
Junxiao Shib9420cf2016-08-13 04:38:52 +0000131 }
Junxiao Shi727ed292014-02-19 23:26:45 -0700132
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700133public:
134 struct SendInterestArgs
135 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000136 Interest pitInterest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700137 FaceId outFaceId;
Junxiao Shic5f651f2016-11-17 22:58:12 +0000138 Interest interest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700139 };
140 std::vector<SendInterestArgs> sendInterestHistory;
141
142 struct RejectPendingInterestArgs
143 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000144 Interest pitInterest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700145 };
146 std::vector<RejectPendingInterestArgs> rejectPendingInterestHistory;
147
148 struct SendNackArgs
149 {
Junxiao Shi8ff0a862016-08-13 04:50:50 +0000150 Interest pitInterest;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700151 FaceId outFaceId;
152 lp::NackHeader header;
153 };
154 std::vector<SendNackArgs> sendNackHistory;
Junxiao Shi727ed292014-02-19 23:26:45 -0700155};
156
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700157} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800158} // namespace fw
Junxiao Shi727ed292014-02-19 23:26:45 -0700159} // namespace nfd
160
Davide Pesavento97210d52016-10-14 15:45:48 +0200161#endif // NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP