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, |
| 6 | * University Pierre & Marie Curie, Sorbonne Universit |
| 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 | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 27 | #include "table/pit.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 32 | namespace tests { |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 34 | BOOST_FIXTURE_TEST_SUITE(TableMeasurements, BaseFixture) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 35 | |
| 36 | BOOST_AUTO_TEST_CASE(Get_Parent) |
| 37 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 38 | NameTree nameTree; |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 39 | Measurements measurements(nameTree); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 40 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 41 | Name name0; |
| 42 | Name nameA ("ndn:/A"); |
| 43 | Name nameAB("ndn:/A/B"); |
| 44 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 45 | shared_ptr<measurements::Entry> entryAB = measurements.get(nameAB); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 46 | BOOST_REQUIRE(entryAB != nullptr); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(entryAB->getName(), nameAB); |
| 48 | |
| 49 | shared_ptr<measurements::Entry> entry0 = measurements.get(name0); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 50 | BOOST_REQUIRE(entry0 != nullptr); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 51 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 52 | shared_ptr<measurements::Entry> entryA = measurements.getParent(*entryAB); |
| 53 | BOOST_REQUIRE(entryA != nullptr); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(entryA->getName(), nameA); |
| 55 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 56 | shared_ptr<measurements::Entry> entry0c = measurements.getParent(*entryA); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(entry0, entry0c); |
| 58 | } |
| 59 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 60 | class DummyStrategyInfo1 : public fw::StrategyInfo |
| 61 | { |
| 62 | public: |
| 63 | static constexpr int |
| 64 | getTypeId() |
| 65 | { |
| 66 | return 21; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | class DummyStrategyInfo2 : public fw::StrategyInfo |
| 71 | { |
| 72 | public: |
| 73 | static constexpr int |
| 74 | getTypeId() |
| 75 | { |
| 76 | return 22; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch) |
| 81 | { |
| 82 | NameTree nameTree; |
| 83 | Measurements measurements(nameTree); |
| 84 | |
| 85 | measurements.get("/A"); |
| 86 | measurements.get("/A/B/C")->getOrCreateStrategyInfo<DummyStrategyInfo1>(); |
| 87 | measurements.get("/A/B/C/D"); |
| 88 | |
| 89 | shared_ptr<measurements::Entry> found1 = measurements.findLongestPrefixMatch("/A/B/C/D/E"); |
| 90 | BOOST_REQUIRE(found1 != nullptr); |
| 91 | BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D"); |
| 92 | |
| 93 | shared_ptr<measurements::Entry> found2 = measurements.findLongestPrefixMatch("/A/B/C/D/E", |
| 94 | measurements::EntryWithStrategyInfo<DummyStrategyInfo1>()); |
| 95 | BOOST_REQUIRE(found2 != nullptr); |
| 96 | BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C"); |
| 97 | |
| 98 | shared_ptr<measurements::Entry> found3 = measurements.findLongestPrefixMatch("/A/B/C/D/E", |
| 99 | measurements::EntryWithStrategyInfo<DummyStrategyInfo2>()); |
| 100 | BOOST_CHECK(found3 == nullptr); |
| 101 | } |
| 102 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 103 | BOOST_AUTO_TEST_CASE(FindLongestPrefixMatchWithPitEntry) |
| 104 | { |
| 105 | NameTree nameTree; |
| 106 | Measurements measurements(nameTree); |
| 107 | Pit pit(nameTree); |
| 108 | |
| 109 | measurements.get("/A"); |
| 110 | measurements.get("/A/B/C")->getOrCreateStrategyInfo<DummyStrategyInfo1>(); |
| 111 | measurements.get("/A/B/C/D"); |
| 112 | |
| 113 | shared_ptr<Interest> interest = makeInterest("/A/B/C/D/E"); |
| 114 | shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first; |
| 115 | |
| 116 | shared_ptr<measurements::Entry> found1 = measurements.findLongestPrefixMatch(*pitEntry); |
| 117 | BOOST_REQUIRE(found1 != nullptr); |
| 118 | BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D"); |
| 119 | |
| 120 | shared_ptr<measurements::Entry> found2 = measurements.findLongestPrefixMatch(*pitEntry, |
| 121 | measurements::EntryWithStrategyInfo<DummyStrategyInfo1>()); |
| 122 | BOOST_REQUIRE(found2 != nullptr); |
| 123 | BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C"); |
| 124 | |
| 125 | shared_ptr<measurements::Entry> found3 = measurements.findLongestPrefixMatch(*pitEntry, |
| 126 | measurements::EntryWithStrategyInfo<DummyStrategyInfo2>()); |
| 127 | BOOST_CHECK(found3 == nullptr); |
| 128 | } |
| 129 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 130 | BOOST_FIXTURE_TEST_CASE(Lifetime, UnitTestTimeFixture) |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 131 | { |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 132 | NameTree nameTree; |
| 133 | Measurements measurements(nameTree); |
| 134 | Name nameA("ndn:/A"); |
| 135 | Name nameB("ndn:/B"); |
| 136 | Name nameC("ndn:/C"); |
| 137 | |
| 138 | BOOST_CHECK_EQUAL(measurements.size(), 0); |
| 139 | |
| 140 | shared_ptr<measurements::Entry> entryA = measurements.get(nameA); |
| 141 | shared_ptr<measurements::Entry> entryB = measurements.get(nameB); |
| 142 | shared_ptr<measurements::Entry> entryC = measurements.get(nameC); |
| 143 | BOOST_CHECK_EQUAL(measurements.size(), 3); |
| 144 | |
| 145 | const time::nanoseconds EXTEND_A = time::seconds(2); |
| 146 | const time::nanoseconds CHECK1 = time::seconds(3); |
| 147 | const time::nanoseconds CHECK2 = time::seconds(5); |
| 148 | const time::nanoseconds EXTEND_C = time::seconds(6); |
| 149 | const time::nanoseconds CHECK3 = time::seconds(7); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 150 | BOOST_ASSERT(EXTEND_A < CHECK1); |
| 151 | BOOST_ASSERT(CHECK1 < Measurements::getInitialLifetime()); |
| 152 | BOOST_ASSERT(Measurements::getInitialLifetime() < CHECK2); |
| 153 | BOOST_ASSERT(CHECK2 < EXTEND_C); |
| 154 | BOOST_ASSERT(EXTEND_C < CHECK3); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 155 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 156 | measurements.extendLifetime(*entryA, EXTEND_A); |
| 157 | measurements.extendLifetime(*entryC, EXTEND_C); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 158 | // remaining lifetime: |
| 159 | // A = initial lifetime, because it's extended by less duration |
| 160 | // B = initial lifetime |
| 161 | // C = EXTEND_C |
| 162 | entryA.reset(); |
| 163 | entryB.reset(); |
| 164 | entryC.reset(); |
| 165 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 166 | this->advanceClocks(time::milliseconds(100), CHECK1); |
| 167 | BOOST_CHECK(measurements.findExactMatch(nameA) != nullptr); |
| 168 | BOOST_CHECK(measurements.findExactMatch(nameB) != nullptr); |
| 169 | BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(measurements.size(), 3); |
| 171 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 172 | this->advanceClocks(time::milliseconds(100), CHECK2 - CHECK1); |
| 173 | BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr); |
| 174 | BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr); |
| 175 | BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 176 | BOOST_CHECK_EQUAL(measurements.size(), 1); |
| 177 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 178 | this->advanceClocks(time::milliseconds(100), CHECK3 - CHECK2); |
| 179 | BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr); |
| 180 | BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr); |
| 181 | BOOST_CHECK(measurements.findExactMatch(nameC) == nullptr); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 182 | BOOST_CHECK_EQUAL(measurements.size(), 0); |
| 183 | } |
| 184 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 185 | BOOST_FIXTURE_TEST_CASE(EraseNameTreeEntry, UnitTestTimeFixture) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 186 | { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 187 | NameTree nameTree; |
| 188 | Measurements measurements(nameTree); |
| 189 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 190 | |
| 191 | shared_ptr<measurements::Entry> entry = measurements.get("/A"); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 192 | this->advanceClocks(Measurements::getInitialLifetime() + time::milliseconds(10)); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 193 | BOOST_CHECK_EQUAL(measurements.size(), 0); |
| 194 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 195 | } |
| 196 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 197 | BOOST_AUTO_TEST_SUITE_END() |
| 198 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 199 | } // namespace tests |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 200 | } // namespace nfd |