blob: 3a312d4505146e7f7a9fb4f5b3eba7254435f88c [file] [log] [blame]
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2014 University of California, Los Angeles
4 *
5 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
7 *
8 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "interest-table.hpp"
21
22#include <ndn-cxx/util/scheduler.hpp>
23#include <unistd.h>
24
25#include "boost-test.hpp"
26
27namespace chronosync {
28namespace test {
29
30class InterestTableFixture
31{
32public:
33 InterestTableFixture()
34 : scheduler(io)
35 {
36 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
37 Name prefix("/test/prefix");
38
39 Name interestName1;
40 digest1 = ndn::crypto::sha256(origin, 1);
41 interestName1.append(prefix).append(name::Component(digest1));
42 interest1 = make_shared<Interest>(interestName1);
43 interest1->setInterestLifetime(time::milliseconds(100));
44
45 Name interestName2;
46 digest2 = ndn::crypto::sha256(origin, 2);
47 interestName2.append(prefix).append(name::Component(digest2));
48 interest2 = make_shared<Interest>(interestName2);
49 interest2->setInterestLifetime(time::milliseconds(100));
50
51 Name interestName3;
52 digest3 = ndn::crypto::sha256(origin, 3);
53 interestName3.append(prefix).append(name::Component(digest3));
54 interest3 = make_shared<Interest>(interestName3);
55 interest3->setInterestLifetime(time::milliseconds(100));
56 }
57
58 void
59 insert(InterestTable& table,
60 shared_ptr<Interest> interest,
61 ndn::ConstBufferPtr digest)
62 {
63 table.insert(interest, digest);
64 }
65
66 void
67 check(InterestTable& table, size_t size)
68 {
69 BOOST_CHECK_EQUAL(table.size(), size);
70 }
71
72 void
73 terminate()
74 {
75 io.stop();
76 }
77
78 shared_ptr<Interest> interest1;
79 ndn::ConstBufferPtr digest1;
80
81 shared_ptr<Interest> interest2;
82 ndn::ConstBufferPtr digest2;
83
84 shared_ptr<Interest> interest3;
85 ndn::ConstBufferPtr digest3;
86
87 boost::asio::io_service io;
88 ndn::Scheduler scheduler;
89};
90
91BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture)
92
93BOOST_AUTO_TEST_CASE(Container)
94{
95 InterestContainer container;
96
97 container.insert(boost::make_shared<UnsatisfiedInterest>(interest1, digest1));
98 container.insert(boost::make_shared<UnsatisfiedInterest>(interest2, digest2));
99 container.insert(boost::make_shared<UnsatisfiedInterest>(interest3, digest3));
100
101 BOOST_CHECK_EQUAL(container.size(), 3);
102 BOOST_CHECK(container.find(digest3) != container.end());
103 BOOST_CHECK(container.find(digest2) != container.end());
104 BOOST_CHECK(container.find(digest1) != container.end());
105}
106
107BOOST_AUTO_TEST_CASE(Basic)
108{
109 InterestTable table(io);
110
111 table.insert(interest1, digest1);
112 table.insert(interest2, digest2);
113 table.insert(interest3, digest3);
114
115 BOOST_CHECK_EQUAL(table.size(), 3);
116 InterestTable::const_iterator it = table.begin();
117 BOOST_CHECK(it != table.end());
118 it++;
119 BOOST_CHECK(it != table.end());
120 it++;
121 BOOST_CHECK(it != table.end());
122 it++;
123 BOOST_CHECK(it == table.end());
124
125 BOOST_CHECK_EQUAL(table.size(), 3);
126 table.erase(digest1);
127 BOOST_CHECK_EQUAL(table.size(), 2);
128 table.erase(digest2);
129 BOOST_CHECK_EQUAL(table.size(), 1);
130 ConstUnsatisfiedInterestPtr pendingInterest = *table.begin();
131 table.clear();
132 BOOST_CHECK_EQUAL(table.size(), 0);
133 BOOST_CHECK(*pendingInterest->digest == *digest3);
134}
135
136BOOST_AUTO_TEST_CASE(Expire)
137{
138 InterestTable table(io);
139
140 scheduler.scheduleEvent(ndn::time::milliseconds(50),
141 ndn::bind(&InterestTableFixture::insert, this,
142 boost::ref(table), interest1, digest1));
143
144 scheduler.scheduleEvent(ndn::time::milliseconds(150),
145 ndn::bind(&InterestTableFixture::insert, this,
146 boost::ref(table), interest2, digest2));
147
148 scheduler.scheduleEvent(ndn::time::milliseconds(150),
149 ndn::bind(&InterestTableFixture::insert, this,
150 boost::ref(table), interest3, digest3));
151
152 scheduler.scheduleEvent(ndn::time::milliseconds(200),
153 ndn::bind(&InterestTableFixture::insert, this,
154 boost::ref(table), interest2, digest2));
155
156 scheduler.scheduleEvent(ndn::time::milliseconds(220),
157 ndn::bind(&InterestTableFixture::check, this,
158 boost::ref(table), 2));
159
160 scheduler.scheduleEvent(ndn::time::milliseconds(270),
161 ndn::bind(&InterestTableFixture::check, this,
162 boost::ref(table), 1));
163
164 scheduler.scheduleEvent(ndn::time::milliseconds(420),
165 ndn::bind(&InterestTableFixture::check, this,
166 boost::ref(table), 0));
167
168 scheduler.scheduleEvent(ndn::time::milliseconds(500),
169 ndn::bind(&InterestTableFixture::terminate, this));
170
171 io.run();
172}
173
174BOOST_AUTO_TEST_SUITE_END()
175
176} // namespace test
177} // namespace chronosync