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 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 34 | |
| 35 | /** \brief Extends strategy S for unit testing. |
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 | * Actions invoked by S are recorded but not passed to forwarder. |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 38 | * |
| 39 | * StrategyTester should be registered into the strategy registry prior to use. |
| 40 | * \code |
| 41 | * // appears in or included by every .cpp MyStrategyTester is used |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 42 | * using MyStrategyTester = StrategyTester<MyStrategy>; |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 43 | * |
| 44 | * // appears in only one .cpp |
| 45 | * NFD_REGISTER_STRATEGY(MyStrategyTester); |
| 46 | * \endcode |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 47 | */ |
| 48 | template<typename S> |
| 49 | class StrategyTester : public S |
| 50 | { |
| 51 | public: |
Davide Pesavento | 5809158 | 2021-03-05 19:02:48 -0500 | [diff] [blame] | 52 | using S::S; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 53 | |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 54 | static Name |
| 55 | getStrategyName() |
| 56 | { |
| 57 | Name name = S::getStrategyName(); |
| 58 | if (!name.empty() && name[-1].isVersion()) { |
| 59 | // insert "tester" before version component |
| 60 | name::Component versionComp = name[-1]; |
| 61 | name = name.getPrefix(-1); |
| 62 | name.append("tester"); |
| 63 | name.append(versionComp); |
| 64 | } |
| 65 | else { |
| 66 | name.append("tester"); |
| 67 | } |
| 68 | return name; |
| 69 | } |
| 70 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 71 | /** \brief Signal emitted after each action. |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 72 | */ |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 73 | signal::Signal<StrategyTester<S>> afterAction; |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 74 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 75 | /** \brief Execute f and wait for a number of strategy actions. |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 76 | * \note The actions may occur either during f() invocation or afterwards. |
| 77 | * \return whether expected number of actions have occurred |
| 78 | */ |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 79 | template<typename F> |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 80 | bool |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 81 | waitForAction(F&& f, LimitedIo& limitedIo, int nExpectedActions = 1) |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 82 | { |
| 83 | int nActions = 0; |
| 84 | |
| 85 | signal::ScopedConnection conn = afterAction.connect([&] { |
| 86 | limitedIo.afterOp(); |
| 87 | ++nActions; |
| 88 | }); |
| 89 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 90 | std::invoke(std::forward<F>(f)); |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 91 | |
| 92 | if (nActions < nExpectedActions) { |
Alexander Afanasyev | 4400e42 | 2021-02-17 11:17:33 -0500 | [diff] [blame] | 93 | // If strategy doesn't forward anything (e.g., decides not to forward an Interest), the number |
Davide Pesavento | 5809158 | 2021-03-05 19:02:48 -0500 | [diff] [blame] | 94 | // of expected actions should be 0; otherwise the test will get stuck. |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 95 | return limitedIo.run(nExpectedActions - nActions, LimitedIo::UNLIMITED_TIME) == LimitedIo::EXCEED_OPS; |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 96 | } |
| 97 | return nActions == nExpectedActions; |
| 98 | } |
| 99 | |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 100 | protected: |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 101 | pit::OutRecord* |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 102 | sendInterest(const Interest& interest, Face& egress, |
| 103 | const shared_ptr<pit::Entry>& pitEntry) override |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 104 | { |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 105 | sendInterestHistory.push_back({pitEntry->getInterest(), egress.getId(), interest}); |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 106 | auto it = pitEntry->insertOrUpdateOutRecord(egress, interest); |
| 107 | BOOST_ASSERT(it != pitEntry->out_end()); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 108 | afterAction(); |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 109 | return &*it; |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 110 | } |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame] | 112 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 113 | rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry) override |
| 114 | { |
Junxiao Shi | 8ff0a86 | 2016-08-13 04:50:50 +0000 | [diff] [blame] | 115 | rejectPendingInterestHistory.push_back({pitEntry->getInterest()}); |
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 | |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 119 | bool |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 120 | sendNack(const lp::NackHeader& header, Face& egress, |
| 121 | const shared_ptr<pit::Entry>& pitEntry) override |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 122 | { |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 123 | sendNackHistory.push_back({pitEntry->getInterest(), egress.getId(), header}); |
| 124 | pitEntry->deleteInRecord(egress); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 125 | afterAction(); |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 126 | return true; |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 127 | } |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 128 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 129 | public: |
| 130 | struct SendInterestArgs |
| 131 | { |
Junxiao Shi | 8ff0a86 | 2016-08-13 04:50:50 +0000 | [diff] [blame] | 132 | Interest pitInterest; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 133 | FaceId outFaceId; |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 134 | Interest interest; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 135 | }; |
| 136 | std::vector<SendInterestArgs> sendInterestHistory; |
| 137 | |
| 138 | struct RejectPendingInterestArgs |
| 139 | { |
Junxiao Shi | 8ff0a86 | 2016-08-13 04:50:50 +0000 | [diff] [blame] | 140 | Interest pitInterest; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 141 | }; |
| 142 | std::vector<RejectPendingInterestArgs> rejectPendingInterestHistory; |
| 143 | |
| 144 | struct SendNackArgs |
| 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 | FaceId outFaceId; |
| 148 | lp::NackHeader header; |
| 149 | }; |
| 150 | std::vector<SendNackArgs> sendNackHistory; |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 153 | } // namespace nfd::tests |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 154 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 155 | #endif // NFD_TESTS_DAEMON_FW_STRATEGY_TESTER_HPP |