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