blob: 42f7f2dac879726e8c0c7950500f383b1a225e72 [file] [log] [blame]
Alexander Afanasyev21a166e2013-01-20 16:04:41 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
20 */
21
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080022#include "fetch-manager.hpp"
23#include "fetcher.hpp"
24#include "ccnx-wrapper.hpp"
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080025#include <boost/test/unit_test.hpp>
26#include <boost/make_shared.hpp>
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080027#include "logging.hpp"
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080028
Yingdi Yuf0b3de32013-07-11 12:59:00 -070029
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080030INIT_LOGGER ("Test.FetchManager");
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080031
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070032using namespace Ndnx;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080033using namespace std;
34using namespace boost;
35
36BOOST_AUTO_TEST_SUITE(TestFetchManager)
37
38struct FetcherTestData
39{
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080040 set<uint64_t> recvData;
41 set<uint64_t> recvContent;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080042
43 set<Name> differentNames;
44 set<Name> segmentNames;
45
46 bool m_done;
47 bool m_failed;
48
49 FetcherTestData ()
50 : m_done (false)
51 , m_failed (false)
52 {
53 }
54
55 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070056 onData (const Ndnx::Name &deviceName, const Ndnx::Name &basename, uint64_t seqno, Ndnx::PcoPtr pco)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080057 {
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080058 _LOG_TRACE ("onData: " << seqno);
59
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080060 recvData.insert (seqno);
61 differentNames.insert (basename);
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080062 Name name = basename;
63 name.appendComp(seqno);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080064 segmentNames.insert (name);
65
Alexander Afanasyevf278db32013-01-21 14:41:01 -080066 BytesPtr data = pco->contentPtr ();
67
68 if (data->size () == sizeof(int))
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080069 {
Alexander Afanasyevf278db32013-01-21 14:41:01 -080070 recvContent.insert (*reinterpret_cast<const int*> (head(*data)));
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080071 }
72
Alexander Afanasyev650ba282013-01-20 20:14:06 -080073 // cout << "<<< " << basename << ", " << name << ", " << seqno << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080074 }
75
76 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070077 finish(const Ndnx::Name &deviceName, const Ndnx::Name &baseName)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080078 {
79 }
80
81 void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080082 onComplete (Fetcher &fetcher)
83 {
84 m_done = true;
85 // cout << "Done" << endl;
86 }
87
88 void
89 onFail (Fetcher &fetcher)
90 {
91 m_failed = true;
92 // cout << "Failed" << endl;
93 }
94};
95
Yingdi Yuf0b3de32013-07-11 12:59:00 -070096void run()
97{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070098 NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> ();
Yingdi Yuf0b3de32013-07-11 12:59:00 -070099
100 Name baseName ("/base");
101 Name deviceName ("/device");
102
103 for (int i = 0; i < 10; i++)
104 {
105 usleep(100000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700106 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700107 }
108
109 for (int i = 11; i < 50; i++)
110 {
111 usleep(100000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700112 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700113 }
114
115}
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800116
117BOOST_AUTO_TEST_CASE (TestFetcher)
118{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800119 INIT_LOGGERS ();
120
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700121 NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> ();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800122
123 Name baseName ("/base");
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800124 Name deviceName ("/device");
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800125 /* 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 */
126 // this will allow us to test our pipeline of 6
127 for (int i = 0; i < 10; i++)
128 {
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700129 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800130 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800131
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800132 for (int i = 15; i < 25; i++)
133 {
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700134 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800135 }
136
137 int oneMore = 26;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700138 ndnx->publishData (Name (baseName)(oneMore), reinterpret_cast<const unsigned char*> (&oneMore), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800139
140 FetcherTestData data;
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800141 ExecutorPtr executor = make_shared<Executor>(1);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800142 executor->start ();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800143
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700144 Fetcher fetcher (ndnx,
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800145 executor,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800146 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4),
147 bind (&FetcherTestData::finish, &data, _1, _2),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800148 bind (&FetcherTestData::onComplete, &data, _1),
149 bind (&FetcherTestData::onFail, &data, _1),
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800150 deviceName, Name ("/base"), 0, 26,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800151 boost::posix_time::seconds (5)); // this time is not precise
152
153 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
154 fetcher.RestartPipeline ();
155 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
156
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800157 usleep(7000000);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800158 BOOST_CHECK_EQUAL (data.m_failed, true);
159 BOOST_CHECK_EQUAL (data.differentNames.size (), 1);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800160 BOOST_CHECK_EQUAL (data.segmentNames.size (), 20);
161 BOOST_CHECK_EQUAL (data.recvData.size (), 20);
162 BOOST_CHECK_EQUAL (data.recvContent.size (), 20);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800163
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800164 {
165 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800166 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800167 recvData << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800168
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800169 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800170 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800171 recvContent << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800172
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800173 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, ");
174 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
175 }
176
177 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
178 fetcher.RestartPipeline ();
179 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
180
181 usleep(7000000);
182 BOOST_CHECK_EQUAL (data.m_failed, true);
183
184 // publishing missing pieces
185 for (int i = 0; i < 27; i++)
186 {
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700187 ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 1);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800188 }
189 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
190 fetcher.RestartPipeline ();
191 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
192
193 usleep(1000000);
194 BOOST_CHECK_EQUAL (data.m_done, true);
195
196 {
197 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800198 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800199 recvData << *i << ", ";
200
201 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800202 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800203 recvContent << *i << ", ";
204
205 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, ");
206 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
207 }
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800208
209 executor->shutdown ();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800210}
211
Yingdi Yu57f667b2013-07-11 10:37:59 -0700212
213BOOST_AUTO_TEST_CASE (TestFetcher2)
214{
215 INIT_LOGGERS ();
216
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700217 NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> ();
Yingdi Yu57f667b2013-07-11 10:37:59 -0700218
219 Name baseName ("/base");
220 Name deviceName ("/device");
Yingdi Yu57f667b2013-07-11 10:37:59 -0700221
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700222 thread publishThread(run);
Yingdi Yu57f667b2013-07-11 10:37:59 -0700223
224 FetcherTestData data;
225 ExecutorPtr executor = make_shared<Executor>(1);
226 executor->start ();
227
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700228 Fetcher fetcher (ndnx,
Yingdi Yu57f667b2013-07-11 10:37:59 -0700229 executor,
230 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4),
231 bind (&FetcherTestData::finish, &data, _1, _2),
232 bind (&FetcherTestData::onComplete, &data, _1),
233 bind (&FetcherTestData::onFail, &data, _1),
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700234 deviceName, baseName, 0, 49,
Yingdi Yu57f667b2013-07-11 10:37:59 -0700235 boost::posix_time::seconds (5)); // this time is not precise
236
237 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
238 fetcher.RestartPipeline ();
239 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
240
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700241 usleep(20000000);
Yingdi Yu57f667b2013-07-11 10:37:59 -0700242 BOOST_CHECK_EQUAL (data.m_failed, true);
243
244 executor->shutdown ();
245}
246
247
248
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700249// BOOST_AUTO_TEST_CASE (NdnxWrapperSelector)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800250// {
251
252// Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
253
254// Selectors selectors;
255// selectors.interestLifetime(1);
256
257// string n1 = "/random/01";
258// c1->sendInterest(Name(n1), closure, selectors);
259// sleep(2);
260// c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
261// usleep(100000);
262// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
263// BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
264
265// string n2 = "/random/02";
266// selectors.interestLifetime(2);
267// c1->sendInterest(Name(n2), closure, selectors);
268// sleep(1);
269// c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
270// usleep(100000);
271// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
272// BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
273
274// // reset
275// g_dataCallback_counter = 0;
276// g_timeout_counter = 0;
277// }
278
279BOOST_AUTO_TEST_SUITE_END()