blob: fb3ab90d38e8b6d9c3ddecd1796584055b541f82 [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/*
Davide Pesaventod4123932021-05-28 18:48:37 -04003 * Copyright (c) 2014-2021, 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 Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/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");
Davide Pesavento51cf75c2020-03-11 22:21:13 -040042 const Interest::Nonce nonce1(0x53b4eaa8);
43 const Interest::Nonce nonce2(0x1f46372b);
Junxiao Shi0e42c572014-10-05 20:33:48 -070044
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{
Davide Pesaventod4123932021-05-28 18:48:37 -040058 BOOST_CHECK_THROW(DeadNonceList(0_ms), std::invalid_argument);
Junxiao Shia110f262014-10-12 12:35:20 -070059}
60
Davide Pesaventod4123932021-05-28 18:48:37 -040061/// 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()
Junxiao Shi0e42c572014-10-05 20:33:48 -070066 {
Davide Pesavento51cf75c2020-03-11 22:21:13 -040067 addNonce();
Junxiao Shi0e42c572014-10-05 20:33:48 -070068 }
69
Junxiao Shi0e42c572014-10-05 20:33:48 -070070 void
71 setRate(size_t nNoncesPerLifetime)
72 {
73 addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT;
74 }
75
76 void
77 addNonce()
78 {
79 for (size_t i = 0; i < addNonceBatch; ++i) {
80 dnl.add(name, ++lastNonce);
81 }
82
Davide Pesaventod4123932021-05-28 18:48:37 -040083 addNonceEvent = getScheduler().schedule(ADD_INTERVAL, [this] { addNonce(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -070084 }
85
Junxiao Shib2831282015-01-09 14:55:58 -070086 /** \brief advance clocks by LIFETIME*t
87 */
88 void
Davide Pesaventod4123932021-05-28 18:48:37 -040089 advanceClocksByLifetime(double t)
Junxiao Shib2831282015-01-09 14:55:58 -070090 {
Davide Pesaventod4123932021-05-28 18:48:37 -040091 advanceClocks(ADD_INTERVAL / 2, time::duration_cast<time::nanoseconds>(LIFETIME * t));
Junxiao Shib2831282015-01-09 14:55:58 -070092 }
93
Junxiao Shi0e42c572014-10-05 20:33:48 -070094protected:
Davide Pesaventod4123932021-05-28 18:48:37 -040095 static constexpr time::nanoseconds LIFETIME = 200_ms;
96 static constexpr time::nanoseconds ADD_INTERVAL = LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT;
97
98 DeadNonceList dnl{LIFETIME};
Davide Pesavento51cf75c2020-03-11 22:21:13 -040099 Name name = "/N";
100 uint32_t lastNonce = 0;
101 size_t addNonceBatch = 0;
Junxiao Shib2831282015-01-09 14:55:58 -0700102 scheduler::ScopedEventId addNonceEvent;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700103};
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400104
Davide Pesaventod4123932021-05-28 18:48:37 -0400105const time::nanoseconds PeriodicalInsertionFixture::LIFETIME;
106const time::nanoseconds PeriodicalInsertionFixture::ADD_INTERVAL;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700107
108BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture)
109{
Junxiao Shia110f262014-10-12 12:35:20 -0700110 BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME);
111
Junxiao Shi0e42c572014-10-05 20:33:48 -0700112 const int RATE = DeadNonceList::INITIAL_CAPACITY / 2;
113 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700114 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700115
116 Name nameC("ndn:/C");
Davide Pesavento51cf75c2020-03-11 22:21:13 -0400117 const Interest::Nonce nonceC(0x25390656);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700118 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
119 dnl.add(nameC, nonceC);
120 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
121
Junxiao Shib2831282015-01-09 14:55:58 -0700122 this->advanceClocksByLifetime(0.5); // -50%, entry should exist
Junxiao Shi0e42c572014-10-05 20:33:48 -0700123 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
124
Junxiao Shib2831282015-01-09 14:55:58 -0700125 this->advanceClocksByLifetime(1.0); // +50%, entry should be gone
Junxiao Shi0e42c572014-10-05 20:33:48 -0700126 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
127}
128
129BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture)
130{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700131 ssize_t cap0 = dnl.m_capacity;
132
133 const int RATE = DeadNonceList::INITIAL_CAPACITY / 3;
134 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700135 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700136
137 ssize_t cap1 = dnl.m_capacity;
138 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
139}
140
141BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture)
142{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700143 ssize_t cap0 = dnl.m_capacity;
144
145 const int RATE = DeadNonceList::INITIAL_CAPACITY * 3;
146 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700147 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700148
149 ssize_t cap1 = dnl.m_capacity;
150 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
151}
152
Junxiao Shi3978c922016-07-15 18:24:11 +0000153BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList
154BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shi0e42c572014-10-05 20:33:48 -0700155
156} // namespace tests
157} // namespace nfd