blob: a258b830db17dfe21b0ecdd0ed479e3510fde167 [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"
9#include "../face/dummy-face.hpp"
10#include "../core/limited-io.hpp"
11
12#include <boost/test/unit_test.hpp>
13
14namespace nfd {
15
16BOOST_AUTO_TEST_SUITE(FwNccStrategy)
17
18// NccStrategy is fairly complex.
19// The most important property is:
20// it remembers which upstream is the fastest to return Data,
21// and favors this upstream in subsequent Interests.
22BOOST_AUTO_TEST_CASE(FavorRespondingUpstream)
23{
24 LimitedIo limitedIo;
25 Forwarder forwarder;
26 typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
27 shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(boost::ref(forwarder));
28 strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo);
29
30 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
31 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
32 shared_ptr<DummyFace> face3 = make_shared<DummyFace>();
33 forwarder.addFace(face1);
34 forwarder.addFace(face2);
35 forwarder.addFace(face3);
36
37 Fib& fib = forwarder.getFib();
38 shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
39 fibEntry->setStrategy(strategy);
40 fibEntry->addNextHop(face1, 10);
41 fibEntry->addNextHop(face2, 20);
42
43 Pit& pit = forwarder.getPit();
44
45 // first Interest: strategy knows nothing and follows routing
46 shared_ptr<Interest> interest1p = make_shared<Interest>("ndn:/0Jm1ajrW/%00");
47 Interest& interest1 = *interest1p;
48 interest1.setInterestLifetime(8000);
49 shared_ptr<pit::Entry> pitEntry1 = pit.insert(interest1).first;
50
51 pitEntry1->insertOrUpdateInRecord(face3, interest1);
52 strategy->afterReceiveInterest(*face3, interest1, fibEntry, pitEntry1);
53
54 // forwards to face1 because routing says it's best
55 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1));
56 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1);
57 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
58
59 // forwards to face2 because face1 doesn't respond
60 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500));
61 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2);
62 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2);
63
64 // face2 responds
65 shared_ptr<Data> data1p = make_shared<Data>("ndn:/0Jm1ajrW/%00");
66 Data& data1 = *data1p;
67 strategy->beforeSatisfyPendingInterest(pitEntry1, *face2, data1);
68 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500));
69
70 // second Interest: strategy knows face2 is best
71 shared_ptr<Interest> interest2p = make_shared<Interest>("ndn:/0Jm1ajrW/%00%01");
72 Interest& interest2 = *interest2p;
73 interest2.setInterestLifetime(8000);
74 shared_ptr<pit::Entry> pitEntry2 = pit.insert(interest2).first;
75
76 pitEntry2->insertOrUpdateInRecord(face3, interest2);
77 strategy->afterReceiveInterest(*face3, interest2, fibEntry, pitEntry2);
78
79 // forwards to face2 because it responds previously
80 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1));
81 BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3);
82 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face2);
83}
84
85BOOST_AUTO_TEST_SUITE_END()
86
87} // namespace nfd