blob: 2709913cbe1375c6c04392d89b8972edba7460aa [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;
40 fibEntry->setStrategy(strategy);
41 fibEntry->addNextHop(face1, 10);
42 fibEntry->addNextHop(face2, 20);
43
44 Pit& pit = forwarder.getPit();
45
46 // first Interest: strategy knows nothing and follows routing
47 shared_ptr<Interest> interest1p = make_shared<Interest>("ndn:/0Jm1ajrW/%00");
48 Interest& interest1 = *interest1p;
49 interest1.setInterestLifetime(8000);
50 shared_ptr<pit::Entry> pitEntry1 = pit.insert(interest1).first;
51
52 pitEntry1->insertOrUpdateInRecord(face3, interest1);
53 strategy->afterReceiveInterest(*face3, interest1, fibEntry, pitEntry1);
54
55 // forwards to face1 because routing says it's best
56 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1));
57 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1);
58 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
59
60 // forwards to face2 because face1 doesn't respond
61 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500));
62 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2);
63 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2);
64
65 // face2 responds
66 shared_ptr<Data> data1p = make_shared<Data>("ndn:/0Jm1ajrW/%00");
67 Data& data1 = *data1p;
68 strategy->beforeSatisfyPendingInterest(pitEntry1, *face2, data1);
69 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500));
70
71 // second Interest: strategy knows face2 is best
72 shared_ptr<Interest> interest2p = make_shared<Interest>("ndn:/0Jm1ajrW/%00%01");
73 Interest& interest2 = *interest2p;
74 interest2.setInterestLifetime(8000);
75 shared_ptr<pit::Entry> pitEntry2 = pit.insert(interest2).first;
76
77 pitEntry2->insertOrUpdateInRecord(face3, interest2);
78 strategy->afterReceiveInterest(*face3, interest2, fibEntry, pitEntry2);
79
80 // forwards to face2 because it responds previously
81 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1));
82 BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3);
83 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face2);
84}
85
86BOOST_AUTO_TEST_SUITE_END()
87
Junxiao Shid9ee45c2014-02-27 15:38:11 -070088} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070089} // namespace nfd