blob: 599a7e2b55400bfb3b801261a2772e90cd67fd81 [file] [log] [blame]
Junxiao Shi0e42c572014-10-05 20:33:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi3978c922016-07-15 18:24:11 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shib2831282015-01-09 14:55:58 -07004 * 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.
Junxiao Shi0e42c572014-10-05 20:33:48 -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/>.
24 */
25
26#include "table/dead-nonce-list.hpp"
27
28#include "tests/test-common.hpp"
Junxiao Shi0e42c572014-10-05 20:33:48 -070029
30namespace nfd {
31namespace tests {
32
Junxiao Shi3978c922016-07-15 18:24:11 +000033BOOST_AUTO_TEST_SUITE(Table)
34BOOST_FIXTURE_TEST_SUITE(TestDeadNonceList, BaseFixture)
Junxiao Shi0e42c572014-10-05 20:33:48 -070035
36BOOST_AUTO_TEST_CASE(Basic)
37{
38 Name nameA("ndn:/A");
39 Name nameB("ndn:/B");
40 const uint32_t nonce1 = 0x53b4eaa8;
41 const uint32_t nonce2 = 0x1f46372b;
42
43 DeadNonceList dnl;
Junxiao Shia110f262014-10-12 12:35:20 -070044 BOOST_CHECK_EQUAL(dnl.size(), 0);
Junxiao Shi0e42c572014-10-05 20:33:48 -070045 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), false);
Junxiao Shia110f262014-10-12 12:35:20 -070046
Junxiao Shi0e42c572014-10-05 20:33:48 -070047 dnl.add(nameA, nonce1);
Junxiao Shia110f262014-10-12 12:35:20 -070048 BOOST_CHECK_EQUAL(dnl.size(), 1);
Junxiao Shi0e42c572014-10-05 20:33:48 -070049 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
50 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
51 BOOST_CHECK_EQUAL(dnl.has(nameB, nonce1), false);
52}
53
Junxiao Shia110f262014-10-12 12:35:20 -070054BOOST_AUTO_TEST_CASE(MinLifetime)
55{
56 BOOST_CHECK_THROW(DeadNonceList dnl(time::milliseconds::zero()), std::invalid_argument);
57}
58
Junxiao Shi0e42c572014-10-05 20:33:48 -070059/// A Fixture that periodically inserts Nonces
Junxiao Shib2831282015-01-09 14:55:58 -070060class PeriodicalInsertionFixture : public UnitTestTimeFixture
Junxiao Shi0e42c572014-10-05 20:33:48 -070061{
62protected:
63 PeriodicalInsertionFixture()
64 : dnl(LIFETIME)
65 , name("ndn:/N")
Junxiao Shi3978c922016-07-15 18:24:11 +000066 , lastNonce(0)
Junxiao Shi0e42c572014-10-05 20:33:48 -070067 , addNonceBatch(0)
68 , addNonceInterval(LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT)
Junxiao Shib2831282015-01-09 14:55:58 -070069 , timeUnit(addNonceInterval / 2)
Junxiao Shi0e42c572014-10-05 20:33:48 -070070 {
71 this->addNonce();
72 }
73
Junxiao Shi0e42c572014-10-05 20:33:48 -070074 void
75 setRate(size_t nNoncesPerLifetime)
76 {
77 addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT;
78 }
79
80 void
81 addNonce()
82 {
83 for (size_t i = 0; i < addNonceBatch; ++i) {
84 dnl.add(name, ++lastNonce);
85 }
86
Junxiao Shi0e42c572014-10-05 20:33:48 -070087 if (addNonceInterval > time::nanoseconds::zero()) {
88 addNonceEvent = scheduler::schedule(addNonceInterval,
89 bind(&PeriodicalInsertionFixture::addNonce, this));
90 }
91 }
92
Junxiao Shib2831282015-01-09 14:55:58 -070093 /** \brief advance clocks by LIFETIME*t
94 */
95 void
96 advanceClocksByLifetime(float t)
97 {
98 this->advanceClocks(timeUnit, time::duration_cast<time::nanoseconds>(LIFETIME * t));
99 }
100
Junxiao Shi0e42c572014-10-05 20:33:48 -0700101protected:
102 static const time::nanoseconds LIFETIME;
103 DeadNonceList dnl;
104 Name name;
105 uint32_t lastNonce;
106 size_t addNonceBatch;
107 time::nanoseconds addNonceInterval;
Junxiao Shib2831282015-01-09 14:55:58 -0700108 time::nanoseconds timeUnit;
109 scheduler::ScopedEventId addNonceEvent;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700110};
111const time::nanoseconds PeriodicalInsertionFixture::LIFETIME = time::milliseconds(200);
112
113BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture)
114{
Junxiao Shia110f262014-10-12 12:35:20 -0700115 BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME);
116
Junxiao Shi0e42c572014-10-05 20:33:48 -0700117 const int RATE = DeadNonceList::INITIAL_CAPACITY / 2;
118 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700119 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700120
121 Name nameC("ndn:/C");
122 const uint32_t nonceC = 0x25390656;
123 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
124 dnl.add(nameC, nonceC);
125 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
126
Junxiao Shib2831282015-01-09 14:55:58 -0700127 this->advanceClocksByLifetime(0.5); // -50%, entry should exist
Junxiao Shi0e42c572014-10-05 20:33:48 -0700128 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
129
Junxiao Shib2831282015-01-09 14:55:58 -0700130 this->advanceClocksByLifetime(1.0); // +50%, entry should be gone
Junxiao Shi0e42c572014-10-05 20:33:48 -0700131 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
132}
133
134BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture)
135{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700136 ssize_t cap0 = dnl.m_capacity;
137
138 const int RATE = DeadNonceList::INITIAL_CAPACITY / 3;
139 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700140 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700141
142 ssize_t cap1 = dnl.m_capacity;
143 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
144}
145
146BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture)
147{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700148 ssize_t cap0 = dnl.m_capacity;
149
150 const int RATE = DeadNonceList::INITIAL_CAPACITY * 3;
151 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700152 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700153
154 ssize_t cap1 = dnl.m_capacity;
155 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
156}
157
Junxiao Shi3978c922016-07-15 18:24:11 +0000158BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList
159BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shi0e42c572014-10-05 20:33:48 -0700160
161} // namespace tests
162} // namespace nfd