blob: 8b1f1569ac20b620330e2d44194c5dce233021b3 [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"
21
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070022#include <unistd.h>
23
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080024#include "../unit-test-time-fixture.hpp"
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070025#include "boost-test.hpp"
26
27namespace chronosync {
28namespace test {
29
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080030class InterestTableFixture : public ndn::tests::UnitTestTimeFixture
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070031{
32public:
33 InterestTableFixture()
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070034 {
35 uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
36 Name prefix("/test/prefix");
37
38 Name interestName1;
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -080039 digest1 = ndn::crypto::computeSha256Digest(origin, 1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070040 interestName1.append(prefix).append(name::Component(digest1));
41 interest1 = make_shared<Interest>(interestName1);
42 interest1->setInterestLifetime(time::milliseconds(100));
43
44 Name interestName2;
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -080045 digest2 = ndn::crypto::computeSha256Digest(origin, 2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070046 interestName2.append(prefix).append(name::Component(digest2));
47 interest2 = make_shared<Interest>(interestName2);
48 interest2->setInterestLifetime(time::milliseconds(100));
49
50 Name interestName3;
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -080051 digest3 = ndn::crypto::computeSha256Digest(origin, 3);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070052 interestName3.append(prefix).append(name::Component(digest3));
53 interest3 = make_shared<Interest>(interestName3);
54 interest3->setInterestLifetime(time::milliseconds(100));
55 }
56
57 void
58 insert(InterestTable& table,
59 shared_ptr<Interest> interest,
60 ndn::ConstBufferPtr digest)
61 {
62 table.insert(interest, digest);
63 }
64
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070065 shared_ptr<Interest> interest1;
66 ndn::ConstBufferPtr digest1;
67
68 shared_ptr<Interest> interest2;
69 ndn::ConstBufferPtr digest2;
70
71 shared_ptr<Interest> interest3;
72 ndn::ConstBufferPtr digest3;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070073};
74
75BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture)
76
77BOOST_AUTO_TEST_CASE(Container)
78{
79 InterestContainer container;
80
Yingdi Yu906c2ea2014-10-31 11:24:50 -070081 container.insert(make_shared<UnsatisfiedInterest>(interest1, digest1));
82 container.insert(make_shared<UnsatisfiedInterest>(interest2, digest2));
83 container.insert(make_shared<UnsatisfiedInterest>(interest3, digest3));
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070084
85 BOOST_CHECK_EQUAL(container.size(), 3);
86 BOOST_CHECK(container.find(digest3) != container.end());
87 BOOST_CHECK(container.find(digest2) != container.end());
88 BOOST_CHECK(container.find(digest1) != container.end());
89}
90
91BOOST_AUTO_TEST_CASE(Basic)
92{
93 InterestTable table(io);
94
95 table.insert(interest1, digest1);
96 table.insert(interest2, digest2);
97 table.insert(interest3, digest3);
98
99 BOOST_CHECK_EQUAL(table.size(), 3);
100 InterestTable::const_iterator it = table.begin();
101 BOOST_CHECK(it != table.end());
102 it++;
103 BOOST_CHECK(it != table.end());
104 it++;
105 BOOST_CHECK(it != table.end());
106 it++;
107 BOOST_CHECK(it == table.end());
108
109 BOOST_CHECK_EQUAL(table.size(), 3);
110 table.erase(digest1);
111 BOOST_CHECK_EQUAL(table.size(), 2);
112 table.erase(digest2);
113 BOOST_CHECK_EQUAL(table.size(), 1);
114 ConstUnsatisfiedInterestPtr pendingInterest = *table.begin();
115 table.clear();
116 BOOST_CHECK_EQUAL(table.size(), 0);
117 BOOST_CHECK(*pendingInterest->digest == *digest3);
118}
119
120BOOST_AUTO_TEST_CASE(Expire)
121{
122 InterestTable table(io);
123
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800124 insert(table, interest1, digest1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700125
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800126 advanceClocks(ndn::time::milliseconds(10), 10);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700127
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800128 insert(table, interest2, digest2);
129 insert(table, interest3, digest3);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700130
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800131 advanceClocks(ndn::time::milliseconds(10), 5);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700132
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800133 insert(table, interest2, digest2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700134
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800135 advanceClocks(ndn::time::milliseconds(10), 2);
136 BOOST_CHECK_EQUAL(table.size(), 2);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700137
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800138 advanceClocks(ndn::time::milliseconds(10), 5);
139 BOOST_CHECK_EQUAL(table.size(), 1);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700140
Qiuhan Dingd71a08a2014-12-30 18:56:15 -0800141 advanceClocks(ndn::time::milliseconds(10), 15);
142 BOOST_CHECK_EQUAL(table.size(), 0);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700143}
144
145BOOST_AUTO_TEST_SUITE_END()
146
147} // namespace test
148} // namespace chronosync