Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 4e837f8 | 2017-10-02 22:14:43 +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 | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/strategy-choice.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "tests/daemon/global-io-fixture.hpp" |
| 30 | #include "tests/daemon/fw/dummy-strategy.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tests { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 34 | class StrategyChoiceFixture : public GlobalIoFixture |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 35 | { |
| 36 | protected: |
| 37 | StrategyChoiceFixture() |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 38 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 39 | DummyStrategy::registerAs(strategyNameP); |
| 40 | DummyStrategy::registerAs(strategyNameQ); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 43 | /** \brief Insert StrategyChoice entry at \p prefix for \p instanceName. |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 44 | * \return constructed instance name |
| 45 | */ |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 46 | Name |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 47 | insertAndGet(const Name& prefix, const Name& instanceName) |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 48 | { |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 49 | BOOST_REQUIRE(sc.insert(prefix, instanceName)); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 50 | auto [isFound, foundName] = sc.get(prefix); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 51 | BOOST_REQUIRE(isFound); |
| 52 | return foundName; |
| 53 | } |
| 54 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 55 | /** \brief Determine whether the effective strategy type at \p prefix is \p S. |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 56 | * \tparam S expected strategy type |
| 57 | */ |
| 58 | template<typename S> |
| 59 | bool |
| 60 | isStrategyType(const Name& prefix) |
| 61 | { |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 62 | auto& effectiveStrategy = sc.findEffectiveStrategy(prefix); |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 63 | return dynamic_cast<S*>(&effectiveStrategy) != nullptr; |
| 64 | } |
| 65 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 66 | template<typename Q> |
| 67 | Name |
| 68 | findInstanceName(const Q& query) |
| 69 | { |
| 70 | return sc.findEffectiveStrategy(query).getInstanceName(); |
| 71 | } |
| 72 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 73 | protected: |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 74 | FaceTable faceTable; |
| 75 | Forwarder forwarder{faceTable}; |
| 76 | StrategyChoice& sc{forwarder.getStrategyChoice()}; |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 77 | |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 78 | const Name strategyNameP = Name("/strategy-choice-P").appendVersion(0); |
| 79 | const Name strategyNameQ = Name("/strategy-choice-Q").appendVersion(0); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 82 | BOOST_AUTO_TEST_SUITE(Table) |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 83 | BOOST_FIXTURE_TEST_SUITE(TestStrategyChoice, StrategyChoiceFixture) |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 84 | |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 85 | BOOST_AUTO_TEST_CASE(Versioning) |
| 86 | { |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 87 | const auto strategyNameV = Name("/strategy-choice-V"); |
| 88 | const auto strategyNameV0 = Name("/strategy-choice-V").appendVersion(0); |
| 89 | const auto strategyNameV1 = Name("/strategy-choice-V").appendVersion(1); |
| 90 | const auto strategyNameV2 = Name("/strategy-choice-V").appendVersion(2); |
| 91 | const auto strategyNameV3 = Name("/strategy-choice-V").appendVersion(3); |
| 92 | const auto strategyNameV4 = Name("/strategy-choice-V").appendVersion(4); |
| 93 | const auto strategyNameV5 = Name("/strategy-choice-V").appendVersion(5); |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 94 | |
| 95 | VersionedDummyStrategy<1>::registerAs(strategyNameV1); |
| 96 | VersionedDummyStrategy<3>::registerAs(strategyNameV3); |
| 97 | VersionedDummyStrategy<4>::registerAs(strategyNameV4); |
| 98 | |
| 99 | // unversioned: choose latest version |
| 100 | BOOST_CHECK_EQUAL(this->insertAndGet("/A", strategyNameV), strategyNameV4); |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 101 | BOOST_CHECK(this->isStrategyType<VersionedDummyStrategy<4>>("/A")); |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 102 | |
| 103 | // exact version: choose same version |
| 104 | BOOST_CHECK_EQUAL(this->insertAndGet("/B", strategyNameV1), strategyNameV1); |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 105 | BOOST_CHECK(this->isStrategyType<VersionedDummyStrategy<1>>("/B")); |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(this->insertAndGet("/C", strategyNameV3), strategyNameV3); |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 107 | BOOST_CHECK(this->isStrategyType<VersionedDummyStrategy<3>>("/C")); |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(this->insertAndGet("/D", strategyNameV4), strategyNameV4); |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 109 | BOOST_CHECK(this->isStrategyType<VersionedDummyStrategy<4>>("/D")); |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 110 | |
| 111 | // lower version: choose next higher version |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 112 | BOOST_CHECK_EQUAL(this->insertAndGet("/E", strategyNameV0), strategyNameV0); |
| 113 | BOOST_CHECK(this->isStrategyType<VersionedDummyStrategy<1>>("/E")); |
| 114 | BOOST_CHECK_EQUAL(this->insertAndGet("/F", strategyNameV2), strategyNameV2); |
| 115 | BOOST_CHECK(this->isStrategyType<VersionedDummyStrategy<3>>("/F")); |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 116 | |
| 117 | // higher version: failure |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 118 | StrategyChoice::InsertResult res5 = sc.insert("/G", strategyNameV5); |
| 119 | BOOST_CHECK(!res5); |
| 120 | BOOST_CHECK(!res5.isRegistered()); |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(Parameters) |
| 124 | { |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 125 | // no parameters |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(this->insertAndGet("/A", strategyNameP), strategyNameP); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 127 | |
| 128 | // one parameter |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 129 | Name oneParamName = Name(strategyNameP).append("param"); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(this->insertAndGet("/B", oneParamName), oneParamName); |
| 131 | |
| 132 | // two parameters |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 133 | Name twoParamName = Name(strategyNameP).append("x").append("y"); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(this->insertAndGet("/C", twoParamName), twoParamName); |
| 135 | |
| 136 | // parameter without version is disallowed |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 137 | Name oneParamUnversioned = strategyNameP.getPrefix(-1).append("param"); |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 138 | BOOST_CHECK(!sc.insert("/D", oneParamUnversioned)); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Junxiao Shi | 4e837f8 | 2017-10-02 22:14:43 +0000 | [diff] [blame] | 141 | BOOST_AUTO_TEST_CASE(InsertLongName) |
| 142 | { |
| 143 | Name n1; |
| 144 | while (n1.size() < NameTree::getMaxDepth()) { |
| 145 | n1.append("A"); |
| 146 | } |
| 147 | Name n2 = n1; |
| 148 | while (n2.size() < NameTree::getMaxDepth() * 2) { |
| 149 | n2.append("B"); |
| 150 | } |
| 151 | |
| 152 | BOOST_CHECK(sc.insert(n1, strategyNameP)); |
| 153 | BOOST_CHECK(!sc.insert(n2, strategyNameP)); |
| 154 | } |
| 155 | |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 156 | BOOST_AUTO_TEST_CASE(Get) |
| 157 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 158 | BOOST_CHECK(sc.insert("/", strategyNameP)); |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 159 | // { '/'=>P } |
| 160 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 161 | auto getRoot = sc.get("/"); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(getRoot.first, true); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(getRoot.second, strategyNameP); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 164 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 165 | auto getA = sc.get("/A"); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(getA.first, false); |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 167 | } |
| 168 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 169 | BOOST_AUTO_TEST_CASE(FindEffectiveStrategy) |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 170 | { |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 171 | const auto strategyNameZ = Name("/strategy-choice-Z").appendVersion(0); // unregistered strategyName |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 172 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 173 | BOOST_CHECK(sc.insert("/", strategyNameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 174 | // { '/'=>P } |
| 175 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 176 | BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP); |
| 177 | BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP); |
| 178 | BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 179 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 180 | BOOST_CHECK(sc.insert("/A/B", strategyNameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 181 | // { '/'=>P, '/A/B'=>P } |
| 182 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 183 | BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP); |
| 184 | BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP); |
| 185 | BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP); |
| 186 | // same entry, same instance |
| 187 | BOOST_CHECK_EQUAL(&sc.findEffectiveStrategy("/"), &sc.findEffectiveStrategy("/A")); |
| 188 | // different entries, distinct instances |
| 189 | BOOST_CHECK_NE(&sc.findEffectiveStrategy("/"), &sc.findEffectiveStrategy("/A/B")); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 190 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 191 | sc.erase("/A"); // no effect |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 192 | // { '/'=>P, '/A/B'=>P } |
| 193 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 194 | BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP); |
| 195 | BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP); |
| 196 | BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 197 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 198 | BOOST_CHECK(sc.insert("/A", strategyNameQ)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 199 | // { '/'=>P, '/A/B'=>P, '/A'=>Q } |
| 200 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP); |
| 202 | BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameQ); |
| 203 | BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 204 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 205 | sc.erase("/A/B"); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 206 | // { '/'=>P, '/A'=>Q } |
| 207 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 208 | BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP); |
| 209 | BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameQ); |
| 210 | BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameQ); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 211 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 212 | BOOST_CHECK(!sc.insert("/", strategyNameZ)); // non existent strategy |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 213 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 214 | BOOST_CHECK(sc.insert("/", strategyNameQ)); |
| 215 | BOOST_CHECK(sc.insert("/A", strategyNameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 216 | // { '/'=>Q, '/A'=>P } |
| 217 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 218 | BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameQ); |
| 219 | BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP); |
| 220 | BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP); |
| 221 | BOOST_CHECK_EQUAL(this->findInstanceName("/D"), strategyNameQ); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 224 | BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithPitEntry) |
| 225 | { |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 226 | shared_ptr<Data> dataABC = makeData("/A/B/C"); |
| 227 | Name fullName = dataABC->getFullName(); |
| 228 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 229 | BOOST_CHECK(sc.insert("/A", strategyNameP)); |
| 230 | BOOST_CHECK(sc.insert(fullName, strategyNameQ)); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 231 | |
| 232 | Pit& pit = forwarder.getPit(); |
| 233 | shared_ptr<Interest> interestAB = makeInterest("/A/B"); |
| 234 | shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first; |
| 235 | shared_ptr<Interest> interestFull = makeInterest(fullName); |
| 236 | shared_ptr<pit::Entry> pitFull = pit.insert(*interestFull).first; |
| 237 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 238 | BOOST_CHECK_EQUAL(this->findInstanceName(*pitAB), strategyNameP); |
| 239 | BOOST_CHECK_EQUAL(this->findInstanceName(*pitFull), strategyNameQ); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithMeasurementsEntry) |
| 243 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 244 | BOOST_CHECK(sc.insert("/A", strategyNameP)); |
| 245 | BOOST_CHECK(sc.insert("/A/B/C", strategyNameQ)); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 246 | |
| 247 | Measurements& measurements = forwarder.getMeasurements(); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 248 | measurements::Entry& mAB = measurements.get("/A/B"); |
| 249 | measurements::Entry& mABCD = measurements.get("/A/B/C/D"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 250 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 251 | BOOST_CHECK_EQUAL(this->findInstanceName(mAB), strategyNameP); |
| 252 | BOOST_CHECK_EQUAL(this->findInstanceName(mABCD), strategyNameQ); |
| 253 | } |
| 254 | |
| 255 | BOOST_AUTO_TEST_CASE(Erase) |
| 256 | { |
| 257 | NameTree& nameTree = forwarder.getNameTree(); |
| 258 | |
| 259 | sc.insert("/", strategyNameP); |
| 260 | |
| 261 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 262 | |
| 263 | sc.insert("/A/B", strategyNameQ); |
| 264 | sc.erase("/A/B"); |
| 265 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 268 | BOOST_AUTO_TEST_CASE(Enumerate) |
| 269 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 270 | sc.insert("/", strategyNameP); |
| 271 | sc.insert("/A/B", strategyNameQ); |
| 272 | sc.insert("/A/B/C", strategyNameP); |
| 273 | sc.insert("/D", strategyNameP); |
| 274 | sc.insert("/E", strategyNameQ); |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 275 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 276 | BOOST_CHECK_EQUAL(sc.size(), 5); |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 277 | |
| 278 | std::map<Name, Name> map; // namespace=>strategyName |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 279 | for (StrategyChoice::const_iterator it = sc.begin(); it != sc.end(); ++it) { |
Junxiao Shi | 4cb7431 | 2016-12-25 20:48:47 +0000 | [diff] [blame] | 280 | map[it->getPrefix()] = it->getStrategyInstanceName(); |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 281 | } |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 282 | |
| 283 | BOOST_CHECK_EQUAL(map.at("/"), strategyNameP); |
| 284 | BOOST_CHECK_EQUAL(map.at("/A/B"), strategyNameQ); |
| 285 | BOOST_CHECK_EQUAL(map.at("/A/B/C"), strategyNameP); |
| 286 | BOOST_CHECK_EQUAL(map.at("/D"), strategyNameP); |
| 287 | BOOST_CHECK_EQUAL(map.at("/E"), strategyNameQ); |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 288 | BOOST_CHECK_EQUAL(map.size(), 5); |
| 289 | } |
| 290 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 291 | class PStrategyInfo : public fw::StrategyInfo |
| 292 | { |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 293 | public: |
| 294 | static constexpr int |
| 295 | getTypeId() |
| 296 | { |
| 297 | return 10; |
| 298 | } |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | BOOST_AUTO_TEST_CASE(ClearStrategyInfo) |
| 302 | { |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 303 | Measurements& measurements = forwarder.getMeasurements(); |
| 304 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 305 | BOOST_CHECK(sc.insert("/", strategyNameP)); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 306 | // { '/'=>P } |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 307 | measurements.get("/").insertStrategyInfo<PStrategyInfo>(); |
| 308 | measurements.get("/A").insertStrategyInfo<PStrategyInfo>(); |
| 309 | measurements.get("/A/B").insertStrategyInfo<PStrategyInfo>(); |
| 310 | measurements.get("/A/C").insertStrategyInfo<PStrategyInfo>(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 311 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 312 | BOOST_CHECK(sc.insert("/A/B", strategyNameP)); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 313 | // { '/'=>P, '/A/B'=>P } |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 314 | BOOST_CHECK(measurements.get("/").getStrategyInfo<PStrategyInfo>() != nullptr); |
| 315 | BOOST_CHECK(measurements.get("/A").getStrategyInfo<PStrategyInfo>() != nullptr); |
Junxiao Shi | 55e21b9 | 2017-01-23 03:27:47 +0000 | [diff] [blame] | 316 | BOOST_CHECK(measurements.get("/A/B").getStrategyInfo<PStrategyInfo>() != nullptr); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 317 | BOOST_CHECK(measurements.get("/A/C").getStrategyInfo<PStrategyInfo>() != nullptr); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 318 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 319 | BOOST_CHECK(sc.insert("/A", strategyNameQ)); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 320 | // { '/'=>P, '/A/B'=>P, '/A'=>Q } |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 321 | BOOST_CHECK(measurements.get("/").getStrategyInfo<PStrategyInfo>() != nullptr); |
| 322 | BOOST_CHECK(measurements.get("/A").getStrategyInfo<PStrategyInfo>() == nullptr); |
Junxiao Shi | 55e21b9 | 2017-01-23 03:27:47 +0000 | [diff] [blame] | 323 | BOOST_CHECK(measurements.get("/A/B").getStrategyInfo<PStrategyInfo>() != nullptr); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 324 | BOOST_CHECK(measurements.get("/A/C").getStrategyInfo<PStrategyInfo>() == nullptr); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 325 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 326 | sc.erase("/A/B"); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 327 | // { '/'=>P, '/A'=>Q } |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 328 | BOOST_CHECK(measurements.get("/").getStrategyInfo<PStrategyInfo>() != nullptr); |
| 329 | BOOST_CHECK(measurements.get("/A").getStrategyInfo<PStrategyInfo>() == nullptr); |
| 330 | BOOST_CHECK(measurements.get("/A/B").getStrategyInfo<PStrategyInfo>() == nullptr); |
| 331 | BOOST_CHECK(measurements.get("/A/C").getStrategyInfo<PStrategyInfo>() == nullptr); |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 334 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoice |
| 335 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 336 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 337 | } // namespace nfd::tests |