Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 24 | |
| 25 | #include "fw/ncc-strategy.hpp" |
| 26 | #include "strategy-tester.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 27 | #include "tests/daemon/face/dummy-face.hpp" |
| 28 | #include "tests/limited-io.hpp" |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 29 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 30 | #include "tests/test-common.hpp" |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 33 | namespace tests { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 34 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(FwNccStrategy, BaseFixture) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 36 | |
| 37 | // NccStrategy is fairly complex. |
| 38 | // The most important property is: |
| 39 | // it remembers which upstream is the fastest to return Data, |
| 40 | // and favors this upstream in subsequent Interests. |
| 41 | BOOST_AUTO_TEST_CASE(FavorRespondingUpstream) |
| 42 | { |
| 43 | LimitedIo limitedIo; |
| 44 | Forwarder forwarder; |
| 45 | typedef StrategyTester<fw::NccStrategy> NccStrategyTester; |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 46 | shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 47 | strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo); |
| 48 | |
| 49 | shared_ptr<DummyFace> face1 = make_shared<DummyFace>(); |
| 50 | shared_ptr<DummyFace> face2 = make_shared<DummyFace>(); |
| 51 | shared_ptr<DummyFace> face3 = make_shared<DummyFace>(); |
| 52 | forwarder.addFace(face1); |
| 53 | forwarder.addFace(face2); |
| 54 | forwarder.addFace(face3); |
| 55 | |
| 56 | Fib& fib = forwarder.getFib(); |
| 57 | shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 58 | fibEntry->addNextHop(face1, 10); |
| 59 | fibEntry->addNextHop(face2, 20); |
| 60 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 61 | StrategyChoice& strategyChoice = forwarder.getStrategyChoice(); |
| 62 | strategyChoice.install(strategy); |
| 63 | strategyChoice.insert(Name(), strategy->getName()); |
| 64 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 65 | Pit& pit = forwarder.getPit(); |
| 66 | |
| 67 | // first Interest: strategy knows nothing and follows routing |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 68 | shared_ptr<Interest> interest1p = makeInterest("ndn:/0Jm1ajrW/%00"); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 69 | Interest& interest1 = *interest1p; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 70 | interest1.setInterestLifetime(time::milliseconds(8000)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 71 | shared_ptr<pit::Entry> pitEntry1 = pit.insert(interest1).first; |
| 72 | |
| 73 | pitEntry1->insertOrUpdateInRecord(face3, interest1); |
| 74 | strategy->afterReceiveInterest(*face3, interest1, fibEntry, pitEntry1); |
| 75 | |
| 76 | // forwards to face1 because routing says it's best |
| 77 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1)); |
| 78 | BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1); |
| 79 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1); |
| 80 | |
| 81 | // forwards to face2 because face1 doesn't respond |
| 82 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500)); |
| 83 | BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2); |
| 84 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2); |
| 85 | |
| 86 | // face2 responds |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 87 | shared_ptr<Data> data1p = makeData("ndn:/0Jm1ajrW/%00"); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 88 | Data& data1 = *data1p; |
| 89 | strategy->beforeSatisfyPendingInterest(pitEntry1, *face2, data1); |
| 90 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500)); |
| 91 | |
| 92 | // second Interest: strategy knows face2 is best |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 93 | shared_ptr<Interest> interest2p = makeInterest("ndn:/0Jm1ajrW/%00%01"); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 94 | Interest& interest2 = *interest2p; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 95 | interest2.setInterestLifetime(time::milliseconds(8000)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 96 | shared_ptr<pit::Entry> pitEntry2 = pit.insert(interest2).first; |
| 97 | |
| 98 | pitEntry2->insertOrUpdateInRecord(face3, interest2); |
| 99 | strategy->afterReceiveInterest(*face3, interest2, fibEntry, pitEntry2); |
| 100 | |
| 101 | // forwards to face2 because it responds previously |
| 102 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1)); |
| 103 | BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3); |
| 104 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face2); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_SUITE_END() |
| 108 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 109 | } // namespace tests |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 110 | } // namespace nfd |