blob: 8fd174609649e85eecc60ec74b435941e55bf9ec [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, 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 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "table/cs.hpp"
27#include "table/cs-policy-priority-fifo.hpp"
28#include <ndn-cxx/util/crypto.hpp>
29
30#include "tests/test-common.hpp"
31
32namespace nfd {
33namespace cs {
34namespace tests {
35
36using namespace nfd::tests;
37using ndn::nfd::LocalControlHeader;
38
39BOOST_AUTO_TEST_SUITE(CsPriorityFifo)
40
41BOOST_FIXTURE_TEST_CASE(EvictOne, UnitTestTimeFixture)
42{
43 Cs cs(3);
Davide Pesavento01f8dba2015-09-19 21:11:45 +020044 cs.setPolicy(make_unique<PriorityFifoPolicy>());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050045
46 shared_ptr<Data> dataA = makeData("ndn:/A");
47 dataA->setFreshnessPeriod(time::milliseconds(99999));
48 dataA->wireEncode();
49 cs.insert(*dataA);
50
51 shared_ptr<Data> dataB = makeData("ndn:/B");
52 dataB->setFreshnessPeriod(time::milliseconds(10));
53 dataB->wireEncode();
54 cs.insert(*dataB);
55
56 shared_ptr<Data> dataC = makeData("ndn:/C");
57 dataC->setFreshnessPeriod(time::milliseconds(99999));
58 dataC->wireEncode();
59 cs.insert(*dataC, true);
60
61 this->advanceClocks(time::milliseconds(11));
62
63 // evict unsolicited
64 shared_ptr<Data> dataD = makeData("ndn:/D");
65 dataD->setFreshnessPeriod(time::milliseconds(99999));
66 dataD->wireEncode();
67 cs.insert(*dataD);
68 BOOST_CHECK_EQUAL(cs.size(), 3);
69 cs.find(Interest("ndn:/C"),
70 bind([] { BOOST_CHECK(false); }),
71 bind([] { BOOST_CHECK(true); }));
72
73 // evict stale
74 shared_ptr<Data> dataE = makeData("ndn:/E");
75 dataE->setFreshnessPeriod(time::milliseconds(99999));
76 dataE->wireEncode();
77 cs.insert(*dataE);
78 BOOST_CHECK_EQUAL(cs.size(), 3);
79 cs.find(Interest("ndn:/B"),
80 bind([] { BOOST_CHECK(false); }),
81 bind([] { BOOST_CHECK(true); }));
82
83 // evict fifo
84 shared_ptr<Data> dataF = makeData("ndn:/F");
85 dataF->setFreshnessPeriod(time::milliseconds(99999));
86 dataF->wireEncode();
87 cs.insert(*dataF);
88 BOOST_CHECK_EQUAL(cs.size(), 3);
89 cs.find(Interest("ndn:/A"),
90 bind([] { BOOST_CHECK(false); }),
91 bind([] { BOOST_CHECK(true); }));
92}
93
94BOOST_FIXTURE_TEST_CASE(Refresh, UnitTestTimeFixture)
95{
96 Cs cs(3);
Davide Pesavento01f8dba2015-09-19 21:11:45 +020097 cs.setPolicy(make_unique<PriorityFifoPolicy>());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050098
99 shared_ptr<Data> dataA = makeData("ndn:/A");
100 dataA->setFreshnessPeriod(time::milliseconds(99999));
101 dataA->wireEncode();
102 cs.insert(*dataA);
103
104 shared_ptr<Data> dataB = makeData("ndn:/B");
105 dataB->setFreshnessPeriod(time::milliseconds(10));
106 dataB->wireEncode();
107 cs.insert(*dataB);
108
109 shared_ptr<Data> dataC = makeData("ndn:/C");
110 dataC->setFreshnessPeriod(time::milliseconds(10));
111 dataC->wireEncode();
112 cs.insert(*dataC);
113
114 this->advanceClocks(time::milliseconds(11));
115
116 // refresh dataB
117 shared_ptr<Data> dataB2 = make_shared<Data>(*dataB);
118 dataB2->wireEncode();
119 cs.insert(*dataB2);
120 BOOST_CHECK_EQUAL(cs.size(), 3);
121 cs.find(Interest("ndn:/A"),
122 bind([] { BOOST_CHECK(true); }),
123 bind([] { BOOST_CHECK(false); }));
124
125 cs.find(Interest("ndn:/B"),
126 bind([] { BOOST_CHECK(true); }),
127 bind([] { BOOST_CHECK(false); }));
128
129 cs.find(Interest("ndn:/C"),
130 bind([] { BOOST_CHECK(true); }),
131 bind([] { BOOST_CHECK(false); }));
132
133 // evict dataC stale
134 shared_ptr<Data> dataD = makeData("ndn:/D");
135 dataD->setFreshnessPeriod(time::milliseconds(99999));
136 dataD->wireEncode();
137 cs.insert(*dataD);
138 BOOST_CHECK_EQUAL(cs.size(), 3);
139 cs.find(Interest("ndn:/C"),
140 bind([] { BOOST_CHECK(false); }),
141 bind([] { BOOST_CHECK(true); }));
142}
143
144BOOST_AUTO_TEST_SUITE_END()
145
146} // namespace tests
147} // namespace cs
148} // namespace nfd