blob: a526c1074ef87f7c38699b76f3fc23868992844a [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;
113
114 Fetcher fetcher (ccnx,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800115 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4),
116 bind (&FetcherTestData::finish, &data, _1, _2),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800117 bind (&FetcherTestData::onComplete, &data, _1),
118 bind (&FetcherTestData::onFail, &data, _1),
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800119 deviceName, Name ("/base"), 0, 26,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800120 boost::posix_time::seconds (5)); // this time is not precise
121
122 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
123 fetcher.RestartPipeline ();
124 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
125
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800126 usleep(7000000);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800127 BOOST_CHECK_EQUAL (data.m_failed, true);
128 BOOST_CHECK_EQUAL (data.differentNames.size (), 1);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800129 BOOST_CHECK_EQUAL (data.segmentNames.size (), 20);
130 BOOST_CHECK_EQUAL (data.recvData.size (), 20);
131 BOOST_CHECK_EQUAL (data.recvContent.size (), 20);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800132
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800133 {
134 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800135 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800136 recvData << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800137
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800138 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800139 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800140 recvContent << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800141
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800142 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, ");
143 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
144 }
145
146 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
147 fetcher.RestartPipeline ();
148 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
149
150 usleep(7000000);
151 BOOST_CHECK_EQUAL (data.m_failed, true);
152
153 // publishing missing pieces
154 for (int i = 0; i < 27; i++)
155 {
156 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 1);
157 }
158 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
159 fetcher.RestartPipeline ();
160 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
161
162 usleep(1000000);
163 BOOST_CHECK_EQUAL (data.m_done, true);
164
165 {
166 ostringstream recvData;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800167 for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800168 recvData << *i << ", ";
169
170 ostringstream recvContent;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800171 for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800172 recvContent << *i << ", ";
173
174 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, ");
175 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
176 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800177}
178
179// BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
180// {
181
182// Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
183
184// Selectors selectors;
185// selectors.interestLifetime(1);
186
187// string n1 = "/random/01";
188// c1->sendInterest(Name(n1), closure, selectors);
189// sleep(2);
190// c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
191// usleep(100000);
192// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
193// BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
194
195// string n2 = "/random/02";
196// selectors.interestLifetime(2);
197// c1->sendInterest(Name(n2), closure, selectors);
198// sleep(1);
199// c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
200// usleep(100000);
201// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
202// BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
203
204// // reset
205// g_dataCallback_counter = 0;
206// g_timeout_counter = 0;
207// }
208
209BOOST_AUTO_TEST_SUITE_END()