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