blob: 970d53756661501c2b9e2c463f65a064a0b1509d [file] [log] [blame]
Junxiao Shi0e42c572014-10-05 20:33:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, 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
10 *
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"
29#include "tests/limited-io.hpp"
30
31namespace nfd {
32namespace tests {
33
34BOOST_FIXTURE_TEST_SUITE(TableDeadNonceList, BaseFixture)
35
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
60class PeriodicalInsertionFixture : public BaseFixture
61{
62protected:
63 PeriodicalInsertionFixture()
64 : dnl(LIFETIME)
65 , name("ndn:/N")
66 , addNonceBatch(0)
67 , addNonceInterval(LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT)
68 {
69 this->addNonce();
70 }
71
72 ~PeriodicalInsertionFixture()
73 {
74 scheduler::cancel(addNonceEvent);
75 }
76
77 void
78 setRate(size_t nNoncesPerLifetime)
79 {
80 addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT;
81 }
82
83 void
84 addNonce()
85 {
86 for (size_t i = 0; i < addNonceBatch; ++i) {
87 dnl.add(name, ++lastNonce);
88 }
89
90 scheduler::cancel(addNonceEvent); // avoid double schedules
91 if (addNonceInterval > time::nanoseconds::zero()) {
92 addNonceEvent = scheduler::schedule(addNonceInterval,
93 bind(&PeriodicalInsertionFixture::addNonce, this));
94 }
95 }
96
97protected:
98 static const time::nanoseconds LIFETIME;
99 DeadNonceList dnl;
100 Name name;
101 uint32_t lastNonce;
102 size_t addNonceBatch;
103 time::nanoseconds addNonceInterval;
104 scheduler::EventId addNonceEvent;
105};
106const time::nanoseconds PeriodicalInsertionFixture::LIFETIME = time::milliseconds(200);
107
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 LimitedIo limitedIo;
113
114 const int RATE = DeadNonceList::INITIAL_CAPACITY / 2;
115 this->setRate(RATE);
116 limitedIo.defer(LIFETIME * 10);
117
118 Name nameC("ndn:/C");
119 const uint32_t nonceC = 0x25390656;
120 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
121 dnl.add(nameC, nonceC);
122 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
123
124 limitedIo.defer(LIFETIME / 2); // -50%, entry should exist
125 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
126
127 limitedIo.defer(LIFETIME); // +50%, entry should be gone
128 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
129}
130
131BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture)
132{
133 LimitedIo limitedIo;
134
135 ssize_t cap0 = dnl.m_capacity;
136
137 const int RATE = DeadNonceList::INITIAL_CAPACITY / 3;
138 this->setRate(RATE);
139 limitedIo.defer(LIFETIME * 10);
140
141 ssize_t cap1 = dnl.m_capacity;
142 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
143}
144
145BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture)
146{
147 LimitedIo limitedIo;
148
149 ssize_t cap0 = dnl.m_capacity;
150
151 const int RATE = DeadNonceList::INITIAL_CAPACITY * 3;
152 this->setRate(RATE);
153 limitedIo.defer(LIFETIME * 10);
154
155 ssize_t cap1 = dnl.m_capacity;
156 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
157}
158
159BOOST_AUTO_TEST_SUITE_END()
160
161} // namespace tests
162} // namespace nfd