blob: 0f0c95dc75496f9479712a018c50dcc01be63994 [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));
40 interest1 = make_shared<Interest>(interestName1);
41 interest1->setInterestLifetime(time::milliseconds(100));
42
43 Name interestName2;
Ashlesh Gawande687cf922017-05-30 15:04:16 -050044 digest2 = ndn::util::Sha256::computeDigest(origin, 2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070045 interestName2.append(prefix).append(name::Component(digest2));
46 interest2 = make_shared<Interest>(interestName2);
47 interest2->setInterestLifetime(time::milliseconds(100));
48
49 Name interestName3;
Ashlesh Gawande687cf922017-05-30 15:04:16 -050050 digest3 = ndn::util::Sha256::computeDigest(origin, 3);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070051 interestName3.append(prefix).append(name::Component(digest3));
52 interest3 = make_shared<Interest>(interestName3);
53 interest3->setInterestLifetime(time::milliseconds(100));
54 }
55
56 void
57 insert(InterestTable& table,
58 shared_ptr<Interest> interest,
59 ndn::ConstBufferPtr digest)
60 {
61 table.insert(interest, digest);
62 }
63
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070064 shared_ptr<Interest> interest1;
65 ndn::ConstBufferPtr digest1;
66
67 shared_ptr<Interest> interest2;
68 ndn::ConstBufferPtr digest2;
69
70 shared_ptr<Interest> interest3;
71 ndn::ConstBufferPtr digest3;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070072};
73
74BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture)
75
76BOOST_AUTO_TEST_CASE(Container)
77{
78 InterestContainer container;
79
Yingdi Yu906c2ea2014-10-31 11:24:50 -070080 container.insert(make_shared<UnsatisfiedInterest>(interest1, digest1));
81 container.insert(make_shared<UnsatisfiedInterest>(interest2, digest2));
82 container.insert(make_shared<UnsatisfiedInterest>(interest3, digest3));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070083
84 BOOST_CHECK_EQUAL(container.size(), 3);
85 BOOST_CHECK(container.find(digest3) != container.end());
86 BOOST_CHECK(container.find(digest2) != container.end());
87 BOOST_CHECK(container.find(digest1) != container.end());
88}
89
90BOOST_AUTO_TEST_CASE(Basic)
91{
92 InterestTable table(io);
93
94 table.insert(interest1, digest1);
95 table.insert(interest2, digest2);
96 table.insert(interest3, digest3);
97
98 BOOST_CHECK_EQUAL(table.size(), 3);
99 InterestTable::const_iterator it = table.begin();
100 BOOST_CHECK(it != table.end());
101 it++;
102 BOOST_CHECK(it != table.end());
103 it++;
104 BOOST_CHECK(it != table.end());
105 it++;
106 BOOST_CHECK(it == table.end());
107
108 BOOST_CHECK_EQUAL(table.size(), 3);
109 table.erase(digest1);
110 BOOST_CHECK_EQUAL(table.size(), 2);
111 table.erase(digest2);
112 BOOST_CHECK_EQUAL(table.size(), 1);
113 ConstUnsatisfiedInterestPtr pendingInterest = *table.begin();
114 table.clear();
115 BOOST_CHECK_EQUAL(table.size(), 0);
116 BOOST_CHECK(*pendingInterest->digest == *digest3);
117}
118
119BOOST_AUTO_TEST_CASE(Expire)
120{
121 InterestTable table(io);
122
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800123 insert(table, interest1, digest1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700124
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800125 advanceClocks(ndn::time::milliseconds(10), 10);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700126
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800127 insert(table, interest2, digest2);
128 insert(table, interest3, digest3);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700129
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800130 advanceClocks(ndn::time::milliseconds(10), 5);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700131
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800132 insert(table, interest2, digest2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700133
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800134 advanceClocks(ndn::time::milliseconds(10), 2);
135 BOOST_CHECK_EQUAL(table.size(), 2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700136
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800137 advanceClocks(ndn::time::milliseconds(10), 5);
138 BOOST_CHECK_EQUAL(table.size(), 1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700139
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800140 advanceClocks(ndn::time::milliseconds(10), 15);
141 BOOST_CHECK_EQUAL(table.size(), 0);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700142}
143
144BOOST_AUTO_TEST_SUITE_END()
145
146} // namespace test
147} // namespace chronosync