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" |
| 21 | |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 24 | #include "../unit-test-time-fixture.hpp" |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
| 26 | |
| 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; |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame^] | 39 | digest1 = ndn::crypto::computeSha256Digest(origin, 1); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 40 | interestName1.append(prefix).append(name::Component(digest1)); |
| 41 | interest1 = make_shared<Interest>(interestName1); |
| 42 | interest1->setInterestLifetime(time::milliseconds(100)); |
| 43 | |
| 44 | Name interestName2; |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame^] | 45 | digest2 = ndn::crypto::computeSha256Digest(origin, 2); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 46 | interestName2.append(prefix).append(name::Component(digest2)); |
| 47 | interest2 = make_shared<Interest>(interestName2); |
| 48 | interest2->setInterestLifetime(time::milliseconds(100)); |
| 49 | |
| 50 | Name interestName3; |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame^] | 51 | digest3 = ndn::crypto::computeSha256Digest(origin, 3); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 52 | 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 Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 65 | 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 Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture) |
| 76 | |
| 77 | BOOST_AUTO_TEST_CASE(Container) |
| 78 | { |
| 79 | InterestContainer container; |
| 80 | |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 81 | container.insert(make_shared<UnsatisfiedInterest>(interest1, digest1)); |
| 82 | container.insert(make_shared<UnsatisfiedInterest>(interest2, digest2)); |
| 83 | container.insert(make_shared<UnsatisfiedInterest>(interest3, digest3)); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 84 | |
| 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 | |
| 91 | BOOST_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 | |
| 120 | BOOST_AUTO_TEST_CASE(Expire) |
| 121 | { |
| 122 | InterestTable table(io); |
| 123 | |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame^] | 124 | insert(table, interest1, digest1); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 125 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 126 | advanceClocks(ndn::time::milliseconds(10), 10); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 127 | |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame^] | 128 | insert(table, interest2, digest2); |
| 129 | insert(table, interest3, digest3); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 130 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 131 | advanceClocks(ndn::time::milliseconds(10), 5); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame^] | 133 | insert(table, interest2, digest2); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 134 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 135 | advanceClocks(ndn::time::milliseconds(10), 2); |
| 136 | BOOST_CHECK_EQUAL(table.size(), 2); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 137 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 138 | advanceClocks(ndn::time::milliseconds(10), 5); |
| 139 | BOOST_CHECK_EQUAL(table.size(), 1); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 140 | |
Qiuhan Ding | d71a08a | 2014-12-30 18:56:15 -0800 | [diff] [blame] | 141 | advanceClocks(ndn::time::milliseconds(10), 15); |
| 142 | BOOST_CHECK_EQUAL(table.size(), 0); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | BOOST_AUTO_TEST_SUITE_END() |
| 146 | |
| 147 | } // namespace test |
| 148 | } // namespace chronosync |