Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 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 Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 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" |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 27 | #include "daemon/global.hpp" |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 28 | |
| 29 | #include "tests/test-common.hpp" |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace tests { |
| 33 | |
Junxiao Shi | 3978c92 | 2016-07-15 18:24:11 +0000 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Table) |
| 35 | BOOST_FIXTURE_TEST_SUITE(TestDeadNonceList, BaseFixture) |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 36 | |
| 37 | BOOST_AUTO_TEST_CASE(Basic) |
| 38 | { |
| 39 | Name nameA("ndn:/A"); |
| 40 | Name nameB("ndn:/B"); |
| 41 | const uint32_t nonce1 = 0x53b4eaa8; |
| 42 | const uint32_t nonce2 = 0x1f46372b; |
| 43 | |
| 44 | DeadNonceList dnl; |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(dnl.size(), 0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), false); |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 48 | dnl.add(nameA, nonce1); |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(dnl.size(), 1); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true); |
| 51 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false); |
| 52 | BOOST_CHECK_EQUAL(dnl.has(nameB, nonce1), false); |
| 53 | } |
| 54 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 55 | BOOST_AUTO_TEST_CASE(MinLifetime) |
| 56 | { |
| 57 | BOOST_CHECK_THROW(DeadNonceList dnl(time::milliseconds::zero()), std::invalid_argument); |
| 58 | } |
| 59 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 60 | /// A Fixture that periodically inserts Nonces |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 61 | class PeriodicalInsertionFixture : public UnitTestTimeFixture |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 62 | { |
| 63 | protected: |
| 64 | PeriodicalInsertionFixture() |
| 65 | : dnl(LIFETIME) |
| 66 | , name("ndn:/N") |
Junxiao Shi | 3978c92 | 2016-07-15 18:24:11 +0000 | [diff] [blame] | 67 | , lastNonce(0) |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 68 | , addNonceBatch(0) |
| 69 | , addNonceInterval(LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT) |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 70 | , timeUnit(addNonceInterval / 2) |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 71 | { |
| 72 | this->addNonce(); |
| 73 | } |
| 74 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 75 | void |
| 76 | setRate(size_t nNoncesPerLifetime) |
| 77 | { |
| 78 | addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | addNonce() |
| 83 | { |
| 84 | for (size_t i = 0; i < addNonceBatch; ++i) { |
| 85 | dnl.add(name, ++lastNonce); |
| 86 | } |
| 87 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 88 | if (addNonceInterval > time::nanoseconds::zero()) { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 89 | addNonceEvent = getScheduler().schedule(addNonceInterval, [this] { addNonce(); }); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 93 | /** \brief advance clocks by LIFETIME*t |
| 94 | */ |
| 95 | void |
| 96 | advanceClocksByLifetime(float t) |
| 97 | { |
| 98 | this->advanceClocks(timeUnit, time::duration_cast<time::nanoseconds>(LIFETIME * t)); |
| 99 | } |
| 100 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 101 | protected: |
| 102 | static const time::nanoseconds LIFETIME; |
| 103 | DeadNonceList dnl; |
| 104 | Name name; |
| 105 | uint32_t lastNonce; |
| 106 | size_t addNonceBatch; |
| 107 | time::nanoseconds addNonceInterval; |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 108 | time::nanoseconds timeUnit; |
| 109 | scheduler::ScopedEventId addNonceEvent; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 110 | }; |
| 111 | const time::nanoseconds PeriodicalInsertionFixture::LIFETIME = time::milliseconds(200); |
| 112 | |
| 113 | BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture) |
| 114 | { |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME); |
| 116 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 117 | const int RATE = DeadNonceList::INITIAL_CAPACITY / 2; |
| 118 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 119 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 120 | |
| 121 | Name nameC("ndn:/C"); |
| 122 | const uint32_t nonceC = 0x25390656; |
| 123 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false); |
| 124 | dnl.add(nameC, nonceC); |
| 125 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true); |
| 126 | |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 127 | this->advanceClocksByLifetime(0.5); // -50%, entry should exist |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true); |
| 129 | |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 130 | this->advanceClocksByLifetime(1.0); // +50%, entry should be gone |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false); |
| 132 | } |
| 133 | |
| 134 | BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture) |
| 135 | { |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 136 | ssize_t cap0 = dnl.m_capacity; |
| 137 | |
| 138 | const int RATE = DeadNonceList::INITIAL_CAPACITY / 3; |
| 139 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 140 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 141 | |
| 142 | ssize_t cap1 = dnl.m_capacity; |
| 143 | BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE)); |
| 144 | } |
| 145 | |
| 146 | BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture) |
| 147 | { |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 148 | ssize_t cap0 = dnl.m_capacity; |
| 149 | |
| 150 | const int RATE = DeadNonceList::INITIAL_CAPACITY * 3; |
| 151 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 152 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 153 | |
| 154 | ssize_t cap1 = dnl.m_capacity; |
| 155 | BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE)); |
| 156 | } |
| 157 | |
Junxiao Shi | 3978c92 | 2016-07-15 18:24:11 +0000 | [diff] [blame] | 158 | BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList |
| 159 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 160 | |
| 161 | } // namespace tests |
| 162 | } // namespace nfd |