blob: a5a537e3fceb86b62a426e8a2008f4c0f07fb608 [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 Pesavento51cf75c2020-03-11 22:21:13 -04003 * Copyright (c) 2014-2020, 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{
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)
Junxiao Shi0e42c572014-10-05 20:33:48 -070067 {
Davide Pesavento51cf75c2020-03-11 22:21:13 -040068 addNonce();
Junxiao Shi0e42c572014-10-05 20:33:48 -070069 }
70
Junxiao Shi0e42c572014-10-05 20:33:48 -070071 void
72 setRate(size_t nNoncesPerLifetime)
73 {
74 addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT;
75 }
76
77 void
78 addNonce()
79 {
80 for (size_t i = 0; i < addNonceBatch; ++i) {
81 dnl.add(name, ++lastNonce);
82 }
83
Davide Pesavento51cf75c2020-03-11 22:21:13 -040084 if (addNonceInterval > 0_ns) {
Davide Pesavento3dade002019-03-19 11:29:56 -060085 addNonceEvent = getScheduler().schedule(addNonceInterval, [this] { addNonce(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -070086 }
87 }
88
Junxiao Shib2831282015-01-09 14:55:58 -070089 /** \brief advance clocks by LIFETIME*t
90 */
91 void
92 advanceClocksByLifetime(float t)
93 {
Davide Pesavento51cf75c2020-03-11 22:21:13 -040094 advanceClocks(timeUnit, time::duration_cast<time::nanoseconds>(LIFETIME * t));
Junxiao Shib2831282015-01-09 14:55:58 -070095 }
96
Junxiao Shi0e42c572014-10-05 20:33:48 -070097protected:
98 static const time::nanoseconds LIFETIME;
99 DeadNonceList dnl;
Davide Pesavento51cf75c2020-03-11 22:21:13 -0400100 Name name = "/N";
101 uint32_t lastNonce = 0;
102 size_t addNonceBatch = 0;
103 const time::nanoseconds addNonceInterval = LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT;
104 const time::nanoseconds timeUnit = addNonceInterval / 2;
Junxiao Shib2831282015-01-09 14:55:58 -0700105 scheduler::ScopedEventId addNonceEvent;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700106};
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400107
Davide Pesavento14e71f02019-03-28 17:35:25 -0400108const time::nanoseconds PeriodicalInsertionFixture::LIFETIME = 200_ms;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700109
110BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture)
111{
Junxiao Shia110f262014-10-12 12:35:20 -0700112 BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME);
113
Junxiao Shi0e42c572014-10-05 20:33:48 -0700114 const int RATE = DeadNonceList::INITIAL_CAPACITY / 2;
115 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700116 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700117
118 Name nameC("ndn:/C");
Davide Pesavento51cf75c2020-03-11 22:21:13 -0400119 const Interest::Nonce nonceC(0x25390656);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700120 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
121 dnl.add(nameC, nonceC);
122 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
123
Junxiao Shib2831282015-01-09 14:55:58 -0700124 this->advanceClocksByLifetime(0.5); // -50%, entry should exist
Junxiao Shi0e42c572014-10-05 20:33:48 -0700125 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
126
Junxiao Shib2831282015-01-09 14:55:58 -0700127 this->advanceClocksByLifetime(1.0); // +50%, entry should be gone
Junxiao Shi0e42c572014-10-05 20:33:48 -0700128 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
129}
130
131BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture)
132{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700133 ssize_t cap0 = dnl.m_capacity;
134
135 const int RATE = DeadNonceList::INITIAL_CAPACITY / 3;
136 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700137 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700138
139 ssize_t cap1 = dnl.m_capacity;
140 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
141}
142
143BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture)
144{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700145 ssize_t cap0 = dnl.m_capacity;
146
147 const int RATE = DeadNonceList::INITIAL_CAPACITY * 3;
148 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700149 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700150
151 ssize_t cap1 = dnl.m_capacity;
152 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
153}
154
Junxiao Shi3978c922016-07-15 18:24:11 +0000155BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList
156BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shi0e42c572014-10-05 20:33:48 -0700157
158} // namespace tests
159} // namespace nfd