blob: be0f289dd19b026e0c1ab2d0c9ca796467d72714 [file] [log] [blame]
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -08003 * Copyright (c) 2012-2017 University of California, Los Angeles
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -07004 *
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"
Ashlesh Gawande687cf922017-05-30 15:04:16 -050021#include "boost-test.hpp"
22#include "../unit-test-time-fixture.hpp"
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070023
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070024#include <unistd.h>
25
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070026namespace chronosync {
27namespace test {
28
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080029class InterestTableFixture : public ndn::tests::UnitTestTimeFixture
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070030{
31public:
32 InterestTableFixture()
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070033 {
34 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
35 Name prefix("/test/prefix");
36
37 Name interestName1;
Ashlesh Gawande687cf922017-05-30 15:04:16 -050038 digest1 = ndn::util::Sha256::computeDigest(origin, 1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070039 interestName1.append(prefix).append(name::Component(digest1));
Ashlesh Gawande08784d42017-09-06 23:40:21 -050040
41 interest1 = Interest(interestName1);
42 interest1.setInterestLifetime(time::milliseconds(100));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070043
44 Name interestName2;
Ashlesh Gawande687cf922017-05-30 15:04:16 -050045 digest2 = ndn::util::Sha256::computeDigest(origin, 2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070046 interestName2.append(prefix).append(name::Component(digest2));
Ashlesh Gawande08784d42017-09-06 23:40:21 -050047 interest2 = Interest(interestName2);
48 interest2.setInterestLifetime(time::milliseconds(100));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070049
50 Name interestName3;
Ashlesh Gawande687cf922017-05-30 15:04:16 -050051 digest3 = ndn::util::Sha256::computeDigest(origin, 3);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070052 interestName3.append(prefix).append(name::Component(digest3));
Ashlesh Gawande08784d42017-09-06 23:40:21 -050053 interest3 = Interest(interestName3);
54 interest3.setInterestLifetime(time::milliseconds(100));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070055 }
56
57 void
Ashlesh Gawande08784d42017-09-06 23:40:21 -050058 insert(InterestTable& table, const Interest& interest, ndn::ConstBufferPtr digest)
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070059 {
60 table.insert(interest, digest);
61 }
62
Ashlesh Gawande08784d42017-09-06 23:40:21 -050063 Interest interest1;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070064 ndn::ConstBufferPtr digest1;
65
Ashlesh Gawande08784d42017-09-06 23:40:21 -050066 Interest interest2;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070067 ndn::ConstBufferPtr digest2;
68
Ashlesh Gawande08784d42017-09-06 23:40:21 -050069 Interest interest3;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070070 ndn::ConstBufferPtr digest3;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070071};
72
73BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture)
74
75BOOST_AUTO_TEST_CASE(Container)
76{
77 InterestContainer container;
78
Yingdi Yu906c2ea2014-10-31 11:24:50 -070079 container.insert(make_shared<UnsatisfiedInterest>(interest1, digest1));
80 container.insert(make_shared<UnsatisfiedInterest>(interest2, digest2));
81 container.insert(make_shared<UnsatisfiedInterest>(interest3, digest3));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070082
83 BOOST_CHECK_EQUAL(container.size(), 3);
84 BOOST_CHECK(container.find(digest3) != container.end());
85 BOOST_CHECK(container.find(digest2) != container.end());
86 BOOST_CHECK(container.find(digest1) != container.end());
87}
88
89BOOST_AUTO_TEST_CASE(Basic)
90{
91 InterestTable table(io);
92
93 table.insert(interest1, digest1);
94 table.insert(interest2, digest2);
95 table.insert(interest3, digest3);
96
97 BOOST_CHECK_EQUAL(table.size(), 3);
98 InterestTable::const_iterator it = table.begin();
99 BOOST_CHECK(it != table.end());
100 it++;
101 BOOST_CHECK(it != table.end());
102 it++;
103 BOOST_CHECK(it != table.end());
104 it++;
105 BOOST_CHECK(it == table.end());
106
107 BOOST_CHECK_EQUAL(table.size(), 3);
108 table.erase(digest1);
109 BOOST_CHECK_EQUAL(table.size(), 2);
110 table.erase(digest2);
111 BOOST_CHECK_EQUAL(table.size(), 1);
112 ConstUnsatisfiedInterestPtr pendingInterest = *table.begin();
113 table.clear();
114 BOOST_CHECK_EQUAL(table.size(), 0);
115 BOOST_CHECK(*pendingInterest->digest == *digest3);
116}
117
118BOOST_AUTO_TEST_CASE(Expire)
119{
120 InterestTable table(io);
121
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800122 insert(table, interest1, digest1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700123
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800124 advanceClocks(ndn::time::milliseconds(10), 10);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700125
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800126 insert(table, interest2, digest2);
127 insert(table, interest3, digest3);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700128
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800129 advanceClocks(ndn::time::milliseconds(10), 5);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700130
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800131 insert(table, interest2, digest2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700132
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800133 advanceClocks(ndn::time::milliseconds(10), 2);
134 BOOST_CHECK_EQUAL(table.size(), 2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700135
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800136 advanceClocks(ndn::time::milliseconds(10), 5);
137 BOOST_CHECK_EQUAL(table.size(), 1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700138
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800139 advanceClocks(ndn::time::milliseconds(10), 15);
140 BOOST_CHECK_EQUAL(table.size(), 0);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700141}
142
143BOOST_AUTO_TEST_SUITE_END()
144
145} // namespace test
146} // namespace chronosync