blob: 9ce544f769af577ce112be35058748c4312fdc32 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyev21a166e2013-01-20 16:04:41 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev21a166e2013-01-20 16:04:41 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080019 */
20
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080021#include "ccnx-wrapper.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080022#include "fetch-manager.hpp"
23#include "fetcher.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "logging.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080025#include <boost/make_shared.hpp>
26#include <boost/test/unit_test.hpp>
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080027
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080028_LOG_INIT(Test.FetchManager);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080029
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070030using namespace Ndnx;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080031using namespace std;
32using namespace boost;
33
34BOOST_AUTO_TEST_SUITE(TestFetchManager)
35
36struct FetcherTestData
37{
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080038 set<uint64_t> recvData;
39 set<uint64_t> recvContent;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080040
41 set<Name> differentNames;
42 set<Name> segmentNames;
43
44 bool m_done;
45 bool m_failed;
46
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047 FetcherTestData()
48 : m_done(false)
49 , m_failed(false)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080050 {
51 }
52
53 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 onData(const Ccnx::Name& deviceName, const Ccnx::Name& basename, uint64_t seqno, Ccnx::PcoPtr pco)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080055 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056 _LOG_TRACE("onData: " << seqno);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080057
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 recvData.insert(seqno);
59 differentNames.insert(basename);
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080060 Name name = basename;
61 name.appendComp(seqno);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062 segmentNames.insert(name);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080063
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080064 BytesPtr data = pco->contentPtr();
Alexander Afanasyevf278db32013-01-21 14:41:01 -080065
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080066 if (data->size() == sizeof(int)) {
67 recvContent.insert(*reinterpret_cast<const int*>(head(*data)));
68 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080069
Alexander Afanasyev650ba282013-01-20 20:14:06 -080070 // cout << "<<< " << basename << ", " << name << ", " << seqno << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080071 }
72
73 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080074 finish(const Ccnx::Name& deviceName, const Ccnx::Name& baseName)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080075 {
76 }
77
78 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 onComplete(Fetcher& fetcher)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080080 {
81 m_done = true;
82 // cout << "Done" << endl;
83 }
84
85 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080086 onFail(Fetcher& fetcher)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080087 {
88 m_failed = true;
89 // cout << "Failed" << endl;
90 }
91};
92
Yingdi Yuf0b3de32013-07-11 12:59:00 -070093void run()
94{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070095 NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> ();
Yingdi Yuf0b3de32013-07-11 12:59:00 -070096
97 Name baseName ("/base");
98 Name deviceName ("/device");
99
100 for (int i = 0; i < 10; i++)
101 {
102 usleep(100000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700103 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700104 }
105
106 for (int i = 11; i < 50; i++)
107 {
108 usleep(100000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700109 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700110 }
111
112}
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800113
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800114BOOST_AUTO_TEST_CASE(TestFetcher)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800115{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800116 CcnxWrapperPtr ccnx = make_shared<CcnxWrapper>();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800117
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800118 Name baseName("/base");
119 Name deviceName("/device");
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800120 /* publish seqnos: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, <gap 5>, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, <gap 1>, 26 */
121 // this will allow us to test our pipeline of 6
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800122 for (int i = 0; i < 10; i++) {
123 ccnx->publishData(Name(baseName)(i), reinterpret_cast<const unsigned char*>(&i), sizeof(int), 30);
124 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800125
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800126 for (int i = 15; i < 25; i++) {
127 ccnx->publishData(Name(baseName)(i), reinterpret_cast<const unsigned char*>(&i), sizeof(int), 30);
128 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800129
130 int oneMore = 26;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800131 ccnx->publishData(Name(baseName)(oneMore), reinterpret_cast<const unsigned char*>(&oneMore),
132 sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800133
134 FetcherTestData data;
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800135 ExecutorPtr executor = make_shared<Executor>(1);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800136 executor->start();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800137
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800138 Fetcher fetcher(ccnx, executor, bind(&FetcherTestData::onData, &data, _1, _2, _3, _4),
139 bind(&FetcherTestData::finish, &data, _1, _2),
140 bind(&FetcherTestData::onComplete, &data, _1),
141 bind(&FetcherTestData::onFail, &data, _1), deviceName, Name("/base"), 0, 26,
142 boost::posix_time::seconds(5)); // this time is not precise
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800143
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800144 BOOST_CHECK_EQUAL(fetcher.IsActive(), false);
145 fetcher.RestartPipeline();
146 BOOST_CHECK_EQUAL(fetcher.IsActive(), true);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800147
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800148 usleep(7000000);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800149 BOOST_CHECK_EQUAL(data.m_failed, true);
150 BOOST_CHECK_EQUAL(data.differentNames.size(), 1);
151 BOOST_CHECK_EQUAL(data.segmentNames.size(), 20);
152 BOOST_CHECK_EQUAL(data.recvData.size(), 20);
153 BOOST_CHECK_EQUAL(data.recvContent.size(), 20);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800154
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800155 {
156 ostringstream recvData;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800157 for (set<uint64_t>::iterator i = data.recvData.begin(); i != data.recvData.end(); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800158 recvData << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800159
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800160 ostringstream recvContent;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800161 for (set<uint64_t>::iterator i = data.recvContent.begin(); i != data.recvContent.end(); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800162 recvContent << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800163
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800164 BOOST_CHECK_EQUAL(recvData.str(),
165 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, ");
166 BOOST_CHECK_EQUAL(recvData.str(), recvContent.str());
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800167 }
168
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800169 BOOST_CHECK_EQUAL(fetcher.IsActive(), false);
170 fetcher.RestartPipeline();
171 BOOST_CHECK_EQUAL(fetcher.IsActive(), true);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800172
173 usleep(7000000);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800174 BOOST_CHECK_EQUAL(data.m_failed, true);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800175
176 // publishing missing pieces
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800177 for (int i = 0; i < 27; i++) {
178 ccnx->publishData(Name(baseName)(i), reinterpret_cast<const unsigned char*>(&i), sizeof(int), 1);
179 }
180 BOOST_CHECK_EQUAL(fetcher.IsActive(), false);
181 fetcher.RestartPipeline();
182 BOOST_CHECK_EQUAL(fetcher.IsActive(), true);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800183
184 usleep(1000000);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800185 BOOST_CHECK_EQUAL(data.m_done, true);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800186
187 {
188 ostringstream recvData;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800189 for (set<uint64_t>::iterator i = data.recvData.begin(); i != data.recvData.end(); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800190 recvData << *i << ", ";
191
192 ostringstream recvContent;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800193 for (set<uint64_t>::iterator i = data.recvContent.begin(); i != data.recvContent.end(); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800194 recvContent << *i << ", ";
195
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800196 BOOST_CHECK_EQUAL(recvData.str(),
197 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, ");
198 BOOST_CHECK_EQUAL(recvData.str(), recvContent.str());
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800199 }
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800200
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800201 executor->shutdown();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800202}
203
Yingdi Yu57f667b2013-07-11 10:37:59 -0700204
205BOOST_AUTO_TEST_CASE (TestFetcher2)
206{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700207 NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> ();
Yingdi Yu57f667b2013-07-11 10:37:59 -0700208
209 Name baseName ("/base");
210 Name deviceName ("/device");
Yingdi Yu57f667b2013-07-11 10:37:59 -0700211
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700212 thread publishThread(run);
Yingdi Yu57f667b2013-07-11 10:37:59 -0700213
214 FetcherTestData data;
215 ExecutorPtr executor = make_shared<Executor>(1);
216 executor->start ();
217
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700218 Fetcher fetcher (ndnx,
Yingdi Yu57f667b2013-07-11 10:37:59 -0700219 executor,
220 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4),
221 bind (&FetcherTestData::finish, &data, _1, _2),
222 bind (&FetcherTestData::onComplete, &data, _1),
223 bind (&FetcherTestData::onFail, &data, _1),
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700224 deviceName, baseName, 0, 49,
Yingdi Yu57f667b2013-07-11 10:37:59 -0700225 boost::posix_time::seconds (5)); // this time is not precise
226
227 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
228 fetcher.RestartPipeline ();
229 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
230
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700231 usleep(20000000);
Yingdi Yu57f667b2013-07-11 10:37:59 -0700232 BOOST_CHECK_EQUAL (data.m_failed, true);
233
234 executor->shutdown ();
235}
236
237
238
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700239// BOOST_AUTO_TEST_CASE (NdnxWrapperSelector)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800240// {
241
242// Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
243
244// Selectors selectors;
245// selectors.interestLifetime(1);
246
247// string n1 = "/random/01";
248// c1->sendInterest(Name(n1), closure, selectors);
249// sleep(2);
250// c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
251// usleep(100000);
252// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
253// BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
254
255// string n2 = "/random/02";
256// selectors.interestLifetime(2);
257// c1->sendInterest(Name(n2), closure, selectors);
258// sleep(1);
259// c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
260// usleep(100000);
261// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
262// BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
263
264// // reset
265// g_dataCallback_counter = 0;
266// g_timeout_counter = 0;
267// }
268
269BOOST_AUTO_TEST_SUITE_END()