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 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 56 | BOOST_AUTO_TEST_CASE(MinLifetime) |
| 57 | { |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame^] | 58 | BOOST_CHECK_THROW(DeadNonceList(0_ms), std::invalid_argument); |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [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() |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 66 | { |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 67 | addNonce(); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 70 | void |
| 71 | setRate(size_t nNoncesPerLifetime) |
| 72 | { |
| 73 | addNonceBatch = nNoncesPerLifetime / DeadNonceList::EXPECTED_MARK_COUNT; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | addNonce() |
| 78 | { |
| 79 | for (size_t i = 0; i < addNonceBatch; ++i) { |
| 80 | dnl.add(name, ++lastNonce); |
| 81 | } |
| 82 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame^] | 83 | addNonceEvent = getScheduler().schedule(ADD_INTERVAL, [this] { addNonce(); }); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 86 | /** \brief advance clocks by LIFETIME*t |
| 87 | */ |
| 88 | void |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame^] | 89 | advanceClocksByLifetime(double t) |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 90 | { |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame^] | 91 | advanceClocks(ADD_INTERVAL / 2, time::duration_cast<time::nanoseconds>(LIFETIME * t)); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 94 | protected: |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame^] | 95 | static constexpr time::nanoseconds LIFETIME = 200_ms; |
| 96 | static constexpr time::nanoseconds ADD_INTERVAL = LIFETIME / DeadNonceList::EXPECTED_MARK_COUNT; |
| 97 | |
| 98 | DeadNonceList dnl{LIFETIME}; |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 99 | Name name = "/N"; |
| 100 | uint32_t lastNonce = 0; |
| 101 | size_t addNonceBatch = 0; |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 102 | scheduler::ScopedEventId addNonceEvent; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 103 | }; |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 104 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame^] | 105 | const time::nanoseconds PeriodicalInsertionFixture::LIFETIME; |
| 106 | const time::nanoseconds PeriodicalInsertionFixture::ADD_INTERVAL; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 107 | |
| 108 | BOOST_FIXTURE_TEST_CASE(Lifetime, PeriodicalInsertionFixture) |
| 109 | { |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(dnl.getLifetime(), LIFETIME); |
| 111 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 112 | const int RATE = DeadNonceList::INITIAL_CAPACITY / 2; |
| 113 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 114 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 115 | |
| 116 | Name nameC("ndn:/C"); |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 117 | const Interest::Nonce nonceC(0x25390656); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false); |
| 119 | dnl.add(nameC, nonceC); |
| 120 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true); |
| 121 | |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 122 | this->advanceClocksByLifetime(0.5); // -50%, entry should exist |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), true); |
| 124 | |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 125 | this->advanceClocksByLifetime(1.0); // +50%, entry should be gone |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(dnl.has(nameC, nonceC), false); |
| 127 | } |
| 128 | |
| 129 | BOOST_FIXTURE_TEST_CASE(CapacityDown, PeriodicalInsertionFixture) |
| 130 | { |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 131 | ssize_t cap0 = dnl.m_capacity; |
| 132 | |
| 133 | const int RATE = DeadNonceList::INITIAL_CAPACITY / 3; |
| 134 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 135 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 136 | |
| 137 | ssize_t cap1 = dnl.m_capacity; |
| 138 | BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE)); |
| 139 | } |
| 140 | |
| 141 | BOOST_FIXTURE_TEST_CASE(CapacityUp, PeriodicalInsertionFixture) |
| 142 | { |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 143 | ssize_t cap0 = dnl.m_capacity; |
| 144 | |
| 145 | const int RATE = DeadNonceList::INITIAL_CAPACITY * 3; |
| 146 | this->setRate(RATE); |
Junxiao Shi | b283128 | 2015-01-09 14:55:58 -0700 | [diff] [blame] | 147 | this->advanceClocksByLifetime(10.0); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 148 | |
| 149 | ssize_t cap1 = dnl.m_capacity; |
| 150 | BOOST_CHECK_LT(std::abs(cap1 - RATE), std::abs(cap0 - RATE)); |
| 151 | } |
| 152 | |
Junxiao Shi | 3978c92 | 2016-07-15 18:24:11 +0000 | [diff] [blame] | 153 | BOOST_AUTO_TEST_SUITE_END() // TestDeadNonceList |
| 154 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 155 | |
| 156 | } // namespace tests |
| 157 | } // namespace nfd |