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 | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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; |
| 102 | |
| 103 | shared_ptr<measurements::Entry> entryA = measurements.get(*pitA); |
| 104 | BOOST_REQUIRE(entryA != nullptr); |
| 105 | BOOST_CHECK_EQUAL(entryA->getName(), "/A"); |
| 106 | } |
| 107 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 108 | class DummyStrategyInfo1 : public fw::StrategyInfo |
| 109 | { |
| 110 | public: |
| 111 | static constexpr int |
| 112 | getTypeId() |
| 113 | { |
| 114 | return 21; |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | class DummyStrategyInfo2 : public fw::StrategyInfo |
| 119 | { |
| 120 | public: |
| 121 | static constexpr int |
| 122 | getTypeId() |
| 123 | { |
| 124 | return 22; |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch) |
| 129 | { |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 130 | measurements.get("/A"); |
| 131 | measurements.get("/A/B/C")->getOrCreateStrategyInfo<DummyStrategyInfo1>(); |
| 132 | measurements.get("/A/B/C/D"); |
| 133 | |
| 134 | shared_ptr<measurements::Entry> found1 = measurements.findLongestPrefixMatch("/A/B/C/D/E"); |
| 135 | BOOST_REQUIRE(found1 != nullptr); |
| 136 | BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D"); |
| 137 | |
| 138 | shared_ptr<measurements::Entry> found2 = measurements.findLongestPrefixMatch("/A/B/C/D/E", |
| 139 | measurements::EntryWithStrategyInfo<DummyStrategyInfo1>()); |
| 140 | BOOST_REQUIRE(found2 != nullptr); |
| 141 | BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C"); |
| 142 | |
| 143 | shared_ptr<measurements::Entry> found3 = measurements.findLongestPrefixMatch("/A/B/C/D/E", |
| 144 | measurements::EntryWithStrategyInfo<DummyStrategyInfo2>()); |
| 145 | BOOST_CHECK(found3 == nullptr); |
| 146 | } |
| 147 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 148 | BOOST_AUTO_TEST_CASE(FindLongestPrefixMatchWithPitEntry) |
| 149 | { |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 150 | Pit pit(nameTree); |
| 151 | |
| 152 | measurements.get("/A"); |
| 153 | measurements.get("/A/B/C")->getOrCreateStrategyInfo<DummyStrategyInfo1>(); |
| 154 | measurements.get("/A/B/C/D"); |
| 155 | |
| 156 | shared_ptr<Interest> interest = makeInterest("/A/B/C/D/E"); |
| 157 | shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first; |
| 158 | |
| 159 | shared_ptr<measurements::Entry> found1 = measurements.findLongestPrefixMatch(*pitEntry); |
| 160 | BOOST_REQUIRE(found1 != nullptr); |
| 161 | BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D"); |
| 162 | |
| 163 | shared_ptr<measurements::Entry> found2 = measurements.findLongestPrefixMatch(*pitEntry, |
| 164 | measurements::EntryWithStrategyInfo<DummyStrategyInfo1>()); |
| 165 | BOOST_REQUIRE(found2 != nullptr); |
| 166 | BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C"); |
| 167 | |
| 168 | shared_ptr<measurements::Entry> found3 = measurements.findLongestPrefixMatch(*pitEntry, |
| 169 | measurements::EntryWithStrategyInfo<DummyStrategyInfo2>()); |
| 170 | BOOST_CHECK(found3 == nullptr); |
| 171 | } |
| 172 | |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 173 | BOOST_AUTO_TEST_CASE(Lifetime) |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 174 | { |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 175 | Name nameA("ndn:/A"); |
| 176 | Name nameB("ndn:/B"); |
| 177 | Name nameC("ndn:/C"); |
| 178 | |
| 179 | BOOST_CHECK_EQUAL(measurements.size(), 0); |
| 180 | |
| 181 | shared_ptr<measurements::Entry> entryA = measurements.get(nameA); |
| 182 | shared_ptr<measurements::Entry> entryB = measurements.get(nameB); |
| 183 | shared_ptr<measurements::Entry> entryC = measurements.get(nameC); |
| 184 | BOOST_CHECK_EQUAL(measurements.size(), 3); |
| 185 | |
| 186 | const time::nanoseconds EXTEND_A = time::seconds(2); |
| 187 | const time::nanoseconds CHECK1 = time::seconds(3); |
| 188 | const time::nanoseconds CHECK2 = time::seconds(5); |
| 189 | const time::nanoseconds EXTEND_C = time::seconds(6); |
| 190 | const time::nanoseconds CHECK3 = time::seconds(7); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 191 | BOOST_ASSERT(EXTEND_A < CHECK1); |
| 192 | BOOST_ASSERT(CHECK1 < Measurements::getInitialLifetime()); |
| 193 | BOOST_ASSERT(Measurements::getInitialLifetime() < CHECK2); |
| 194 | BOOST_ASSERT(CHECK2 < EXTEND_C); |
| 195 | BOOST_ASSERT(EXTEND_C < CHECK3); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 196 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 197 | measurements.extendLifetime(*entryA, EXTEND_A); |
| 198 | measurements.extendLifetime(*entryC, EXTEND_C); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 199 | // remaining lifetime: |
| 200 | // A = initial lifetime, because it's extended by less duration |
| 201 | // B = initial lifetime |
| 202 | // C = EXTEND_C |
| 203 | entryA.reset(); |
| 204 | entryB.reset(); |
| 205 | entryC.reset(); |
| 206 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 207 | this->advanceClocks(time::milliseconds(100), CHECK1); |
| 208 | BOOST_CHECK(measurements.findExactMatch(nameA) != nullptr); |
| 209 | BOOST_CHECK(measurements.findExactMatch(nameB) != nullptr); |
| 210 | BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 211 | BOOST_CHECK_EQUAL(measurements.size(), 3); |
| 212 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 213 | this->advanceClocks(time::milliseconds(100), CHECK2 - CHECK1); |
| 214 | BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr); |
| 215 | BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr); |
| 216 | BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(measurements.size(), 1); |
| 218 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 219 | this->advanceClocks(time::milliseconds(100), CHECK3 - CHECK2); |
| 220 | BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr); |
| 221 | BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr); |
| 222 | BOOST_CHECK(measurements.findExactMatch(nameC) == nullptr); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 223 | BOOST_CHECK_EQUAL(measurements.size(), 0); |
| 224 | } |
| 225 | |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 226 | BOOST_AUTO_TEST_CASE(EraseNameTreeEntry) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 227 | { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 228 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 229 | |
| 230 | shared_ptr<measurements::Entry> entry = measurements.get("/A"); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 231 | this->advanceClocks(Measurements::getInitialLifetime() + time::milliseconds(10)); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 232 | BOOST_CHECK_EQUAL(measurements.size(), 0); |
| 233 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 234 | } |
| 235 | |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 236 | BOOST_AUTO_TEST_SUITE_END() // TestMeasurements |
| 237 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 238 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 239 | } // namespace tests |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 240 | } // namespace nfd |