Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [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. |
| 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 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 26 | #include "table/cs-policy-priority-fifo.hpp" |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 27 | #include "table/cs.hpp" |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/util/crypto.hpp> |
| 29 | |
| 30 | #include "tests/test-common.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace cs { |
| 34 | namespace tests { |
| 35 | |
| 36 | using namespace nfd::tests; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 37 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(Table) |
| 39 | BOOST_AUTO_TEST_SUITE(TestCsPriorityFifo) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 40 | |
| 41 | BOOST_FIXTURE_TEST_CASE(EvictOne, UnitTestTimeFixture) |
| 42 | { |
| 43 | Cs cs(3); |
Davide Pesavento | 01f8dba | 2015-09-19 21:11:45 +0200 | [diff] [blame] | 44 | cs.setPolicy(make_unique<PriorityFifoPolicy>()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 45 | |
| 46 | shared_ptr<Data> dataA = makeData("ndn:/A"); |
| 47 | dataA->setFreshnessPeriod(time::milliseconds(99999)); |
| 48 | dataA->wireEncode(); |
| 49 | cs.insert(*dataA); |
| 50 | |
| 51 | shared_ptr<Data> dataB = makeData("ndn:/B"); |
| 52 | dataB->setFreshnessPeriod(time::milliseconds(10)); |
| 53 | dataB->wireEncode(); |
| 54 | cs.insert(*dataB); |
| 55 | |
| 56 | shared_ptr<Data> dataC = makeData("ndn:/C"); |
| 57 | dataC->setFreshnessPeriod(time::milliseconds(99999)); |
| 58 | dataC->wireEncode(); |
| 59 | cs.insert(*dataC, true); |
| 60 | |
| 61 | this->advanceClocks(time::milliseconds(11)); |
| 62 | |
| 63 | // evict unsolicited |
| 64 | shared_ptr<Data> dataD = makeData("ndn:/D"); |
| 65 | dataD->setFreshnessPeriod(time::milliseconds(99999)); |
| 66 | dataD->wireEncode(); |
| 67 | cs.insert(*dataD); |
| 68 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 69 | cs.find(Interest("ndn:/C"), |
| 70 | bind([] { BOOST_CHECK(false); }), |
| 71 | bind([] { BOOST_CHECK(true); })); |
| 72 | |
| 73 | // evict stale |
| 74 | shared_ptr<Data> dataE = makeData("ndn:/E"); |
| 75 | dataE->setFreshnessPeriod(time::milliseconds(99999)); |
| 76 | dataE->wireEncode(); |
| 77 | cs.insert(*dataE); |
| 78 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 79 | cs.find(Interest("ndn:/B"), |
| 80 | bind([] { BOOST_CHECK(false); }), |
| 81 | bind([] { BOOST_CHECK(true); })); |
| 82 | |
| 83 | // evict fifo |
| 84 | shared_ptr<Data> dataF = makeData("ndn:/F"); |
| 85 | dataF->setFreshnessPeriod(time::milliseconds(99999)); |
| 86 | dataF->wireEncode(); |
| 87 | cs.insert(*dataF); |
| 88 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 89 | cs.find(Interest("ndn:/A"), |
| 90 | bind([] { BOOST_CHECK(false); }), |
| 91 | bind([] { BOOST_CHECK(true); })); |
| 92 | } |
| 93 | |
| 94 | BOOST_FIXTURE_TEST_CASE(Refresh, UnitTestTimeFixture) |
| 95 | { |
| 96 | Cs cs(3); |
Davide Pesavento | 01f8dba | 2015-09-19 21:11:45 +0200 | [diff] [blame] | 97 | cs.setPolicy(make_unique<PriorityFifoPolicy>()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 98 | |
| 99 | shared_ptr<Data> dataA = makeData("ndn:/A"); |
| 100 | dataA->setFreshnessPeriod(time::milliseconds(99999)); |
| 101 | dataA->wireEncode(); |
| 102 | cs.insert(*dataA); |
| 103 | |
| 104 | shared_ptr<Data> dataB = makeData("ndn:/B"); |
| 105 | dataB->setFreshnessPeriod(time::milliseconds(10)); |
| 106 | dataB->wireEncode(); |
| 107 | cs.insert(*dataB); |
| 108 | |
| 109 | shared_ptr<Data> dataC = makeData("ndn:/C"); |
| 110 | dataC->setFreshnessPeriod(time::milliseconds(10)); |
| 111 | dataC->wireEncode(); |
| 112 | cs.insert(*dataC); |
| 113 | |
| 114 | this->advanceClocks(time::milliseconds(11)); |
| 115 | |
| 116 | // refresh dataB |
| 117 | shared_ptr<Data> dataB2 = make_shared<Data>(*dataB); |
| 118 | dataB2->wireEncode(); |
| 119 | cs.insert(*dataB2); |
| 120 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 121 | cs.find(Interest("ndn:/A"), |
| 122 | bind([] { BOOST_CHECK(true); }), |
| 123 | bind([] { BOOST_CHECK(false); })); |
| 124 | |
| 125 | cs.find(Interest("ndn:/B"), |
| 126 | bind([] { BOOST_CHECK(true); }), |
| 127 | bind([] { BOOST_CHECK(false); })); |
| 128 | |
| 129 | cs.find(Interest("ndn:/C"), |
| 130 | bind([] { BOOST_CHECK(true); }), |
| 131 | bind([] { BOOST_CHECK(false); })); |
| 132 | |
| 133 | // evict dataC stale |
| 134 | shared_ptr<Data> dataD = makeData("ndn:/D"); |
| 135 | dataD->setFreshnessPeriod(time::milliseconds(99999)); |
| 136 | dataD->wireEncode(); |
| 137 | cs.insert(*dataD); |
| 138 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 139 | cs.find(Interest("ndn:/C"), |
| 140 | bind([] { BOOST_CHECK(false); }), |
| 141 | bind([] { BOOST_CHECK(true); })); |
| 142 | } |
| 143 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 144 | BOOST_AUTO_TEST_SUITE_END() // TestCsPriorityFifo |
| 145 | BOOST_AUTO_TEST_SUITE_END() // Table |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 146 | |
| 147 | } // namespace tests |
| 148 | } // namespace cs |
| 149 | } // namespace nfd |