Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 3 | * Copyright (c) 2012-2019 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" |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 21 | |
| 22 | #include "tests/boost-test.hpp" |
| 23 | #include "tests/unit-test-time-fixture.hpp" |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 24 | |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 27 | namespace chronosync { |
| 28 | namespace test { |
| 29 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 30 | class InterestTableFixture : public ndn::tests::UnitTestTimeFixture |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 31 | { |
| 32 | public: |
| 33 | InterestTableFixture() |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 34 | { |
| 35 | uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04}; |
| 36 | Name prefix("/test/prefix"); |
| 37 | |
| 38 | Name interestName1; |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 39 | digest1 = ndn::util::Sha256::computeDigest(origin, 1); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 40 | interestName1.append(prefix).append(name::Component(digest1)); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 41 | |
| 42 | interest1 = Interest(interestName1); |
| 43 | interest1.setInterestLifetime(time::milliseconds(100)); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 44 | |
| 45 | Name interestName2; |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 46 | digest2 = ndn::util::Sha256::computeDigest(origin, 2); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 47 | interestName2.append(prefix).append(name::Component(digest2)); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 48 | interest2 = Interest(interestName2); |
| 49 | interest2.setInterestLifetime(time::milliseconds(100)); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 50 | |
| 51 | Name interestName3; |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 52 | digest3 = ndn::util::Sha256::computeDigest(origin, 3); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 53 | interestName3.append(prefix).append(name::Component(digest3)); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 54 | interest3 = Interest(interestName3); |
| 55 | interest3.setInterestLifetime(time::milliseconds(100)); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 59 | insert(InterestTable& table, const Interest& interest, ndn::ConstBufferPtr digest) |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 60 | { |
| 61 | table.insert(interest, digest); |
| 62 | } |
| 63 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 64 | Interest interest1; |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 65 | ndn::ConstBufferPtr digest1; |
| 66 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 67 | Interest interest2; |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 68 | ndn::ConstBufferPtr digest2; |
| 69 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 70 | Interest interest3; |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 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 |