blob: 24ca59ceeec63c83e5a7813ab1048214ffe1d6aa [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>
27
28using namespace Ccnx;
29using namespace std;
30using namespace boost;
31
32BOOST_AUTO_TEST_SUITE(TestFetchManager)
33
34struct FetcherTestData
35{
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080036 set<uint64_t> recvData;
37 set<uint64_t> recvContent;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080038
39 set<Name> differentNames;
40 set<Name> segmentNames;
41
42 bool m_done;
43 bool m_failed;
44
45 FetcherTestData ()
46 : m_done (false)
47 , m_failed (false)
48 {
49 }
50
51 void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080052 onData (const Ccnx::Name &deviceName, const Ccnx::Name &basename, uint64_t seqno, Ccnx::PcoPtr pco)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080053 {
54 recvData.insert (seqno);
55 differentNames.insert (basename);
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080056 Name name = basename;
57 name.appendComp(seqno);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080058 segmentNames.insert (name);
59
Alexander Afanasyevf278db32013-01-21 14:41:01 -080060 BytesPtr data = pco->contentPtr ();
61
62 if (data->size () == sizeof(int))
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080063 {
Alexander Afanasyevf278db32013-01-21 14:41:01 -080064 recvContent.insert (*reinterpret_cast<const int*> (head(*data)));
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080065 }
66
Alexander Afanasyev650ba282013-01-20 20:14:06 -080067 // cout << "<<< " << basename << ", " << name << ", " << seqno << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080068 }
69
70 void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080071 finish(const Ccnx::Name &deviceName, const Ccnx::Name &baseName)
72 {
73 }
74
75 void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080076 onComplete (Fetcher &fetcher)
77 {
78 m_done = true;
79 // cout << "Done" << endl;
80 }
81
82 void
83 onFail (Fetcher &fetcher)
84 {
85 m_failed = true;
86 // cout << "Failed" << endl;
87 }
88};
89
90
91BOOST_AUTO_TEST_CASE (TestFetcher)
92{
93 CcnxWrapperPtr ccnx = make_shared<CcnxWrapper> ();
94
95 Name baseName ("/base");
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080096 Name deviceName ("/device");
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080097 /* 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 */
98 // this will allow us to test our pipeline of 6
99 for (int i = 0; i < 10; i++)
100 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800101 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
102 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800103
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800104 for (int i = 15; i < 25; i++)
105 {
106 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800107 }
108
109 int oneMore = 26;
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800110 ccnx->publishData (Name (baseName)(oneMore), reinterpret_cast<const unsigned char*> (&oneMore), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800111
112 FetcherTestData data;
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800113 ExecutorPtr executor = make_shared<Executor>(1);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800114
115 Fetcher fetcher (ccnx,
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800116 executor,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800117 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4),
118 bind (&FetcherTestData::finish, &data, _1, _2),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800119 bind (&FetcherTestData::onComplete, &data, _1),
120 bind (&FetcherTestData::onFail, &data, _1),
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800121 deviceName, Name ("/base"), 0, 26,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800122 boost::posix_time::seconds (5)); // this time is not precise
123
124 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
125 fetcher.RestartPipeline ();
126 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
127
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800128 usleep(7000000);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800129 BOOST_CHECK_EQUAL (data.m_failed, true);
130 BOOST_CHECK_EQUAL (data.differentNames.size (), 1);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800131 BOOST_CHECK_EQUAL (data.segmentNames.size (), 20);
132 BOOST_CHECK_EQUAL (data.recvData.size (), 20);
133 BOOST_CHECK_EQUAL (data.recvContent.size (), 20);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800134
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800135 {
136 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800137 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800138 recvData << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800139
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800140 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800141 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800142 recvContent << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800143
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800144 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, ");
145 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
146 }
147
148 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
149 fetcher.RestartPipeline ();
150 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
151
152 usleep(7000000);
153 BOOST_CHECK_EQUAL (data.m_failed, true);
154
155 // publishing missing pieces
156 for (int i = 0; i < 27; i++)
157 {
158 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 1);
159 }
160 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
161 fetcher.RestartPipeline ();
162 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
163
164 usleep(1000000);
165 BOOST_CHECK_EQUAL (data.m_done, true);
166
167 {
168 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800169 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800170 recvData << *i << ", ";
171
172 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800173 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800174 recvContent << *i << ", ";
175
176 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, ");
177 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
178 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800179}
180
181// BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
182// {
183
184// Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
185
186// Selectors selectors;
187// selectors.interestLifetime(1);
188
189// string n1 = "/random/01";
190// c1->sendInterest(Name(n1), closure, selectors);
191// sleep(2);
192// c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
193// usleep(100000);
194// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
195// BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
196
197// string n2 = "/random/02";
198// selectors.interestLifetime(2);
199// c1->sendInterest(Name(n2), closure, selectors);
200// sleep(1);
201// c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
202// usleep(100000);
203// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
204// BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
205
206// // reset
207// g_dataCallback_counter = 0;
208// g_timeout_counter = 0;
209// }
210
211BOOST_AUTO_TEST_SUITE_END()