blob: 55c85dfe834a7aad4b84547cefa62cc64fcbc444 [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{
36 set<uint32_t> recvData;
37 set<uint32_t> recvContent;
38
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
52 onData (Fetcher &fetcher, uint32_t seqno, const Ccnx::Name &basename,
53 const Ccnx::Name &name, const Ccnx::Bytes &data)
54 {
55 recvData.insert (seqno);
56 differentNames.insert (basename);
57 segmentNames.insert (name);
58
59 if (data.size () == sizeof(int))
60 {
61 recvContent.insert (*reinterpret_cast<const int*> (head(data)));
62 }
63
Alexander Afanasyev650ba282013-01-20 20:14:06 -080064 // cout << "<<< " << basename << ", " << name << ", " << seqno << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080065 }
66
67 void
68 onComplete (Fetcher &fetcher)
69 {
70 m_done = true;
71 // cout << "Done" << endl;
72 }
73
74 void
75 onFail (Fetcher &fetcher)
76 {
77 m_failed = true;
78 // cout << "Failed" << endl;
79 }
80};
81
82
83BOOST_AUTO_TEST_CASE (TestFetcher)
84{
85 CcnxWrapperPtr ccnx = make_shared<CcnxWrapper> ();
86
87 Name baseName ("/base");
88 /* 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 */
89 // this will allow us to test our pipeline of 6
90 for (int i = 0; i < 10; i++)
91 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -080092 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
93 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080094
Alexander Afanasyev650ba282013-01-20 20:14:06 -080095 for (int i = 15; i < 25; i++)
96 {
97 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080098 }
99
100 int oneMore = 26;
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800101 ccnx->publishData (Name (baseName)(oneMore), reinterpret_cast<const unsigned char*> (&oneMore), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800102
103 FetcherTestData data;
104
105 Fetcher fetcher (ccnx,
106 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4, _5),
107 bind (&FetcherTestData::onComplete, &data, _1),
108 bind (&FetcherTestData::onFail, &data, _1),
109 Name ("/base"), 0, 26,
110 boost::posix_time::seconds (5)); // this time is not precise
111
112 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
113 fetcher.RestartPipeline ();
114 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
115
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800116 usleep(7000000);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800117 BOOST_CHECK_EQUAL (data.m_failed, true);
118 BOOST_CHECK_EQUAL (data.differentNames.size (), 1);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800119 BOOST_CHECK_EQUAL (data.segmentNames.size (), 20);
120 BOOST_CHECK_EQUAL (data.recvData.size (), 20);
121 BOOST_CHECK_EQUAL (data.recvContent.size (), 20);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800122
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800123 {
124 ostringstream recvData;
125 for (set<uint32_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
126 recvData << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800127
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800128 ostringstream recvContent;
129 for (set<uint32_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
130 recvContent << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800131
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800132 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, ");
133 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
134 }
135
136 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
137 fetcher.RestartPipeline ();
138 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
139
140 usleep(7000000);
141 BOOST_CHECK_EQUAL (data.m_failed, true);
142
143 // publishing missing pieces
144 for (int i = 0; i < 27; i++)
145 {
146 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 1);
147 }
148 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
149 fetcher.RestartPipeline ();
150 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
151
152 usleep(1000000);
153 BOOST_CHECK_EQUAL (data.m_done, true);
154
155 {
156 ostringstream recvData;
157 for (set<uint32_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
158 recvData << *i << ", ";
159
160 ostringstream recvContent;
161 for (set<uint32_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
162 recvContent << *i << ", ";
163
164 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, ");
165 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
166 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800167}
168
169// BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
170// {
171
172// Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
173
174// Selectors selectors;
175// selectors.interestLifetime(1);
176
177// string n1 = "/random/01";
178// c1->sendInterest(Name(n1), closure, selectors);
179// sleep(2);
180// c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
181// usleep(100000);
182// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
183// BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
184
185// string n2 = "/random/02";
186// selectors.interestLifetime(2);
187// c1->sendInterest(Name(n2), closure, selectors);
188// sleep(1);
189// c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
190// usleep(100000);
191// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
192// BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
193
194// // reset
195// g_dataCallback_counter = 0;
196// g_timeout_counter = 0;
197// }
198
199BOOST_AUTO_TEST_SUITE_END()