blob: ea7866c9f67f3d43d83aa07794a2589d31f3be96 [file] [log] [blame]
Junxiao Shi0e42c572014-10-05 20:33:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento3dade002019-03-19 11:29:56 -06002/*
3 * Copyright (c) 2014-2019, 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"
Davide Pesavento3dade002019-03-19 11:29:56 -060027#include "daemon/global.hpp"
Junxiao Shi0e42c572014-10-05 20:33:48 -070028
29#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shi0e42c572014-10-05 20:33:48 -070031
32namespace nfd {
33namespace tests {
34
Junxiao Shi3978c922016-07-15 18:24:11 +000035BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040036BOOST_FIXTURE_TEST_SUITE(TestDeadNonceList, GlobalIoFixture)
Junxiao Shi0e42c572014-10-05 20:33:48 -070037
38BOOST_AUTO_TEST_CASE(Basic)
39{
40 Name nameA("ndn:/A");
41 Name nameB("ndn:/B");
42 const uint32_t nonce1 = 0x53b4eaa8;
43 const uint32_t nonce2 = 0x1f46372b;
44
45 DeadNonceList dnl;
Junxiao Shia110f262014-10-12 12:35:20 -070046 BOOST_CHECK_EQUAL(dnl.size(), 0);
Junxiao Shi0e42c572014-10-05 20:33:48 -070047 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), false);
Junxiao Shia110f262014-10-12 12:35:20 -070048
Junxiao Shi0e42c572014-10-05 20:33:48 -070049 dnl.add(nameA, nonce1);
Junxiao Shia110f262014-10-12 12:35:20 -070050 BOOST_CHECK_EQUAL(dnl.size(), 1);
Junxiao Shi0e42c572014-10-05 20:33:48 -070051 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
52 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
53 BOOST_CHECK_EQUAL(dnl.has(nameB, nonce1), false);
54}
55
Junxiao Shia110f262014-10-12 12:35:20 -070056BOOST_AUTO_TEST_CASE(MinLifetime)
57{
58 BOOST_CHECK_THROW(DeadNonceList dnl(time::milliseconds::zero()), std::invalid_argument);
59}
60
Junxiao Shi0e42c572014-10-05 20:33:48 -070061/// A Fixture that periodically inserts Nonces
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040062class PeriodicalInsertionFixture : public GlobalIoTimeFixture
Junxiao Shi0e42c572014-10-05 20:33:48 -070063{
64protected:
65 PeriodicalInsertionFixture()
66 : dnl(LIFETIME)
67 , name("ndn:/N")
Junxiao Shi3978c922016-07-15 18:24:11 +000068 , lastNonce(0)
Junxiao Shi0e42c572014-10-05 20:33:48 -070069 , addNonceBatch(0)
70 , addNonceInterval(LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT)
Junxiao Shib2831282015-01-09 14:55:58 -070071 , timeUnit(addNonceInterval / 2)
Junxiao Shi0e42c572014-10-05 20:33:48 -070072 {
73 this->addNonce();
74 }
75
Junxiao Shi0e42c572014-10-05 20:33:48 -070076 void
77 setRate(size_t nNoncesPerLifetime)
78 {
79 addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT;
80 }
81
82 void
83 addNonce()
84 {
85 for (size_t i = 0; i < addNonceBatch; ++i) {
86 dnl.add(name, ++lastNonce);
87 }
88
Junxiao Shi0e42c572014-10-05 20:33:48 -070089 if (addNonceInterval > time::nanoseconds::zero()) {
Davide Pesavento3dade002019-03-19 11:29:56 -060090 addNonceEvent = getScheduler().schedule(addNonceInterval, [this] { addNonce(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -070091 }
92 }
93
Junxiao Shib2831282015-01-09 14:55:58 -070094 /** \brief advance clocks by LIFETIME*t
95 */
96 void
97 advanceClocksByLifetime(float t)
98 {
99 this->advanceClocks(timeUnit, time::duration_cast<time::nanoseconds>(LIFETIME * t));
100 }
101
Junxiao Shi0e42c572014-10-05 20:33:48 -0700102protected:
103 static const time::nanoseconds LIFETIME;
104 DeadNonceList dnl;
105 Name name;
106 uint32_t lastNonce;
107 size_t addNonceBatch;
108 time::nanoseconds addNonceInterval;
Junxiao Shib2831282015-01-09 14:55:58 -0700109 time::nanoseconds timeUnit;
110 scheduler::ScopedEventId addNonceEvent;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700111};
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400112
Junxiao Shi0e42c572014-10-05 20:33:48 -0700113const time::nanoseconds PeriodicalInsertionFixture::LIFETIME = time::milliseconds(200);
114
115BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture)
116{
Junxiao Shia110f262014-10-12 12:35:20 -0700117 BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME);
118
Junxiao Shi0e42c572014-10-05 20:33:48 -0700119 const int RATE = DeadNonceList::INITIAL_CAPACITY / 2;
120 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700121 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700122
123 Name nameC("ndn:/C");
124 const uint32_t nonceC = 0x25390656;
125 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
126 dnl.add(nameC, nonceC);
127 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
128
Junxiao Shib2831282015-01-09 14:55:58 -0700129 this->advanceClocksByLifetime(0.5); // -50%, entry should exist
Junxiao Shi0e42c572014-10-05 20:33:48 -0700130 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
131
Junxiao Shib2831282015-01-09 14:55:58 -0700132 this->advanceClocksByLifetime(1.0); // +50%, entry should be gone
Junxiao Shi0e42c572014-10-05 20:33:48 -0700133 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
134}
135
136BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture)
137{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700138 ssize_t cap0 = dnl.m_capacity;
139
140 const int RATE = DeadNonceList::INITIAL_CAPACITY / 3;
141 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700142 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700143
144 ssize_t cap1 = dnl.m_capacity;
145 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
146}
147
148BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture)
149{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700150 ssize_t cap0 = dnl.m_capacity;
151
152 const int RATE = DeadNonceList::INITIAL_CAPACITY * 3;
153 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700154 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700155
156 ssize_t cap1 = dnl.m_capacity;
157 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
158}
159
Junxiao Shi3978c922016-07-15 18:24:11 +0000160BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList
161BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shi0e42c572014-10-05 20:33:48 -0700162
163} // namespace tests
164} // namespace nfd