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