blob: bb3f64038a42a3e0e079a43d8a77e439b809b545 [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- 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 Shid9ee45c2014-02-27 15:38:11 -07009#include "tests/face/dummy-face.hpp"
10#include "tests/core/limited-io.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070011
Junxiao Shid9ee45c2014-02-27 15:38:11 -070012#include "tests/test-common.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070013
14namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070015namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070016
Junxiao Shid9ee45c2014-02-27 15:38:11 -070017BOOST_FIXTURE_TEST_SUITE(FwNccStrategy, BaseFixture)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070018
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.
23BOOST_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 Shi0b5fbbb2014-02-20 15:54:03 -070040 fibEntry->addNextHop(face1, 10);
41 fibEntry->addNextHop(face2, 20);
42
Junxiao Shi7bb01512014-03-05 21:34:09 -070043 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
44 strategyChoice.install(strategy);
45 strategyChoice.insert(Name(), strategy->getName());
46
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070047 Pit& pit = forwarder.getPit();
48
49 // first Interest: strategy knows nothing and follows routing
50 shared_ptr<Interest> interest1p = make_shared<Interest>("ndn:/0Jm1ajrW/%00");
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
69 shared_ptr<Data> data1p = make_shared<Data>("ndn:/0Jm1ajrW/%00");
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
75 shared_ptr<Interest> interest2p = make_shared<Interest>("ndn:/0Jm1ajrW/%00%01");
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
89BOOST_AUTO_TEST_SUITE_END()
90
Junxiao Shid9ee45c2014-02-27 15:38:11 -070091} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070092} // namespace nfd