blob: 4cf7f81a35a6e15ca759c1e6690cad71467b5067 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi19838042014-06-21 00:34:01 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi19838042014-06-21 00:34:01 -070024 */
Junxiao Shi65d00722014-02-17 10:50:20 -070025
26#include "table/measurements.hpp"
27
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Junxiao Shi19838042014-06-21 00:34:01 -070029#include "tests/limited-io.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070030
31namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Junxiao Shi65d00722014-02-17 10:50:20 -070033
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034BOOST_FIXTURE_TEST_SUITE(TableMeasurements, BaseFixture)
Junxiao Shi65d00722014-02-17 10:50:20 -070035
36BOOST_AUTO_TEST_CASE(Get_Parent)
37{
Haowei Yuanf52dac72014-03-24 23:35:03 -050038 NameTree nameTree;
HangZhangc85a23c2014-03-01 15:55:55 +080039 Measurements measurements(nameTree);
Junxiao Shic041ca32014-02-25 20:01:15 -070040
Junxiao Shi65d00722014-02-17 10:50:20 -070041 Name name0;
42 Name nameA ("ndn:/A");
43 Name nameAB("ndn:/A/B");
44
Junxiao Shi65d00722014-02-17 10:50:20 -070045 shared_ptr<measurements::Entry> entryAB = measurements.get(nameAB);
Junxiao Shie368d992014-12-02 23:44:31 -070046 BOOST_REQUIRE(entryAB != nullptr);
Junxiao Shi65d00722014-02-17 10:50:20 -070047 BOOST_CHECK_EQUAL(entryAB->getName(), nameAB);
48
49 shared_ptr<measurements::Entry> entry0 = measurements.get(name0);
Junxiao Shie368d992014-12-02 23:44:31 -070050 BOOST_REQUIRE(entry0 != nullptr);
Junxiao Shi65d00722014-02-17 10:50:20 -070051
Junxiao Shie368d992014-12-02 23:44:31 -070052 shared_ptr<measurements::Entry> entryA = measurements.getParent(*entryAB);
53 BOOST_REQUIRE(entryA != nullptr);
Junxiao Shi65d00722014-02-17 10:50:20 -070054 BOOST_CHECK_EQUAL(entryA->getName(), nameA);
55
Junxiao Shie368d992014-12-02 23:44:31 -070056 shared_ptr<measurements::Entry> entry0c = measurements.getParent(*entryA);
Junxiao Shi65d00722014-02-17 10:50:20 -070057 BOOST_CHECK_EQUAL(entry0, entry0c);
58}
59
Junxiao Shib30c7b02015-01-07 15:45:54 -070060class DummyStrategyInfo1 : public fw::StrategyInfo
61{
62public:
63 static constexpr int
64 getTypeId()
65 {
66 return 21;
67 }
68};
69
70class DummyStrategyInfo2 : public fw::StrategyInfo
71{
72public:
73 static constexpr int
74 getTypeId()
75 {
76 return 22;
77 }
78};
79
80BOOST_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 Shie368d992014-12-02 23:44:31 -0700103BOOST_FIXTURE_TEST_CASE(Lifetime, UnitTestTimeFixture)
Junxiao Shi19838042014-06-21 00:34:01 -0700104{
Junxiao Shi19838042014-06-21 00:34:01 -0700105 NameTree nameTree;
106 Measurements measurements(nameTree);
107 Name nameA("ndn:/A");
108 Name nameB("ndn:/B");
109 Name nameC("ndn:/C");
110
111 BOOST_CHECK_EQUAL(measurements.size(), 0);
112
113 shared_ptr<measurements::Entry> entryA = measurements.get(nameA);
114 shared_ptr<measurements::Entry> entryB = measurements.get(nameB);
115 shared_ptr<measurements::Entry> entryC = measurements.get(nameC);
116 BOOST_CHECK_EQUAL(measurements.size(), 3);
117
118 const time::nanoseconds EXTEND_A = time::seconds(2);
119 const time::nanoseconds CHECK1 = time::seconds(3);
120 const time::nanoseconds CHECK2 = time::seconds(5);
121 const time::nanoseconds EXTEND_C = time::seconds(6);
122 const time::nanoseconds CHECK3 = time::seconds(7);
Junxiao Shie368d992014-12-02 23:44:31 -0700123 BOOST_ASSERT(EXTEND_A < CHECK1);
124 BOOST_ASSERT(CHECK1 < Measurements::getInitialLifetime());
125 BOOST_ASSERT(Measurements::getInitialLifetime() < CHECK2);
126 BOOST_ASSERT(CHECK2 < EXTEND_C);
127 BOOST_ASSERT(EXTEND_C < CHECK3);
Junxiao Shi19838042014-06-21 00:34:01 -0700128
Junxiao Shie368d992014-12-02 23:44:31 -0700129 measurements.extendLifetime(*entryA, EXTEND_A);
130 measurements.extendLifetime(*entryC, EXTEND_C);
Junxiao Shi19838042014-06-21 00:34:01 -0700131 // remaining lifetime:
132 // A = initial lifetime, because it's extended by less duration
133 // B = initial lifetime
134 // C = EXTEND_C
135 entryA.reset();
136 entryB.reset();
137 entryC.reset();
138
Junxiao Shie368d992014-12-02 23:44:31 -0700139 this->advanceClocks(time::milliseconds(100), CHECK1);
140 BOOST_CHECK(measurements.findExactMatch(nameA) != nullptr);
141 BOOST_CHECK(measurements.findExactMatch(nameB) != nullptr);
142 BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700143 BOOST_CHECK_EQUAL(measurements.size(), 3);
144
Junxiao Shie368d992014-12-02 23:44:31 -0700145 this->advanceClocks(time::milliseconds(100), CHECK2 - CHECK1);
146 BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
147 BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
148 BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700149 BOOST_CHECK_EQUAL(measurements.size(), 1);
150
Junxiao Shie368d992014-12-02 23:44:31 -0700151 this->advanceClocks(time::milliseconds(100), CHECK3 - CHECK2);
152 BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
153 BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
154 BOOST_CHECK(measurements.findExactMatch(nameC) == nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700155 BOOST_CHECK_EQUAL(measurements.size(), 0);
156}
157
Junxiao Shie368d992014-12-02 23:44:31 -0700158BOOST_FIXTURE_TEST_CASE(EraseNameTreeEntry, UnitTestTimeFixture)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700159{
Junxiao Shiee5a4442014-07-27 17:13:43 -0700160 NameTree nameTree;
161 Measurements measurements(nameTree);
162 size_t nNameTreeEntriesBefore = nameTree.size();
163
164 shared_ptr<measurements::Entry> entry = measurements.get("/A");
Junxiao Shie368d992014-12-02 23:44:31 -0700165 this->advanceClocks(Measurements::getInitialLifetime() + time::milliseconds(10));
Junxiao Shiee5a4442014-07-27 17:13:43 -0700166 BOOST_CHECK_EQUAL(measurements.size(), 0);
167 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
168}
169
Junxiao Shi65d00722014-02-17 10:50:20 -0700170BOOST_AUTO_TEST_SUITE_END()
171
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700172} // namespace tests
Junxiao Shi65d00722014-02-17 10:50:20 -0700173} // namespace nfd