blob: 0c6bb37f47322e63cf2d8dd3744acb0efcc02200 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, 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 Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "fetch-manager.hpp"
22#include "fetcher.hpp"
23#include "ccnx-wrapper.hpp"
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080024#include <boost/test/unit_test.hpp>
25#include <boost/make_shared.hpp>
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080026#include "logging.hpp"
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080027
Yingdi Yuf0b3de32013-07-11 12:59:00 -070028
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080029INIT_LOGGER ("Test.FetchManager");
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080030
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070031using namespace Ndnx;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080032using namespace std;
33using namespace boost;
34
35BOOST_AUTO_TEST_SUITE(TestFetchManager)
36
37struct FetcherTestData
38{
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080039 set<uint64_t> recvData;
40 set<uint64_t> recvContent;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080041
42 set<Name> differentNames;
43 set<Name> segmentNames;
44
45 bool m_done;
46 bool m_failed;
47
48 FetcherTestData ()
49 : m_done (false)
50 , m_failed (false)
51 {
52 }
53
54 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070055 onData (const Ndnx::Name &deviceName, const Ndnx::Name &basename, uint64_t seqno, Ndnx::PcoPtr pco)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080056 {
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080057 _LOG_TRACE ("onData: " << seqno);
58
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080059 recvData.insert (seqno);
60 differentNames.insert (basename);
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080061 Name name = basename;
62 name.appendComp(seqno);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080063 segmentNames.insert (name);
64
Alexander Afanasyevf278db32013-01-21 14:41:01 -080065 BytesPtr data = pco->contentPtr ();
66
67 if (data->size () == sizeof(int))
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080068 {
Alexander Afanasyevf278db32013-01-21 14:41:01 -080069 recvContent.insert (*reinterpret_cast<const int*> (head(*data)));
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080070 }
71
Alexander Afanasyev650ba282013-01-20 20:14:06 -080072 // cout << "<<< " << basename << ", " << name << ", " << seqno << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080073 }
74
75 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070076 finish(const Ndnx::Name &deviceName, const Ndnx::Name &baseName)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080077 {
78 }
79
80 void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080081 onComplete (Fetcher &fetcher)
82 {
83 m_done = true;
84 // cout << "Done" << endl;
85 }
86
87 void
88 onFail (Fetcher &fetcher)
89 {
90 m_failed = true;
91 // cout << "Failed" << endl;
92 }
93};
94
Yingdi Yuf0b3de32013-07-11 12:59:00 -070095void run()
96{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070097 NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> ();
Yingdi Yuf0b3de32013-07-11 12:59:00 -070098
99 Name baseName ("/base");
100 Name deviceName ("/device");
101
102 for (int i = 0; i < 10; i++)
103 {
104 usleep(100000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700105 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700106 }
107
108 for (int i = 11; i < 50; i++)
109 {
110 usleep(100000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700111 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700112 }
113
114}
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800115
116BOOST_AUTO_TEST_CASE (TestFetcher)
117{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800118 INIT_LOGGERS ();
119
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700120 NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> ();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800121
122 Name baseName ("/base");
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800123 Name deviceName ("/device");
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800124 /* 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 */
125 // this will allow us to test our pipeline of 6
126 for (int i = 0; i < 10; i++)
127 {
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700128 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800129 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800130
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800131 for (int i = 15; i < 25; i++)
132 {
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700133 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800134 }
135
136 int oneMore = 26;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700137 ndnx->publishData (Name (baseName)(oneMore), reinterpret_cast<const unsigned char*> (&oneMore), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800138
139 FetcherTestData data;
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800140 ExecutorPtr executor = make_shared<Executor>(1);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800141 executor->start ();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800142
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700143 Fetcher fetcher (ndnx,
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800144 executor,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800145 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4),
146 bind (&FetcherTestData::finish, &data, _1, _2),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800147 bind (&FetcherTestData::onComplete, &data, _1),
148 bind (&FetcherTestData::onFail, &data, _1),
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800149 deviceName, Name ("/base"), 0, 26,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800150 boost::posix_time::seconds (5)); // this time is not precise
151
152 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
153 fetcher.RestartPipeline ();
154 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
155
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800156 usleep(7000000);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800157 BOOST_CHECK_EQUAL (data.m_failed, true);
158 BOOST_CHECK_EQUAL (data.differentNames.size (), 1);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800159 BOOST_CHECK_EQUAL (data.segmentNames.size (), 20);
160 BOOST_CHECK_EQUAL (data.recvData.size (), 20);
161 BOOST_CHECK_EQUAL (data.recvContent.size (), 20);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800162
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800163 {
164 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800165 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800166 recvData << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800167
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800168 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800169 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800170 recvContent << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800171
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800172 BOOST_CHECK_EQUAL (recvData.str (), "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, ");
173 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
174 }
175
176 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
177 fetcher.RestartPipeline ();
178 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
179
180 usleep(7000000);
181 BOOST_CHECK_EQUAL (data.m_failed, true);
182
183 // publishing missing pieces
184 for (int i = 0; i < 27; i++)
185 {
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700186 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 1);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800187 }
188 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
189 fetcher.RestartPipeline ();
190 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
191
192 usleep(1000000);
193 BOOST_CHECK_EQUAL (data.m_done, true);
194
195 {
196 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800197 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800198 recvData << *i << ", ";
199
200 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800201 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800202 recvContent << *i << ", ";
203
204 BOOST_CHECK_EQUAL (recvData.str (), "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, ");
205 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
206 }
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800207
208 executor->shutdown ();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800209}
210
Yingdi Yu57f667b2013-07-11 10:37:59 -0700211
212BOOST_AUTO_TEST_CASE (TestFetcher2)
213{
214 INIT_LOGGERS ();
215
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700216 NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> ();
Yingdi Yu57f667b2013-07-11 10:37:59 -0700217
218 Name baseName ("/base");
219 Name deviceName ("/device");
Yingdi Yu57f667b2013-07-11 10:37:59 -0700220
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700221 thread publishThread(run);
Yingdi Yu57f667b2013-07-11 10:37:59 -0700222
223 FetcherTestData data;
224 ExecutorPtr executor = make_shared<Executor>(1);
225 executor->start ();
226
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700227 Fetcher fetcher (ndnx,
Yingdi Yu57f667b2013-07-11 10:37:59 -0700228 executor,
229 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4),
230 bind (&FetcherTestData::finish, &data, _1, _2),
231 bind (&FetcherTestData::onComplete, &data, _1),
232 bind (&FetcherTestData::onFail, &data, _1),
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700233 deviceName, baseName, 0, 49,
Yingdi Yu57f667b2013-07-11 10:37:59 -0700234 boost::posix_time::seconds (5)); // this time is not precise
235
236 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
237 fetcher.RestartPipeline ();
238 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
239
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700240 usleep(20000000);
Yingdi Yu57f667b2013-07-11 10:37:59 -0700241 BOOST_CHECK_EQUAL (data.m_failed, true);
242
243 executor->shutdown ();
244}
245
246
247
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700248// BOOST_AUTO_TEST_CASE (NdnxWrapperSelector)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800249// {
250
251// Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
252
253// Selectors selectors;
254// selectors.interestLifetime(1);
255
256// string n1 = "/random/01";
257// c1->sendInterest(Name(n1), closure, selectors);
258// sleep(2);
259// c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
260// usleep(100000);
261// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
262// BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
263
264// string n2 = "/random/02";
265// selectors.interestLifetime(2);
266// c1->sendInterest(Name(n2), closure, selectors);
267// sleep(1);
268// c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
269// usleep(100000);
270// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
271// BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
272
273// // reset
274// g_dataCallback_counter = 0;
275// g_timeout_counter = 0;
276// }
277
278BOOST_AUTO_TEST_SUITE_END()