blob: e795d3560d7c763b23ee64cb237461b0437b8775 [file] [log] [blame]
Junxiao Shi0e42c572014-10-05 20:33:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib2831282015-01-09 14:55:58 -07003 * Copyright (c) 2014-2015, 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.
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
33BOOST_FIXTURE_TEST_SUITE(TableDeadNonceList, BaseFixture)
34
35BOOST_AUTO_TEST_CASE(Basic)
36{
37 Name nameA("ndn:/A");
38 Name nameB("ndn:/B");
39 const uint32_t nonce1 = 0x53b4eaa8;
40 const uint32_t nonce2 = 0x1f46372b;
41
42 DeadNonceList dnl;
Junxiao Shia110f262014-10-12 12:35:20 -070043 BOOST_CHECK_EQUAL(dnl.size(), 0);
Junxiao Shi0e42c572014-10-05 20:33:48 -070044 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), false);
Junxiao Shia110f262014-10-12 12:35:20 -070045
Junxiao Shi0e42c572014-10-05 20:33:48 -070046 dnl.add(nameA, nonce1);
Junxiao Shia110f262014-10-12 12:35:20 -070047 BOOST_CHECK_EQUAL(dnl.size(), 1);
Junxiao Shi0e42c572014-10-05 20:33:48 -070048 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
49 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
50 BOOST_CHECK_EQUAL(dnl.has(nameB, nonce1), false);
51}
52
Junxiao Shia110f262014-10-12 12:35:20 -070053BOOST_AUTO_TEST_CASE(MinLifetime)
54{
55 BOOST_CHECK_THROW(DeadNonceList dnl(time::milliseconds::zero()), std::invalid_argument);
56}
57
Junxiao Shi0e42c572014-10-05 20:33:48 -070058/// A Fixture that periodically inserts Nonces
Junxiao Shib2831282015-01-09 14:55:58 -070059class PeriodicalInsertionFixture : public UnitTestTimeFixture
Junxiao Shi0e42c572014-10-05 20:33:48 -070060{
61protected:
62 PeriodicalInsertionFixture()
63 : dnl(LIFETIME)
64 , name("ndn:/N")
65 , addNonceBatch(0)
66 , addNonceInterval(LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT)
Junxiao Shib2831282015-01-09 14:55:58 -070067 , timeUnit(addNonceInterval / 2)
Junxiao Shi0e42c572014-10-05 20:33:48 -070068 {
69 this->addNonce();
70 }
71
Junxiao Shi0e42c572014-10-05 20:33:48 -070072 void
73 setRate(size_t nNoncesPerLifetime)
74 {
75 addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT;
76 }
77
78 void
79 addNonce()
80 {
81 for (size_t i = 0; i < addNonceBatch; ++i) {
82 dnl.add(name, ++lastNonce);
83 }
84
Junxiao Shi0e42c572014-10-05 20:33:48 -070085 if (addNonceInterval > time::nanoseconds::zero()) {
86 addNonceEvent = scheduler::schedule(addNonceInterval,
87 bind(&PeriodicalInsertionFixture::addNonce, this));
88 }
89 }
90
Junxiao Shib2831282015-01-09 14:55:58 -070091 /** \brief advance clocks by LIFETIME*t
92 */
93 void
94 advanceClocksByLifetime(float t)
95 {
96 this->advanceClocks(timeUnit, time::duration_cast<time::nanoseconds>(LIFETIME * t));
97 }
98
Junxiao Shi0e42c572014-10-05 20:33:48 -070099protected:
100 static const time::nanoseconds LIFETIME;
101 DeadNonceList dnl;
102 Name name;
103 uint32_t lastNonce;
104 size_t addNonceBatch;
105 time::nanoseconds addNonceInterval;
Junxiao Shib2831282015-01-09 14:55:58 -0700106 time::nanoseconds timeUnit;
107 scheduler::ScopedEventId addNonceEvent;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700108};
109const time::nanoseconds PeriodicalInsertionFixture::LIFETIME = time::milliseconds(200);
110
111BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture)
112{
Junxiao Shia110f262014-10-12 12:35:20 -0700113 BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME);
114
Junxiao Shi0e42c572014-10-05 20:33:48 -0700115 const int RATE = DeadNonceList::INITIAL_CAPACITY / 2;
116 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700117 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700118
119 Name nameC("ndn:/C");
120 const uint32_t nonceC = 0x25390656;
121 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
122 dnl.add(nameC, nonceC);
123 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
124
Junxiao Shib2831282015-01-09 14:55:58 -0700125 this->advanceClocksByLifetime(0.5); // -50%, entry should exist
Junxiao Shi0e42c572014-10-05 20:33:48 -0700126 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
127
Junxiao Shib2831282015-01-09 14:55:58 -0700128 this->advanceClocksByLifetime(1.0); // +50%, entry should be gone
Junxiao Shi0e42c572014-10-05 20:33:48 -0700129 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
130}
131
132BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture)
133{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700134 ssize_t cap0 = dnl.m_capacity;
135
136 const int RATE = DeadNonceList::INITIAL_CAPACITY / 3;
137 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700138 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700139
140 ssize_t cap1 = dnl.m_capacity;
141 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
142}
143
144BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture)
145{
Junxiao Shi0e42c572014-10-05 20:33:48 -0700146 ssize_t cap0 = dnl.m_capacity;
147
148 const int RATE = DeadNonceList::INITIAL_CAPACITY * 3;
149 this->setRate(RATE);
Junxiao Shib2831282015-01-09 14:55:58 -0700150 this->advanceClocksByLifetime(10.0);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700151
152 ssize_t cap1 = dnl.m_capacity;
153 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
154}
155
156BOOST_AUTO_TEST_SUITE_END()
157
158} // namespace tests
159} // namespace nfd