blob: 9e1f7bc681f1aca7e6c136135408f938ae243b93 [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 Shi0b5fbbb2014-02-20 15:54:03 -070024
25#include "fw/ncc-strategy.hpp"
26#include "strategy-tester.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070027#include "tests/daemon/face/dummy-face.hpp"
28#include "tests/limited-io.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070029
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030#include "tests/test-common.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070031
32namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070034
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035BOOST_FIXTURE_TEST_SUITE(FwNccStrategy, BaseFixture)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070036
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.
41BOOST_AUTO_TEST_CASE(FavorRespondingUpstream)
42{
43 LimitedIo limitedIo;
44 Forwarder forwarder;
45 typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
Alexander Afanasyevf6980282014-05-13 18:28:40 -070046 shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070047 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 Shi0b5fbbb2014-02-20 15:54:03 -070058 fibEntry->addNextHop(face1, 10);
59 fibEntry->addNextHop(face2, 20);
60
Junxiao Shi7bb01512014-03-05 21:34:09 -070061 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
62 strategyChoice.install(strategy);
63 strategyChoice.insert(Name(), strategy->getName());
64
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070065 Pit& pit = forwarder.getPit();
66
67 // first Interest: strategy knows nothing and follows routing
Junxiao Shif3c07812014-03-11 21:48:49 -070068 shared_ptr<Interest> interest1p = makeInterest("ndn:/0Jm1ajrW/%00");
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070069 Interest& interest1 = *interest1p;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070070 interest1.setInterestLifetime(time::milliseconds(8000));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070071 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 Shif3c07812014-03-11 21:48:49 -070087 shared_ptr<Data> data1p = makeData("ndn:/0Jm1ajrW/%00");
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070088 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 Shif3c07812014-03-11 21:48:49 -070093 shared_ptr<Interest> interest2p = makeInterest("ndn:/0Jm1ajrW/%00%01");
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070094 Interest& interest2 = *interest2p;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070095 interest2.setInterestLifetime(time::milliseconds(8000));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070096 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
107BOOST_AUTO_TEST_SUITE_END()
108
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700109} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700110} // namespace nfd