Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 26 | #ifndef NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP |
| 27 | #define NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 29 | #include "fw/strategy.hpp" |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 30 | |
| 31 | #include "tests/daemon/limited-io.hpp" |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 34 | namespace fw { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | namespace tests { |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 37 | using namespace nfd::tests; |
| 38 | |
| 39 | /** \brief Extends strategy S for unit testing. |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 40 | * |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 41 | * Actions invoked by S are recorded but not passed to forwarder. |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 42 | * |
| 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 Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 46 | * using MyStrategyTester = StrategyTester<MyStrategy>; |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 47 | * |
| 48 | * // appears in only one .cpp |
| 49 | * NFD_REGISTER_STRATEGY(MyStrategyTester); |
| 50 | * \endcode |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 51 | */ |
| 52 | template<typename S> |
| 53 | class StrategyTester : public S |
| 54 | { |
| 55 | public: |
| 56 | explicit |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 57 | StrategyTester(Forwarder& forwarder, const Name& name = getStrategyName()) |
| 58 | : S(forwarder, name) |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 59 | { |
| 60 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 62 | static Name |
| 63 | getStrategyName() |
| 64 | { |
| 65 | Name name = S::getStrategyName(); |
| 66 | if (!name.empty() && name[-1].isVersion()) { |
| 67 | // insert "tester" before version component |
| 68 | name::Component versionComp = name[-1]; |
| 69 | name = name.getPrefix(-1); |
| 70 | name.append("tester"); |
| 71 | name.append(versionComp); |
| 72 | } |
| 73 | else { |
| 74 | name.append("tester"); |
| 75 | } |
| 76 | return name; |
| 77 | } |
| 78 | |
| 79 | /** \brief signal emitted after each Action |
| 80 | */ |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 81 | signal::Signal<StrategyTester<S>> afterAction; |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 82 | |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 83 | /** \brief execute f and wait for a number of strategy actions |
| 84 | * \note The actions may occur either during f() invocation or afterwards. |
| 85 | * \return whether expected number of actions have occurred |
| 86 | */ |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 87 | template<typename F> |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 88 | bool |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 89 | waitForAction(F&& f, LimitedIo& limitedIo, int nExpectedActions = 1) |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 90 | { |
| 91 | int nActions = 0; |
| 92 | |
| 93 | signal::ScopedConnection conn = afterAction.connect([&] { |
| 94 | limitedIo.afterOp(); |
| 95 | ++nActions; |
| 96 | }); |
| 97 | |
| 98 | f(); |
| 99 | |
| 100 | if (nActions < nExpectedActions) { |
| 101 | // A correctly implemented strategy is required to invoke reject pending Interest action if it |
| 102 | // decides to not forward an Interest. If a test case is stuck in the call below, check that |
| 103 | // rejectPendingInterest is invoked under proper condition. |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 104 | return limitedIo.run(nExpectedActions - nActions, LimitedIo::UNLIMITED_TIME) == LimitedIo::EXCEED_OPS; |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 105 | } |
| 106 | return nActions == nExpectedActions; |
| 107 | } |
| 108 | |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 109 | protected: |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 110 | void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 111 | sendInterest(const shared_ptr<pit::Entry>& pitEntry, const FaceEndpoint& egress, |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 112 | const Interest& interest) override |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 113 | { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 114 | sendInterestHistory.push_back({pitEntry->getInterest(), egress.face.getId(), interest}); |
| 115 | pitEntry->insertOrUpdateOutRecord(egress.face, egress.endpoint, interest); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 116 | afterAction(); |
| 117 | } |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 119 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 120 | rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry) override |
| 121 | { |
Junxiao Shi | 8ff0a86 | 2016-08-13 04:50:50 +0000 | [diff] [blame] | 122 | rejectPendingInterestHistory.push_back({pitEntry->getInterest()}); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 123 | afterAction(); |
| 124 | } |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 125 | |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 126 | void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 127 | sendNack(const shared_ptr<pit::Entry>& pitEntry, const FaceEndpoint& egress, |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 128 | const lp::NackHeader& header) override |
| 129 | { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 130 | sendNackHistory.push_back({pitEntry->getInterest(), egress.face.getId(), header}); |
| 131 | pitEntry->deleteInRecord(egress.face, egress.endpoint); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 132 | afterAction(); |
| 133 | } |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 134 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 135 | public: |
| 136 | struct SendInterestArgs |
| 137 | { |
Junxiao Shi | 8ff0a86 | 2016-08-13 04:50:50 +0000 | [diff] [blame] | 138 | Interest pitInterest; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 139 | FaceId outFaceId; |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 140 | Interest interest; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 141 | }; |
| 142 | std::vector<SendInterestArgs> sendInterestHistory; |
| 143 | |
| 144 | struct RejectPendingInterestArgs |
| 145 | { |
Junxiao Shi | 8ff0a86 | 2016-08-13 04:50:50 +0000 | [diff] [blame] | 146 | Interest pitInterest; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 147 | }; |
| 148 | std::vector<RejectPendingInterestArgs> rejectPendingInterestHistory; |
| 149 | |
| 150 | struct SendNackArgs |
| 151 | { |
Junxiao Shi | 8ff0a86 | 2016-08-13 04:50:50 +0000 | [diff] [blame] | 152 | Interest pitInterest; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 153 | FaceId outFaceId; |
| 154 | lp::NackHeader header; |
| 155 | }; |
| 156 | std::vector<SendNackArgs> sendNackHistory; |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 159 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 160 | } // namespace fw |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 161 | } // namespace nfd |
| 162 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 163 | #endif // NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP |