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 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tests { |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | 3978c92 | 2016-07-15 18:24:11 +0000 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(TestDeadNonceList, GlobalIoFixture) |
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"); |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 41 | const Interest::Nonce nonce1(0x53b4eaa8); |
| 42 | const Interest::Nonce nonce2(0x1f46372b); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 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 | |
Varun Patil | 67ad70f | 2021-06-04 10:04:00 -0700 | [diff] [blame] | 55 | BOOST_AUTO_TEST_CASE(NoDuplicates) |
| 56 | { |
| 57 | Name nameA("ndn:/A"); |
| 58 | const Interest::Nonce nonce1(0x53b4eaa8); |
| 59 | const Interest::Nonce nonce2(0x63b4eaa8); |
| 60 | const Interest::Nonce nonce3(0x73b4eaa8); |
| 61 | const Interest::Nonce nonce4(0x83b4eaa8); |
| 62 | const Interest::Nonce nonce5(0x93b4eaa8); |
| 63 | |
| 64 | DeadNonceList dnl; |
| 65 | BOOST_CHECK_EQUAL(dnl.size(), 0); |
| 66 | |
| 67 | dnl.m_capacity = 4; |
| 68 | dnl.add(nameA, nonce1); |
| 69 | dnl.add(nameA, nonce2); |
| 70 | dnl.add(nameA, nonce3); |
| 71 | dnl.add(nameA, nonce4); |
| 72 | BOOST_CHECK_EQUAL(dnl.size(), 4); |
| 73 | |
| 74 | dnl.add(nameA, nonce1); |
| 75 | BOOST_CHECK_EQUAL(dnl.size(), 4); |
| 76 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true); |
| 77 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), true); |
| 78 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce3), true); |
| 79 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce4), true); |
| 80 | |
| 81 | dnl.add(nameA, nonce5); |
| 82 | BOOST_CHECK_EQUAL(dnl.size(), 4); |
| 83 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true); |
| 84 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false); |
| 85 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce3), true); |
| 86 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce4), true); |
| 87 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce5), true); |
| 88 | |
| 89 | dnl.add(nameA, nonce5); |
| 90 | BOOST_CHECK_EQUAL(dnl.size(), 4); |
| 91 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce1), true); |
| 92 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce2), false); |
| 93 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce3), true); |
| 94 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce4), true); |
| 95 | BOOST_CHECK_EQUAL(dnl.has(nameA, nonce5), true); |
| 96 | } |
| 97 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(MinLifetime) |
| 99 | { |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 100 | BOOST_CHECK_THROW(DeadNonceList(0_ms), std::invalid_argument); |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 103 | /// A fixture that periodically inserts Nonces |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 104 | class PeriodicalInsertionFixture : public GlobalIoTimeFixture |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 105 | { |
| 106 | protected: |
| 107 | PeriodicalInsertionFixture() |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 108 | { |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 109 | addNonce(); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 112 | void |
| 113 | setRate(size_t nNoncesPerLifetime) |
| 114 | { |
| 115 | addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT; |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | addNonce() |
| 120 | { |
| 121 | for (size_t i = 0; i < addNonceBatch; ++i) { |
| 122 | dnl.add(name, ++lastNonce); |
| 123 | } |
| 124 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 125 | addNonceEvent = getScheduler().schedule(ADD_INTERVAL, [this] { addNonce(); }); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 128 | /** \brief Advance clocks by `LIFETIME*t`. |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 129 | */ |
| 130 | void |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 131 | advanceClocksByLifetime(double t) |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 132 | { |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 133 | advanceClocks(ADD_INTERVAL / 2, time::duration_cast<time::nanoseconds>(LIFETIME * t)); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 136 | protected: |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 137 | static constexpr time::nanoseconds LIFETIME = 200_ms; |
| 138 | static constexpr time::nanoseconds ADD_INTERVAL = LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT; |
| 139 | |
| 140 | DeadNonceList dnl{LIFETIME}; |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 141 | Name name = "/N"; |
| 142 | uint32_t lastNonce = 0; |
| 143 | size_t addNonceBatch = 0; |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 144 | scheduler::ScopedEventId addNonceEvent; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 145 | }; |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 146 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 147 | BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture) |
| 148 | { |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 149 | BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME); |
| 150 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 151 | const int RATE = DeadNonceList::INITIAL_CAPACITY / 2; |
| 152 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 153 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 154 | |
| 155 | Name nameC("ndn:/C"); |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 156 | const Interest::Nonce nonceC(0x25390656); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false); |
| 158 | dnl.add(nameC, nonceC); |
| 159 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true); |
| 160 | |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 161 | this->advanceClocksByLifetime(0.5); // -50%, entry should exist |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true); |
| 163 | |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 164 | this->advanceClocksByLifetime(1.0); // +50%, entry should be gone |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false); |
| 166 | } |
| 167 | |
| 168 | BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture) |
| 169 | { |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 170 | ssize_t cap0 = dnl.m_capacity; |
| 171 | |
| 172 | const int RATE = DeadNonceList::INITIAL_CAPACITY / 3; |
| 173 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 174 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 175 | |
| 176 | ssize_t cap1 = dnl.m_capacity; |
| 177 | BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE)); |
| 178 | } |
| 179 | |
| 180 | BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture) |
| 181 | { |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 182 | ssize_t cap0 = dnl.m_capacity; |
| 183 | |
| 184 | const int RATE = DeadNonceList::INITIAL_CAPACITY * 3; |
| 185 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 186 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 187 | |
| 188 | ssize_t cap1 = dnl.m_capacity; |
| 189 | BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE)); |
| 190 | } |
| 191 | |
Junxiao Shi | 3978c92 | 2016-07-15 18:24:11 +0000 | [diff] [blame] | 192 | BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList |
| 193 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 194 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 195 | } // namespace nfd::tests |