blob: 5abe47db6c09d12ba78499cdab6678fb91dae733 [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
22#include "fetch-manager.h"
23#include "fetcher.h"
24#include "ccnx-wrapper.h"
25#include <boost/test/unit_test.hpp>
26#include <boost/make_shared.hpp>
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080027#include "logging.h"
28
29INIT_LOGGER ("Test.FetchManager");
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080030
31using namespace Ccnx;
32using 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
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080055 onData (const Ccnx::Name &deviceName, const Ccnx::Name &basename, uint64_t seqno, Ccnx::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
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080076 finish(const Ccnx::Name &deviceName, const Ccnx::Name &baseName)
77 {
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
95
96BOOST_AUTO_TEST_CASE (TestFetcher)
97{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080098 INIT_LOGGERS ();
99
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800100 CcnxWrapperPtr ccnx = make_shared<CcnxWrapper> ();
101
102 Name baseName ("/base");
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800103 Name deviceName ("/device");
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800104 /* 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 */
105 // this will allow us to test our pipeline of 6
106 for (int i = 0; i < 10; i++)
107 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800108 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
109 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800110
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800111 for (int i = 15; i < 25; i++)
112 {
113 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800114 }
115
116 int oneMore = 26;
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800117 ccnx->publishData (Name (baseName)(oneMore), reinterpret_cast<const unsigned char*> (&oneMore), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800118
119 FetcherTestData data;
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800120 ExecutorPtr executor = make_shared<Executor>(1);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800121 executor->start ();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800122
123 Fetcher fetcher (ccnx,
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800124 executor,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800125 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4),
126 bind (&FetcherTestData::finish, &data, _1, _2),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800127 bind (&FetcherTestData::onComplete, &data, _1),
128 bind (&FetcherTestData::onFail, &data, _1),
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800129 deviceName, Name ("/base"), 0, 26,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800130 boost::posix_time::seconds (5)); // this time is not precise
131
132 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
133 fetcher.RestartPipeline ();
134 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
135
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800136 usleep(7000000);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800137 BOOST_CHECK_EQUAL (data.m_failed, true);
138 BOOST_CHECK_EQUAL (data.differentNames.size (), 1);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800139 BOOST_CHECK_EQUAL (data.segmentNames.size (), 20);
140 BOOST_CHECK_EQUAL (data.recvData.size (), 20);
141 BOOST_CHECK_EQUAL (data.recvContent.size (), 20);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800142
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800143 {
144 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800145 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800146 recvData << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800147
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800148 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800149 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800150 recvContent << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800151
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800152 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, ");
153 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
154 }
155
156 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
157 fetcher.RestartPipeline ();
158 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
159
160 usleep(7000000);
161 BOOST_CHECK_EQUAL (data.m_failed, true);
162
163 // publishing missing pieces
164 for (int i = 0; i < 27; i++)
165 {
166 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 1);
167 }
168 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
169 fetcher.RestartPipeline ();
170 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
171
172 usleep(1000000);
173 BOOST_CHECK_EQUAL (data.m_done, true);
174
175 {
176 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800177 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800178 recvData << *i << ", ";
179
180 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800181 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800182 recvContent << *i << ", ";
183
184 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, ");
185 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
186 }
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800187
188 executor->shutdown ();
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800189}
190
191// BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
192// {
193
194// Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
195
196// Selectors selectors;
197// selectors.interestLifetime(1);
198
199// string n1 = "/random/01";
200// c1->sendInterest(Name(n1), closure, selectors);
201// sleep(2);
202// c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
203// usleep(100000);
204// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
205// BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
206
207// string n2 = "/random/02";
208// selectors.interestLifetime(2);
209// c1->sendInterest(Name(n2), closure, selectors);
210// sleep(1);
211// c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
212// usleep(100000);
213// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
214// BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
215
216// // reset
217// g_dataCallback_counter = 0;
218// g_timeout_counter = 0;
219// }
220
221BOOST_AUTO_TEST_SUITE_END()