Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "fw/ncc-strategy.hpp" |
| 8 | #include "strategy-tester.hpp" |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 9 | #include "tests/face/dummy-face.hpp" |
| 10 | #include "tests/core/limited-io.hpp" |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 11 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 12 | #include "tests/test-common.hpp" |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 13 | |
| 14 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 15 | namespace tests { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 16 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 17 | BOOST_FIXTURE_TEST_SUITE(FwNccStrategy, BaseFixture) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 18 | |
| 19 | // NccStrategy is fairly complex. |
| 20 | // The most important property is: |
| 21 | // it remembers which upstream is the fastest to return Data, |
| 22 | // and favors this upstream in subsequent Interests. |
| 23 | BOOST_AUTO_TEST_CASE(FavorRespondingUpstream) |
| 24 | { |
| 25 | LimitedIo limitedIo; |
| 26 | Forwarder forwarder; |
| 27 | typedef StrategyTester<fw::NccStrategy> NccStrategyTester; |
| 28 | shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(boost::ref(forwarder)); |
| 29 | strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo); |
| 30 | |
| 31 | shared_ptr<DummyFace> face1 = make_shared<DummyFace>(); |
| 32 | shared_ptr<DummyFace> face2 = make_shared<DummyFace>(); |
| 33 | shared_ptr<DummyFace> face3 = make_shared<DummyFace>(); |
| 34 | forwarder.addFace(face1); |
| 35 | forwarder.addFace(face2); |
| 36 | forwarder.addFace(face3); |
| 37 | |
| 38 | Fib& fib = forwarder.getFib(); |
| 39 | shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 40 | fibEntry->addNextHop(face1, 10); |
| 41 | fibEntry->addNextHop(face2, 20); |
| 42 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 43 | StrategyChoice& strategyChoice = forwarder.getStrategyChoice(); |
| 44 | strategyChoice.install(strategy); |
| 45 | strategyChoice.insert(Name(), strategy->getName()); |
| 46 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 47 | Pit& pit = forwarder.getPit(); |
| 48 | |
| 49 | // first Interest: strategy knows nothing and follows routing |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 50 | shared_ptr<Interest> interest1p = makeInterest("ndn:/0Jm1ajrW/%00"); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 51 | Interest& interest1 = *interest1p; |
| 52 | interest1.setInterestLifetime(8000); |
| 53 | shared_ptr<pit::Entry> pitEntry1 = pit.insert(interest1).first; |
| 54 | |
| 55 | pitEntry1->insertOrUpdateInRecord(face3, interest1); |
| 56 | strategy->afterReceiveInterest(*face3, interest1, fibEntry, pitEntry1); |
| 57 | |
| 58 | // forwards to face1 because routing says it's best |
| 59 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1)); |
| 60 | BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1); |
| 61 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1); |
| 62 | |
| 63 | // forwards to face2 because face1 doesn't respond |
| 64 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500)); |
| 65 | BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2); |
| 66 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2); |
| 67 | |
| 68 | // face2 responds |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 69 | shared_ptr<Data> data1p = makeData("ndn:/0Jm1ajrW/%00"); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 70 | Data& data1 = *data1p; |
| 71 | strategy->beforeSatisfyPendingInterest(pitEntry1, *face2, data1); |
| 72 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500)); |
| 73 | |
| 74 | // second Interest: strategy knows face2 is best |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 75 | shared_ptr<Interest> interest2p = makeInterest("ndn:/0Jm1ajrW/%00%01"); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 76 | Interest& interest2 = *interest2p; |
| 77 | interest2.setInterestLifetime(8000); |
| 78 | shared_ptr<pit::Entry> pitEntry2 = pit.insert(interest2).first; |
| 79 | |
| 80 | pitEntry2->insertOrUpdateInRecord(face3, interest2); |
| 81 | strategy->afterReceiveInterest(*face3, interest2, fibEntry, pitEntry2); |
| 82 | |
| 83 | // forwards to face2 because it responds previously |
| 84 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1)); |
| 85 | BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3); |
| 86 | BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face2); |
| 87 | } |
| 88 | |
| 89 | BOOST_AUTO_TEST_SUITE_END() |
| 90 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 91 | } // namespace tests |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 92 | } // namespace nfd |