blob: c8a59bb7c7082dfa248eed16c82a8c387fa118ad [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;
44
45 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), false);
46 dnl.add(nameA, nonce1);
47 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true);
48 BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false);
49 BOOST_CHECK_EQUAL(dnl.has(nameB, nonce1), false);
50}
51
52/// A Fixture that periodically inserts Nonces
53class PeriodicalInsertionFixture : public BaseFixture
54{
55protected:
56 PeriodicalInsertionFixture()
57 : dnl(LIFETIME)
58 , name("ndn:/N")
59 , addNonceBatch(0)
60 , addNonceInterval(LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT)
61 {
62 this->addNonce();
63 }
64
65 ~PeriodicalInsertionFixture()
66 {
67 scheduler::cancel(addNonceEvent);
68 }
69
70 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
83 scheduler::cancel(addNonceEvent); // avoid double schedules
84 if (addNonceInterval > time::nanoseconds::zero()) {
85 addNonceEvent = scheduler::schedule(addNonceInterval,
86 bind(&PeriodicalInsertionFixture::addNonce, this));
87 }
88 }
89
90protected:
91 static const time::nanoseconds LIFETIME;
92 DeadNonceList dnl;
93 Name name;
94 uint32_t lastNonce;
95 size_t addNonceBatch;
96 time::nanoseconds addNonceInterval;
97 scheduler::EventId addNonceEvent;
98};
99const time::nanoseconds PeriodicalInsertionFixture::LIFETIME = time::milliseconds(200);
100
101BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture)
102{
103 LimitedIo limitedIo;
104
105 const int RATE = DeadNonceList::INITIAL_CAPACITY / 2;
106 this->setRate(RATE);
107 limitedIo.defer(LIFETIME * 10);
108
109 Name nameC("ndn:/C");
110 const uint32_t nonceC = 0x25390656;
111 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
112 dnl.add(nameC, nonceC);
113 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
114
115 limitedIo.defer(LIFETIME / 2); // -50%, entry should exist
116 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true);
117
118 limitedIo.defer(LIFETIME); // +50%, entry should be gone
119 BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false);
120}
121
122BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture)
123{
124 LimitedIo limitedIo;
125
126 ssize_t cap0 = dnl.m_capacity;
127
128 const int RATE = DeadNonceList::INITIAL_CAPACITY / 3;
129 this->setRate(RATE);
130 limitedIo.defer(LIFETIME * 10);
131
132 ssize_t cap1 = dnl.m_capacity;
133 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
134}
135
136BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture)
137{
138 LimitedIo limitedIo;
139
140 ssize_t cap0 = dnl.m_capacity;
141
142 const int RATE = DeadNonceList::INITIAL_CAPACITY * 3;
143 this->setRate(RATE);
144 limitedIo.defer(LIFETIME * 10);
145
146 ssize_t cap1 = dnl.m_capacity;
147 BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE));
148}
149
150BOOST_AUTO_TEST_SUITE_END()
151
152} // namespace tests
153} // namespace nfd