Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 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 |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -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/>. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 25 | |
| 26 | #include "fw/ncc-strategy.hpp" |
| 27 | #include "strategy-tester.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 28 | #include "tests/daemon/face/dummy-face.hpp" |
| 29 | #include "tests/limited-io.hpp" |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 31 | #include "tests/test-common.hpp" |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 34 | namespace tests { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 35 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(FwNccStrategy, BaseFixture) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 37 | |
| 38 | // NccStrategy is fairly complex. |
| 39 | // The most important property is: |
| 40 | // it remembers which upstream is the fastest to return Data, |
| 41 | // and favors this upstream in subsequent Interests. |
| 42 | BOOST_AUTO_TEST_CASE(FavorRespondingUpstream) |
| 43 | { |
| 44 | LimitedIo limitedIo; |
| 45 | Forwarder forwarder; |
| 46 | typedef StrategyTester<fw::NccStrategy> NccStrategyTester; |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 47 | shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 48 | strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo); |
| 49 | |
| 50 | shared_ptr<DummyFace> face1 = make_shared<DummyFace>(); |
| 51 | shared_ptr<DummyFace> face2 = make_shared<DummyFace>(); |
| 52 | shared_ptr<DummyFace> face3 = make_shared<DummyFace>(); |
| 53 | forwarder.addFace(face1); |
| 54 | forwarder.addFace(face2); |
| 55 | forwarder.addFace(face3); |
| 56 | |
| 57 | Fib& fib = forwarder.getFib(); |
| 58 | shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 59 | fibEntry->addNextHop(face1, 10); |
| 60 | fibEntry->addNextHop(face2, 20); |
| 61 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 62 | StrategyChoice& strategyChoice = forwarder.getStrategyChoice(); |
| 63 | strategyChoice.install(strategy); |
| 64 | strategyChoice.insert(Name(), strategy->getName()); |
| 65 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 66 | Pit& pit = forwarder.getPit(); |
| 67 | |
| 68 | // first Interest: strategy knows nothing and follows routing |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 69 | shared_ptr<Interest> interest1p = makeInterest("ndn:/0Jm1ajrW/%00"); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 70 | Interest& interest1 = *interest1p; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 71 | interest1.setInterestLifetime(time::milliseconds(8000)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 72 | shared_ptr<pit::Entry> pitEntry1 = pit.insert(interest1).first; |
| 73 | |
| 74 | pitEntry1->insertOrUpdateInRecord(face3, interest1); |
| 75 | strategy->afterReceiveInterest(*face3, interest1, fibEntry, pitEntry1); |
| 76 | |
| 77 | // forwards to face1 because routing says it's best |
Junxiao Shi | 1553910 | 2014-10-08 22:39:51 -0700 | [diff] [blame^] | 78 | // (no io run here: afterReceiveInterest has already sent the Interest) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 79 | BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1); |
| 80 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1); |
| 81 | |
| 82 | // forwards to face2 because face1 doesn't respond |
Junxiao Shi | 1553910 | 2014-10-08 22:39:51 -0700 | [diff] [blame^] | 83 | limitedIo.run(1, time::milliseconds(500)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 84 | BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2); |
| 85 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2); |
| 86 | |
| 87 | // face2 responds |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 88 | shared_ptr<Data> data1p = makeData("ndn:/0Jm1ajrW/%00"); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 89 | Data& data1 = *data1p; |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 90 | strategy->beforeSatisfyInterest(pitEntry1, *face2, data1); |
Junxiao Shi | 1553910 | 2014-10-08 22:39:51 -0700 | [diff] [blame^] | 91 | limitedIo.defer(time::milliseconds(500)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 92 | |
| 93 | // second Interest: strategy knows face2 is best |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 94 | shared_ptr<Interest> interest2p = makeInterest("ndn:/0Jm1ajrW/%00%01"); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 95 | Interest& interest2 = *interest2p; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 96 | interest2.setInterestLifetime(time::milliseconds(8000)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 97 | shared_ptr<pit::Entry> pitEntry2 = pit.insert(interest2).first; |
| 98 | |
| 99 | pitEntry2->insertOrUpdateInRecord(face3, interest2); |
| 100 | strategy->afterReceiveInterest(*face3, interest2, fibEntry, pitEntry2); |
| 101 | |
| 102 | // forwards to face2 because it responds previously |
Junxiao Shi | 1553910 | 2014-10-08 22:39:51 -0700 | [diff] [blame^] | 103 | limitedIo.defer(time::milliseconds(1)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 104 | BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3); |
| 105 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face2); |
| 106 | } |
| 107 | |
Junxiao Shi | cbb490a | 2014-08-13 12:24:24 -0700 | [diff] [blame] | 108 | BOOST_AUTO_TEST_CASE(Bug1853) |
| 109 | { |
| 110 | LimitedIo limitedIo; |
| 111 | Forwarder forwarder; |
| 112 | typedef StrategyTester<fw::NccStrategy> NccStrategyTester; |
| 113 | shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder)); |
| 114 | strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo); |
| 115 | |
| 116 | shared_ptr<DummyFace> face1 = make_shared<DummyFace>(); |
| 117 | shared_ptr<DummyFace> face2 = make_shared<DummyFace>(); |
| 118 | shared_ptr<DummyFace> face3 = make_shared<DummyFace>(); |
| 119 | forwarder.addFace(face1); |
| 120 | forwarder.addFace(face2); |
| 121 | forwarder.addFace(face3); |
| 122 | |
| 123 | Fib& fib = forwarder.getFib(); |
| 124 | shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first; |
| 125 | fibEntry->addNextHop(face1, 10); |
| 126 | |
| 127 | StrategyChoice& strategyChoice = forwarder.getStrategyChoice(); |
| 128 | strategyChoice.install(strategy); |
| 129 | strategyChoice.insert(Name(), strategy->getName()); |
| 130 | |
| 131 | Pit& pit = forwarder.getPit(); |
| 132 | |
| 133 | // first Interest: strategy follows routing and forwards to face1 |
| 134 | shared_ptr<Interest> interest1 = makeInterest("ndn:/nztwIvHX/%00"); |
| 135 | interest1->setInterestLifetime(time::milliseconds(8000)); |
| 136 | shared_ptr<pit::Entry> pitEntry1 = pit.insert(*interest1).first; |
| 137 | |
| 138 | pitEntry1->insertOrUpdateInRecord(face3, *interest1); |
| 139 | strategy->afterReceiveInterest(*face3, *interest1, fibEntry, pitEntry1); |
| 140 | |
| 141 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1)); |
| 142 | BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1); |
| 143 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1); |
| 144 | |
| 145 | // face1 responds |
| 146 | shared_ptr<Data> data1 = makeData("ndn:/nztwIvHX/%00"); |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 147 | strategy->beforeSatisfyInterest(pitEntry1, *face1, *data1); |
Junxiao Shi | cbb490a | 2014-08-13 12:24:24 -0700 | [diff] [blame] | 148 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500)); |
| 149 | |
| 150 | // second Interest: bestFace is face1, nUpstreams becomes 0, |
| 151 | // therefore pitEntryInfo->maxInterval cannot be calculated from deferRange and nUpstreams |
| 152 | shared_ptr<Interest> interest2 = makeInterest("ndn:/nztwIvHX/%01"); |
| 153 | interest2->setInterestLifetime(time::milliseconds(8000)); |
| 154 | shared_ptr<pit::Entry> pitEntry2 = pit.insert(*interest2).first; |
| 155 | |
| 156 | pitEntry2->insertOrUpdateInRecord(face3, *interest2); |
| 157 | strategy->afterReceiveInterest(*face3, *interest2, fibEntry, pitEntry2); |
| 158 | |
| 159 | // FIB entry is changed before doPropagate executes |
| 160 | fibEntry->addNextHop(face2, 20); |
| 161 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1000));// should not crash |
| 162 | } |
| 163 | |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 164 | BOOST_AUTO_TEST_CASE(Bug1961) |
| 165 | { |
| 166 | LimitedIo limitedIo; |
| 167 | Forwarder forwarder; |
| 168 | typedef StrategyTester<fw::NccStrategy> NccStrategyTester; |
| 169 | shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder)); |
| 170 | strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo); |
| 171 | |
| 172 | shared_ptr<DummyFace> face1 = make_shared<DummyFace>(); |
| 173 | shared_ptr<DummyFace> face2 = make_shared<DummyFace>(); |
| 174 | shared_ptr<DummyFace> face3 = make_shared<DummyFace>(); |
| 175 | forwarder.addFace(face1); |
| 176 | forwarder.addFace(face2); |
| 177 | forwarder.addFace(face3); |
| 178 | |
| 179 | Fib& fib = forwarder.getFib(); |
| 180 | shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first; |
| 181 | fibEntry->addNextHop(face1, 10); |
| 182 | fibEntry->addNextHop(face2, 20); |
| 183 | |
| 184 | StrategyChoice& strategyChoice = forwarder.getStrategyChoice(); |
| 185 | strategyChoice.install(strategy); |
| 186 | strategyChoice.insert(Name(), strategy->getName()); |
| 187 | |
| 188 | Pit& pit = forwarder.getPit(); |
| 189 | |
| 190 | // first Interest: strategy forwards to face1 and face2 |
| 191 | shared_ptr<Interest> interest1 = makeInterest("ndn:/seRMz5a6/%00"); |
| 192 | interest1->setInterestLifetime(time::milliseconds(2000)); |
| 193 | shared_ptr<pit::Entry> pitEntry1 = pit.insert(*interest1).first; |
| 194 | |
| 195 | pitEntry1->insertOrUpdateInRecord(face3, *interest1); |
| 196 | strategy->afterReceiveInterest(*face3, *interest1, fibEntry, pitEntry1); |
| 197 | limitedIo.run(2 - strategy->m_sendInterestHistory.size(), time::milliseconds(2000)); |
| 198 | BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2); |
| 199 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1); |
| 200 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2); |
| 201 | |
| 202 | // face1 responds |
| 203 | shared_ptr<Data> data1 = makeData("ndn:/seRMz5a6/%00"); |
| 204 | strategy->beforeSatisfyInterest(pitEntry1, *face1, *data1); |
| 205 | pitEntry1->deleteInRecords(); |
| 206 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(10)); |
| 207 | // face2 also responds |
| 208 | strategy->beforeSatisfyInterest(pitEntry1, *face2, *data1); |
| 209 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(10)); |
| 210 | |
| 211 | // second Interest: bestFace should be face 1 |
| 212 | shared_ptr<Interest> interest2 = makeInterest("ndn:/seRMz5a6/%01"); |
| 213 | interest2->setInterestLifetime(time::milliseconds(2000)); |
| 214 | shared_ptr<pit::Entry> pitEntry2 = pit.insert(*interest2).first; |
| 215 | |
| 216 | pitEntry2->insertOrUpdateInRecord(face3, *interest2); |
| 217 | strategy->afterReceiveInterest(*face3, *interest2, fibEntry, pitEntry2); |
| 218 | limitedIo.run(3 - strategy->m_sendInterestHistory.size(), time::milliseconds(2000)); |
| 219 | |
| 220 | BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3); |
| 221 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face1); |
| 222 | } |
Junxiao Shi | cbb490a | 2014-08-13 12:24:24 -0700 | [diff] [blame] | 223 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 224 | BOOST_AUTO_TEST_SUITE_END() |
| 225 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 226 | } // namespace tests |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 227 | } // namespace nfd |