blob: 8f365d4eb67de522401ea419df4b5e15fb4b88d4 [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
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050026#include "table/cs-policy-priority-fifo.hpp"
Junxiao Shi0de23a22015-12-03 20:07:02 +000027#include "table/cs.hpp"
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050028#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;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050037
38BOOST_AUTO_TEST_SUITE(CsPriorityFifo)
39
40BOOST_FIXTURE_TEST_CASE(EvictOne, UnitTestTimeFixture)
41{
42 Cs cs(3);
Davide Pesavento01f8dba2015-09-19 21:11:45 +020043 cs.setPolicy(make_unique<PriorityFifoPolicy>());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050044
45 shared_ptr<Data> dataA = makeData("ndn:/A");
46 dataA->setFreshnessPeriod(time::milliseconds(99999));
47 dataA->wireEncode();
48 cs.insert(*dataA);
49
50 shared_ptr<Data> dataB = makeData("ndn:/B");
51 dataB->setFreshnessPeriod(time::milliseconds(10));
52 dataB->wireEncode();
53 cs.insert(*dataB);
54
55 shared_ptr<Data> dataC = makeData("ndn:/C");
56 dataC->setFreshnessPeriod(time::milliseconds(99999));
57 dataC->wireEncode();
58 cs.insert(*dataC, true);
59
60 this->advanceClocks(time::milliseconds(11));
61
62 // evict unsolicited
63 shared_ptr<Data> dataD = makeData("ndn:/D");
64 dataD->setFreshnessPeriod(time::milliseconds(99999));
65 dataD->wireEncode();
66 cs.insert(*dataD);
67 BOOST_CHECK_EQUAL(cs.size(), 3);
68 cs.find(Interest("ndn:/C"),
69 bind([] { BOOST_CHECK(false); }),
70 bind([] { BOOST_CHECK(true); }));
71
72 // evict stale
73 shared_ptr<Data> dataE = makeData("ndn:/E");
74 dataE->setFreshnessPeriod(time::milliseconds(99999));
75 dataE->wireEncode();
76 cs.insert(*dataE);
77 BOOST_CHECK_EQUAL(cs.size(), 3);
78 cs.find(Interest("ndn:/B"),
79 bind([] { BOOST_CHECK(false); }),
80 bind([] { BOOST_CHECK(true); }));
81
82 // evict fifo
83 shared_ptr<Data> dataF = makeData("ndn:/F");
84 dataF->setFreshnessPeriod(time::milliseconds(99999));
85 dataF->wireEncode();
86 cs.insert(*dataF);
87 BOOST_CHECK_EQUAL(cs.size(), 3);
88 cs.find(Interest("ndn:/A"),
89 bind([] { BOOST_CHECK(false); }),
90 bind([] { BOOST_CHECK(true); }));
91}
92
93BOOST_FIXTURE_TEST_CASE(Refresh, UnitTestTimeFixture)
94{
95 Cs cs(3);
Davide Pesavento01f8dba2015-09-19 21:11:45 +020096 cs.setPolicy(make_unique<PriorityFifoPolicy>());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050097
98 shared_ptr<Data> dataA = makeData("ndn:/A");
99 dataA->setFreshnessPeriod(time::milliseconds(99999));
100 dataA->wireEncode();
101 cs.insert(*dataA);
102
103 shared_ptr<Data> dataB = makeData("ndn:/B");
104 dataB->setFreshnessPeriod(time::milliseconds(10));
105 dataB->wireEncode();
106 cs.insert(*dataB);
107
108 shared_ptr<Data> dataC = makeData("ndn:/C");
109 dataC->setFreshnessPeriod(time::milliseconds(10));
110 dataC->wireEncode();
111 cs.insert(*dataC);
112
113 this->advanceClocks(time::milliseconds(11));
114
115 // refresh dataB
116 shared_ptr<Data> dataB2 = make_shared<Data>(*dataB);
117 dataB2->wireEncode();
118 cs.insert(*dataB2);
119 BOOST_CHECK_EQUAL(cs.size(), 3);
120 cs.find(Interest("ndn:/A"),
121 bind([] { BOOST_CHECK(true); }),
122 bind([] { BOOST_CHECK(false); }));
123
124 cs.find(Interest("ndn:/B"),
125 bind([] { BOOST_CHECK(true); }),
126 bind([] { BOOST_CHECK(false); }));
127
128 cs.find(Interest("ndn:/C"),
129 bind([] { BOOST_CHECK(true); }),
130 bind([] { BOOST_CHECK(false); }));
131
132 // evict dataC stale
133 shared_ptr<Data> dataD = makeData("ndn:/D");
134 dataD->setFreshnessPeriod(time::milliseconds(99999));
135 dataD->wireEncode();
136 cs.insert(*dataD);
137 BOOST_CHECK_EQUAL(cs.size(), 3);
138 cs.find(Interest("ndn:/C"),
139 bind([] { BOOST_CHECK(false); }),
140 bind([] { BOOST_CHECK(true); }));
141}
142
143BOOST_AUTO_TEST_SUITE_END()
144
145} // namespace tests
146} // namespace cs
147} // namespace nfd