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 | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, 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 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 148 | const time::nanoseconds PeriodicalInsertionFixture::LIFETIME; |
| 149 | const time::nanoseconds PeriodicalInsertionFixture::ADD_INTERVAL; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 150 | |
| 151 | BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture) |
| 152 | { |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 153 | BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME); |
| 154 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 155 | const int RATE = DeadNonceList::INITIAL_CAPACITY / 2; |
| 156 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 157 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 158 | |
| 159 | Name nameC("ndn:/C"); |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 160 | const Interest::Nonce nonceC(0x25390656); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false); |
| 162 | dnl.add(nameC, nonceC); |
| 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(0.5); // -50%, entry should exist |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true); |
| 167 | |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 168 | this->advanceClocksByLifetime(1.0); // +50%, entry should be gone |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false); |
| 170 | } |
| 171 | |
| 172 | BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture) |
| 173 | { |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 174 | ssize_t cap0 = dnl.m_capacity; |
| 175 | |
| 176 | const int RATE = DeadNonceList::INITIAL_CAPACITY / 3; |
| 177 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 178 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 179 | |
| 180 | ssize_t cap1 = dnl.m_capacity; |
| 181 | BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE)); |
| 182 | } |
| 183 | |
| 184 | BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture) |
| 185 | { |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 186 | ssize_t cap0 = dnl.m_capacity; |
| 187 | |
| 188 | const int RATE = DeadNonceList::INITIAL_CAPACITY * 3; |
| 189 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 190 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 191 | |
| 192 | ssize_t cap1 = dnl.m_capacity; |
| 193 | BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE)); |
| 194 | } |
| 195 | |
Junxiao Shi | 3978c92 | 2016-07-15 18:24:11 +0000 | [diff] [blame] | 196 | BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList |
| 197 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 198 | |
| 199 | } // namespace tests |
| 200 | } // namespace nfd |