Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2012-2017 University of California, Los Angeles |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 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" |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 21 | #include "boost-test.hpp" |
| 22 | #include "../unit-test-time-fixture.hpp" |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 23 | |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 26 | namespace chronosync { |
| 27 | namespace test { |
| 28 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 29 | class InterestTableFixture : public ndn::tests::UnitTestTimeFixture |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 30 | { |
| 31 | public: |
| 32 | InterestTableFixture() |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 33 | { |
| 34 | uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04}; |
| 35 | Name prefix("/test/prefix"); |
| 36 | |
| 37 | Name interestName1; |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 38 | digest1 = ndn::util::Sha256::computeDigest(origin, 1); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 39 | interestName1.append(prefix).append(name::Component(digest1)); |
| 40 | interest1 = make_shared<Interest>(interestName1); |
| 41 | interest1->setInterestLifetime(time::milliseconds(100)); |
| 42 | |
| 43 | Name interestName2; |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 44 | digest2 = ndn::util::Sha256::computeDigest(origin, 2); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 45 | interestName2.append(prefix).append(name::Component(digest2)); |
| 46 | interest2 = make_shared<Interest>(interestName2); |
| 47 | interest2->setInterestLifetime(time::milliseconds(100)); |
| 48 | |
| 49 | Name interestName3; |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 50 | digest3 = ndn::util::Sha256::computeDigest(origin, 3); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 51 | 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 Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 64 | 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 Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture) |
| 75 | |
| 76 | BOOST_AUTO_TEST_CASE(Container) |
| 77 | { |
| 78 | InterestContainer container; |
| 79 | |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 80 | container.insert(make_shared<UnsatisfiedInterest>(interest1, digest1)); |
| 81 | container.insert(make_shared<UnsatisfiedInterest>(interest2, digest2)); |
| 82 | container.insert(make_shared<UnsatisfiedInterest>(interest3, digest3)); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 83 | |
| 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 | |
| 90 | BOOST_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 | |
| 119 | BOOST_AUTO_TEST_CASE(Expire) |
| 120 | { |
| 121 | InterestTable table(io); |
| 122 | |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 123 | insert(table, interest1, digest1); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 124 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 125 | advanceClocks(ndn::time::milliseconds(10), 10); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 127 | insert(table, interest2, digest2); |
| 128 | insert(table, interest3, digest3); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 129 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 130 | advanceClocks(ndn::time::milliseconds(10), 5); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 132 | insert(table, interest2, digest2); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 133 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 134 | advanceClocks(ndn::time::milliseconds(10), 2); |
| 135 | BOOST_CHECK_EQUAL(table.size(), 2); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 136 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 137 | advanceClocks(ndn::time::milliseconds(10), 5); |
| 138 | BOOST_CHECK_EQUAL(table.size(), 1); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 139 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 140 | advanceClocks(ndn::time::milliseconds(10), 15); |
| 141 | BOOST_CHECK_EQUAL(table.size(), 0); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | BOOST_AUTO_TEST_SUITE_END() |
| 145 | |
| 146 | } // namespace test |
| 147 | } // namespace chronosync |