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