Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 3f9234b | 2017-12-07 17:41:50 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 6 | * University Pierre & Marie Curie, Sorbonne University, |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 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 | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/measurements.hpp" |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 27 | #include "table/fib.hpp" |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 28 | #include "table/pit.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 29 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 30 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 31 | #include "tests/daemon/global-io-fixture.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | using namespace nfd::measurements; |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 36 | |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Table) |
| 38 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 39 | class MeasurementsFixture : public GlobalIoTimeFixture |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | MeasurementsFixture() |
| 43 | : measurements(nameTree) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | public: |
| 48 | NameTree nameTree; |
| 49 | Measurements measurements; |
| 50 | }; |
| 51 | |
| 52 | BOOST_FIXTURE_TEST_SUITE(TestMeasurements, MeasurementsFixture) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 53 | |
| 54 | BOOST_AUTO_TEST_CASE(Get_Parent) |
| 55 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 56 | Entry& entryAB = measurements.get("/A/B"); |
| 57 | BOOST_CHECK_EQUAL(entryAB.getName(), "/A/B"); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 58 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 59 | Entry& entry0 = measurements.get("/"); |
| 60 | BOOST_CHECK_EQUAL(entry0.getName(), "/"); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 62 | Entry* entryA = measurements.getParent(entryAB); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 63 | BOOST_REQUIRE(entryA != nullptr); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(entryA->getName(), "/A"); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 65 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 66 | Entry* entry0c = measurements.getParent(*entryA); |
| 67 | BOOST_REQUIRE(entry0c != nullptr); |
| 68 | BOOST_CHECK_EQUAL(&entry0, entry0c); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Junxiao Shi | 3f9234b | 2017-12-07 17:41:50 +0000 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE(GetLongName) |
| 72 | { |
| 73 | Name n; |
| 74 | while (n.size() < NameTree::getMaxDepth() - 1) { |
| 75 | n.append("A"); |
| 76 | } |
| 77 | Entry& entry1 = measurements.get(n); |
| 78 | BOOST_CHECK_EQUAL(entry1.getName().size(), NameTree::getMaxDepth() - 1); |
| 79 | |
| 80 | n.append("B"); |
| 81 | Entry& entry2 = measurements.get(n); |
| 82 | BOOST_CHECK_EQUAL(entry2.getName().size(), NameTree::getMaxDepth()); |
| 83 | |
| 84 | n.append("C"); |
| 85 | Entry& entry3 = measurements.get(n); |
| 86 | BOOST_CHECK_EQUAL(entry3.getName().size(), NameTree::getMaxDepth()); |
| 87 | } |
| 88 | |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(GetWithFibEntry) |
| 90 | { |
| 91 | Fib fib(nameTree); |
| 92 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 93 | const fib::Entry* fibA = fib.insert("/A").first; |
| 94 | const fib::Entry* fibAB = fib.insert("/A/B").first; |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 96 | Entry& entryA = measurements.get(*fibA); |
| 97 | BOOST_CHECK_EQUAL(entryA.getName(), "/A"); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 98 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 99 | Entry& entryAB = measurements.get(*fibAB); |
| 100 | BOOST_CHECK_EQUAL(entryAB.getName(), "/A/B"); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_CASE(GetWithEmptyFibEntry) // Bug 3275 |
| 104 | { |
| 105 | Fib fib(nameTree); |
| 106 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 107 | const fib::Entry& fib0 = fib.findLongestPrefixMatch("/"); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 108 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 109 | Entry& entry0 = measurements.get(fib0); |
| 110 | BOOST_CHECK_EQUAL(entry0.getName(), "/"); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE(GetWithPitEntry) |
| 114 | { |
| 115 | Pit pit(nameTree); |
| 116 | |
| 117 | shared_ptr<Interest> interestA = makeInterest("/A"); |
| 118 | shared_ptr<pit::Entry> pitA = pit.insert(*interestA).first; |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 119 | shared_ptr<Data> dataABC = makeData("/A/B/C"); |
| 120 | Name fullName = dataABC->getFullName(); |
| 121 | shared_ptr<Interest> interestFull = makeInterest(fullName); |
| 122 | shared_ptr<pit::Entry> pitFull = pit.insert(*interestFull).first; |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 123 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 124 | Entry& entryA = measurements.get(*pitA); |
| 125 | BOOST_CHECK_EQUAL(entryA.getName(), "/A"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 126 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 127 | Entry& entryFull = measurements.get(*pitFull); |
| 128 | BOOST_CHECK_EQUAL(entryFull.getName(), fullName); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 131 | class DummyStrategyInfo1 : public fw::StrategyInfo |
| 132 | { |
| 133 | public: |
| 134 | static constexpr int |
| 135 | getTypeId() |
| 136 | { |
| 137 | return 21; |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | class DummyStrategyInfo2 : public fw::StrategyInfo |
| 142 | { |
| 143 | public: |
| 144 | static constexpr int |
| 145 | getTypeId() |
| 146 | { |
| 147 | return 22; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch) |
| 152 | { |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 153 | measurements.get("/A"); |
Junxiao Shi | 5b3feb6 | 2016-08-19 01:51:41 +0000 | [diff] [blame] | 154 | measurements.get("/A/B/C").insertStrategyInfo<DummyStrategyInfo1>(); |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 155 | measurements.get("/A/B/C/D"); |
| 156 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 157 | Entry* found1 = measurements.findLongestPrefixMatch("/A/B/C/D/E"); |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 158 | BOOST_REQUIRE(found1 != nullptr); |
| 159 | BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D"); |
| 160 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 161 | Entry* found2 = measurements.findLongestPrefixMatch("/A/B/C/D/E", |
| 162 | EntryWithStrategyInfo<DummyStrategyInfo1>()); |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 163 | BOOST_REQUIRE(found2 != nullptr); |
| 164 | BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C"); |
| 165 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 166 | Entry* found3 = measurements.findLongestPrefixMatch("/A/B/C/D/E", |
| 167 | EntryWithStrategyInfo<DummyStrategyInfo2>()); |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 168 | BOOST_CHECK(found3 == nullptr); |
| 169 | } |
| 170 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 171 | BOOST_AUTO_TEST_CASE(FindLongestPrefixMatchWithPitEntry) |
| 172 | { |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 173 | Pit pit(nameTree); |
| 174 | |
| 175 | measurements.get("/A"); |
Junxiao Shi | 5b3feb6 | 2016-08-19 01:51:41 +0000 | [diff] [blame] | 176 | measurements.get("/A/B/C").insertStrategyInfo<DummyStrategyInfo1>(); |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 177 | measurements.get("/A/B/C/D"); |
| 178 | |
| 179 | shared_ptr<Interest> interest = makeInterest("/A/B/C/D/E"); |
| 180 | shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first; |
| 181 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 182 | Entry* found1 = measurements.findLongestPrefixMatch(*pitEntry); |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 183 | BOOST_REQUIRE(found1 != nullptr); |
| 184 | BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D"); |
| 185 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 186 | Entry* found2 = measurements.findLongestPrefixMatch(*pitEntry, |
| 187 | EntryWithStrategyInfo<DummyStrategyInfo1>()); |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 188 | BOOST_REQUIRE(found2 != nullptr); |
| 189 | BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C"); |
| 190 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 191 | Entry* found3 = measurements.findLongestPrefixMatch(*pitEntry, |
| 192 | EntryWithStrategyInfo<DummyStrategyInfo2>()); |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 193 | BOOST_CHECK(found3 == nullptr); |
| 194 | } |
| 195 | |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 196 | BOOST_AUTO_TEST_CASE(Lifetime) |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 197 | { |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 198 | Name nameA("ndn:/A"); |
| 199 | Name nameB("ndn:/B"); |
| 200 | Name nameC("ndn:/C"); |
| 201 | |
| 202 | BOOST_CHECK_EQUAL(measurements.size(), 0); |
| 203 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 204 | Entry& entryA = measurements.get(nameA); |
| 205 | measurements.get(nameB); |
| 206 | Entry& entryC = measurements.get(nameC); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(measurements.size(), 3); |
| 208 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 209 | const time::nanoseconds EXTEND_A = 2_s; |
| 210 | const time::nanoseconds CHECK1 = 3_s; |
| 211 | const time::nanoseconds CHECK2 = 5_s; |
| 212 | const time::nanoseconds EXTEND_C = 6_s; |
| 213 | const time::nanoseconds CHECK3 = 7_s; |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 214 | BOOST_ASSERT(EXTEND_A < CHECK1); |
| 215 | BOOST_ASSERT(CHECK1 < Measurements::getInitialLifetime()); |
| 216 | BOOST_ASSERT(Measurements::getInitialLifetime() < CHECK2); |
| 217 | BOOST_ASSERT(CHECK2 < EXTEND_C); |
| 218 | BOOST_ASSERT(EXTEND_C < CHECK3); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 219 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 220 | measurements.extendLifetime(entryA, EXTEND_A); |
| 221 | measurements.extendLifetime(entryC, EXTEND_C); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 222 | // remaining lifetime: |
| 223 | // A = initial lifetime, because it's extended by less duration |
| 224 | // B = initial lifetime |
| 225 | // C = EXTEND_C |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 226 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 227 | this->advanceClocks(100_ms, CHECK1); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 228 | BOOST_CHECK(measurements.findExactMatch(nameA) != nullptr); |
| 229 | BOOST_CHECK(measurements.findExactMatch(nameB) != nullptr); |
| 230 | BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 231 | BOOST_CHECK_EQUAL(measurements.size(), 3); |
| 232 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 233 | this->advanceClocks(100_ms, CHECK2 - CHECK1); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 234 | BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr); |
| 235 | BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr); |
| 236 | BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 237 | BOOST_CHECK_EQUAL(measurements.size(), 1); |
| 238 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 239 | this->advanceClocks(100_ms, CHECK3 - CHECK2); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 240 | BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr); |
| 241 | BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr); |
| 242 | BOOST_CHECK(measurements.findExactMatch(nameC) == nullptr); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 243 | BOOST_CHECK_EQUAL(measurements.size(), 0); |
| 244 | } |
| 245 | |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 246 | BOOST_AUTO_TEST_CASE(EraseNameTreeEntry) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 247 | { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 248 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 249 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 250 | measurements.get("/A"); |
| 251 | BOOST_CHECK_EQUAL(measurements.size(), 1); |
| 252 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 253 | this->advanceClocks(Measurements::getInitialLifetime() + 10_ms); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 254 | BOOST_CHECK_EQUAL(measurements.size(), 0); |
| 255 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 256 | } |
| 257 | |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 258 | BOOST_AUTO_TEST_SUITE_END() // TestMeasurements |
| 259 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 260 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 261 | } // namespace nfd::tests |