Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Ashlesh Gawande | 5a89547 | 2020-01-25 18:07:32 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, The University of Memphis |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 4 | * |
| 5 | * This file is part of PSync. |
| 6 | * See AUTHORS.md for complete list of PSync authors and contributors. |
| 7 | * |
| 8 | * PSync is free software: you can redistribute it and/or modify it under the terms |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 9 | * of the GNU Lesser General Public License as published by the Free Software Foundation, |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * PSync 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 |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 14 | * PURPOSE. See the GNU Lesser General Public License for more details. |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 15 | * |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public License along with |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 17 | * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
Ashlesh Gawande | 78b94ad | 2018-12-13 15:29:19 -0600 | [diff] [blame] | 20 | #include "PSync/segment-publisher.hpp" |
| 21 | #include "PSync/detail/state.hpp" |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 22 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 23 | #include "tests/boost-test.hpp" |
| 24 | #include "tests/unit-test-time-fixture.hpp" |
| 25 | |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 26 | #include <ndn-cxx/data.hpp> |
| 27 | #include <ndn-cxx/interest.hpp> |
| 28 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 29 | #include <ndn-cxx/util/segment-fetcher.hpp> |
| 30 | #include <ndn-cxx/security/validator-null.hpp> |
| 31 | |
| 32 | namespace psync { |
| 33 | |
| 34 | using namespace ndn; |
| 35 | |
| 36 | class SegmentPublisherFixture : public tests::UnitTestTimeFixture |
| 37 | { |
| 38 | public: |
| 39 | SegmentPublisherFixture() |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 40 | : face(io, util::DummyClientFace::Options{true, true}) |
| 41 | , publisher(face, keyChain) |
| 42 | , freshness(1000) |
| 43 | , numComplete(0) |
| 44 | , numRepliesFromStore(0) |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 45 | { |
| 46 | face.setInterestFilter(InterestFilter("/hello/world"), |
| 47 | bind(&SegmentPublisherFixture::onInterest, this, _1, _2), |
| 48 | [] (const ndn::Name& prefix, const std::string& msg) { |
| 49 | BOOST_CHECK(false); |
| 50 | }); |
| 51 | advanceClocks(ndn::time::milliseconds(10)); |
| 52 | |
| 53 | for (int i = 0; i < 1000; ++i) { |
| 54 | state.addContent(Name("/test").appendNumber(i)); |
| 55 | } |
| 56 | } |
| 57 | |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 58 | ~SegmentPublisherFixture() |
| 59 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 60 | fetcher->stop(); |
| 61 | } |
| 62 | |
| 63 | void |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 64 | expressInterest(const Interest& interest) |
| 65 | { |
| 66 | fetcher = util::SegmentFetcher::start(face, interest, ndn::security::getAcceptAllValidator()); |
| 67 | fetcher->onComplete.connect([this] (auto&&...) { numComplete++; }); |
| 68 | fetcher->onError.connect([] (auto&&...) { BOOST_CHECK(false); }); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 69 | |
| 70 | advanceClocks(ndn::time::milliseconds(10)); |
| 71 | } |
| 72 | |
| 73 | void |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 74 | onInterest(const Name& prefix, const Interest& interest) |
| 75 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 76 | if (publisher.replyFromStore(interest.getName())) { |
| 77 | numRepliesFromStore++; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // If dataName is same as interest name |
| 82 | if (dataName.empty()) { |
| 83 | publisher.publish(interest.getName(), interest.getName(), state.wireEncode(), freshness); |
| 84 | } |
| 85 | else { |
| 86 | publisher.publish(interest.getName(), dataName, state.wireEncode(), freshness); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | util::DummyClientFace face; |
| 91 | KeyChain keyChain; |
| 92 | SegmentPublisher publisher; |
| 93 | shared_ptr<util::SegmentFetcher> fetcher; |
| 94 | Name dataName; |
| 95 | time::milliseconds freshness; |
| 96 | State state; |
| 97 | |
| 98 | int numComplete; |
| 99 | int numRepliesFromStore; |
| 100 | }; |
| 101 | |
| 102 | BOOST_FIXTURE_TEST_SUITE(TestSegmentPublisher, SegmentPublisherFixture) |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE(Basic) |
| 105 | { |
| 106 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 107 | expressInterest(Interest("/hello/world")); |
| 108 | BOOST_CHECK_EQUAL(numComplete, 1); |
| 109 | // First segment is answered directly in publish, |
| 110 | // Rest two are satisfied by the store |
| 111 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 112 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3); |
| 113 | |
| 114 | numRepliesFromStore = 0; |
| 115 | expressInterest(Interest("/hello/world")); |
| 116 | BOOST_CHECK_EQUAL(numComplete, 2); |
| 117 | BOOST_CHECK_EQUAL(numRepliesFromStore, 3); |
| 118 | |
| 119 | advanceClocks(ndn::time::milliseconds(freshness)); |
| 120 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 121 | |
| 122 | numRepliesFromStore = 0; |
| 123 | expressInterest(Interest("/hello/world")); |
| 124 | BOOST_CHECK_EQUAL(numComplete, 3); |
| 125 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 126 | |
| 127 | numRepliesFromStore = 0; |
Junxiao Shi | 3426baf | 2019-01-13 23:19:23 +0000 | [diff] [blame] | 128 | face.expressInterest(Interest("/hello/world/").setCanBePrefix(true), |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 129 | [this] (const Interest& interest, const Data& data) { |
| 130 | numComplete++; |
| 131 | }, |
| 132 | [] (const Interest& interest, const lp::Nack& nack) { |
| 133 | BOOST_CHECK(false); |
| 134 | }, |
| 135 | [] (const Interest& interest) { |
| 136 | BOOST_CHECK(false); |
| 137 | }); |
| 138 | advanceClocks(ndn::time::milliseconds(10)); |
| 139 | BOOST_CHECK_EQUAL(numComplete, 4); |
| 140 | BOOST_CHECK_EQUAL(numRepliesFromStore, 1); |
| 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_CASE(LargerDataName) |
| 144 | { |
| 145 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 146 | dataName = Name("/hello/world/IBF"); |
| 147 | |
| 148 | expressInterest(Interest("/hello/world")); |
| 149 | BOOST_CHECK_EQUAL(numComplete, 1); |
| 150 | // First segment is answered directly in publish, |
| 151 | // Rest two are satisfied by the store |
| 152 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 153 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3); |
| 154 | |
| 155 | advanceClocks(ndn::time::milliseconds(freshness)); |
| 156 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_SUITE_END() |
| 160 | |
Junxiao Shi | 3426baf | 2019-01-13 23:19:23 +0000 | [diff] [blame] | 161 | } // namespace psync |