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