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" |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 24 | #include "tests/io-fixture.hpp" |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 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 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 36 | class SegmentPublisherFixture : public tests::IoFixture |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 37 | { |
| 38 | public: |
| 39 | SegmentPublisherFixture() |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 40 | : face(m_io, util::DummyClientFace::Options{true, true}) |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 41 | , publisher(face, keyChain) |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 42 | { |
| 43 | face.setInterestFilter(InterestFilter("/hello/world"), |
| 44 | bind(&SegmentPublisherFixture::onInterest, this, _1, _2), |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 45 | [] (auto&&...) { BOOST_CHECK(false); }); |
| 46 | advanceClocks(10_ms); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 47 | |
| 48 | for (int i = 0; i < 1000; ++i) { |
| 49 | state.addContent(Name("/test").appendNumber(i)); |
| 50 | } |
| 51 | } |
| 52 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 53 | ~SegmentPublisherFixture() override |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 54 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 55 | fetcher->stop(); |
| 56 | } |
| 57 | |
| 58 | void |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 59 | expressInterest(const Interest& interest) |
| 60 | { |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 61 | fetcher = util::SegmentFetcher::start(face, interest, security::getAcceptAllValidator()); |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 62 | fetcher->onComplete.connect([this] (auto&&...) { numComplete++; }); |
| 63 | fetcher->onError.connect([] (auto&&...) { BOOST_CHECK(false); }); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 64 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 65 | advanceClocks(10_ms); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 69 | onInterest(const Name& prefix, const Interest& interest) |
| 70 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 71 | if (publisher.replyFromStore(interest.getName())) { |
| 72 | numRepliesFromStore++; |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // If dataName is same as interest name |
| 77 | if (dataName.empty()) { |
| 78 | publisher.publish(interest.getName(), interest.getName(), state.wireEncode(), freshness); |
| 79 | } |
| 80 | else { |
| 81 | publisher.publish(interest.getName(), dataName, state.wireEncode(), freshness); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | util::DummyClientFace face; |
| 86 | KeyChain keyChain; |
| 87 | SegmentPublisher publisher; |
| 88 | shared_ptr<util::SegmentFetcher> fetcher; |
| 89 | Name dataName; |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 90 | time::milliseconds freshness = 1_s; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 91 | State state; |
| 92 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 93 | int numComplete = 0; |
| 94 | int numRepliesFromStore = 0; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | BOOST_FIXTURE_TEST_SUITE(TestSegmentPublisher, SegmentPublisherFixture) |
| 98 | |
| 99 | BOOST_AUTO_TEST_CASE(Basic) |
| 100 | { |
| 101 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 102 | expressInterest(Interest("/hello/world")); |
| 103 | BOOST_CHECK_EQUAL(numComplete, 1); |
| 104 | // First segment is answered directly in publish, |
| 105 | // Rest two are satisfied by the store |
| 106 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 107 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3); |
| 108 | |
| 109 | numRepliesFromStore = 0; |
| 110 | expressInterest(Interest("/hello/world")); |
| 111 | BOOST_CHECK_EQUAL(numComplete, 2); |
| 112 | BOOST_CHECK_EQUAL(numRepliesFromStore, 3); |
| 113 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 114 | advanceClocks(time::milliseconds(freshness)); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 116 | |
| 117 | numRepliesFromStore = 0; |
| 118 | expressInterest(Interest("/hello/world")); |
| 119 | BOOST_CHECK_EQUAL(numComplete, 3); |
| 120 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 121 | |
| 122 | numRepliesFromStore = 0; |
Junxiao Shi | 3426baf | 2019-01-13 23:19:23 +0000 | [diff] [blame] | 123 | face.expressInterest(Interest("/hello/world/").setCanBePrefix(true), |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 124 | [this] (auto&&...) { this->numComplete++; }, |
| 125 | [] (auto&&...) { BOOST_CHECK(false); }, |
| 126 | [] (auto&&...) { BOOST_CHECK(false); }); |
| 127 | advanceClocks(10_ms); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(numComplete, 4); |
| 129 | BOOST_CHECK_EQUAL(numRepliesFromStore, 1); |
| 130 | } |
| 131 | |
| 132 | BOOST_AUTO_TEST_CASE(LargerDataName) |
| 133 | { |
| 134 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 135 | dataName = Name("/hello/world/IBF"); |
| 136 | |
| 137 | expressInterest(Interest("/hello/world")); |
| 138 | BOOST_CHECK_EQUAL(numComplete, 1); |
| 139 | // First segment is answered directly in publish, |
| 140 | // Rest two are satisfied by the store |
| 141 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 142 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3); |
| 143 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame^] | 144 | advanceClocks(time::milliseconds(freshness)); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 145 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 146 | } |
| 147 | |
| 148 | BOOST_AUTO_TEST_SUITE_END() |
| 149 | |
Junxiao Shi | 3426baf | 2019-01-13 23:19:23 +0000 | [diff] [blame] | 150 | } // namespace psync |