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