blob: bf437b935bfbeb982c71d6edbe383d9d6541f624 [file] [log] [blame]
Ashlesh Gawandeec43b362018-08-01 15:15:01 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Ashlesh Gawande5a895472020-01-25 18:07:32 -08003 * Copyright (c) 2014-2020, The University of Memphis
Ashlesh Gawandeec43b362018-08-01 15:15:01 -05004 *
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 Gawande0cf4b602019-01-18 15:58:17 -06009 * of the GNU Lesser General Public License as published by the Free Software Foundation,
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050010 * 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 Gawande0cf4b602019-01-18 15:58:17 -060014 * PURPOSE. See the GNU Lesser General Public License for more details.
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050015 *
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -060016 * You should have received a copy of the GNU Lesser General Public License along with
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050017 * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
Ashlesh Gawande78b94ad2018-12-13 15:29:19 -060020#include "PSync/segment-publisher.hpp"
21#include "PSync/detail/state.hpp"
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050022
Davide Pesavento5b3cf762020-04-03 16:20:04 -040023#include "tests/boost-test.hpp"
Davide Pesaventof91d1df2020-11-25 14:50:41 -050024#include "tests/io-fixture.hpp"
Davide Pesavento5b3cf762020-04-03 16:20:04 -040025
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050026#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
32namespace psync {
33
34using namespace ndn;
35
Davide Pesaventof91d1df2020-11-25 14:50:41 -050036class SegmentPublisherFixture : public tests::IoFixture
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050037{
38public:
39 SegmentPublisherFixture()
Davide Pesaventof91d1df2020-11-25 14:50:41 -050040 : face(m_io, util::DummyClientFace::Options{true, true})
Davide Pesavento042dfb32020-07-23 21:07:16 -040041 , publisher(face, keyChain)
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050042 {
43 face.setInterestFilter(InterestFilter("/hello/world"),
44 bind(&SegmentPublisherFixture::onInterest, this, _1, _2),
Davide Pesaventof91d1df2020-11-25 14:50:41 -050045 [] (auto&&...) { BOOST_CHECK(false); });
46 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050047
48 for (int i = 0; i < 1000; ++i) {
49 state.addContent(Name("/test").appendNumber(i));
50 }
51 }
52
Davide Pesaventof91d1df2020-11-25 14:50:41 -050053 ~SegmentPublisherFixture() override
Davide Pesavento042dfb32020-07-23 21:07:16 -040054 {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050055 fetcher->stop();
56 }
57
58 void
Davide Pesavento042dfb32020-07-23 21:07:16 -040059 expressInterest(const Interest& interest)
60 {
Davide Pesaventof91d1df2020-11-25 14:50:41 -050061 fetcher = util::SegmentFetcher::start(face, interest, security::getAcceptAllValidator());
Davide Pesavento042dfb32020-07-23 21:07:16 -040062 fetcher->onComplete.connect([this] (auto&&...) { numComplete++; });
63 fetcher->onError.connect([] (auto&&...) { BOOST_CHECK(false); });
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050064
Davide Pesaventof91d1df2020-11-25 14:50:41 -050065 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050066 }
67
68 void
Davide Pesavento042dfb32020-07-23 21:07:16 -040069 onInterest(const Name& prefix, const Interest& interest)
70 {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050071 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 Pesaventof91d1df2020-11-25 14:50:41 -050090 time::milliseconds freshness = 1_s;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050091 State state;
92
Davide Pesaventof91d1df2020-11-25 14:50:41 -050093 int numComplete = 0;
94 int numRepliesFromStore = 0;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050095};
96
97BOOST_FIXTURE_TEST_SUITE(TestSegmentPublisher, SegmentPublisherFixture)
98
99BOOST_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 Pesaventof91d1df2020-11-25 14:50:41 -0500114 advanceClocks(time::milliseconds(freshness));
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500115 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 Shi3426baf2019-01-13 23:19:23 +0000123 face.expressInterest(Interest("/hello/world/").setCanBePrefix(true),
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500124 [this] (auto&&...) { this->numComplete++; },
125 [] (auto&&...) { BOOST_CHECK(false); },
126 [] (auto&&...) { BOOST_CHECK(false); });
127 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500128 BOOST_CHECK_EQUAL(numComplete, 4);
129 BOOST_CHECK_EQUAL(numRepliesFromStore, 1);
130}
131
132BOOST_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 Pesaventof91d1df2020-11-25 14:50:41 -0500144 advanceClocks(time::milliseconds(freshness));
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500145 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
146}
147
148BOOST_AUTO_TEST_SUITE_END()
149
Junxiao Shi3426baf2019-01-13 23:19:23 +0000150} // namespace psync