blob: 276fe0c338770f1a1aed93a320369dba1fac2e0c [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,
Alexander Afanasyevf278db32013-01-21 14:41:01 -080053 const Ccnx::Name &name, Ccnx::PcoPtr pco)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080054 {
55 recvData.insert (seqno);
56 differentNames.insert (basename);
57 segmentNames.insert (name);
58
Alexander Afanasyevf278db32013-01-21 14:41:01 -080059 BytesPtr data = pco->contentPtr ();
60
61 if (data->size () == sizeof(int))
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080062 {
Alexander Afanasyevf278db32013-01-21 14:41:01 -080063 recvContent.insert (*reinterpret_cast<const int*> (head(*data)));
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080064 }
65
Alexander Afanasyev650ba282013-01-20 20:14:06 -080066 // cout << "<<< " << basename << ", " << name << ", " << seqno << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080067 }
68
69 void
70 onComplete (Fetcher &fetcher)
71 {
72 m_done = true;
73 // cout << "Done" << endl;
74 }
75
76 void
77 onFail (Fetcher &fetcher)
78 {
79 m_failed = true;
80 // cout << "Failed" << endl;
81 }
82};
83
84
85BOOST_AUTO_TEST_CASE (TestFetcher)
86{
87 CcnxWrapperPtr ccnx = make_shared<CcnxWrapper> ();
88
89 Name baseName ("/base");
90 /* 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 */
91 // this will allow us to test our pipeline of 6
92 for (int i = 0; i < 10; i++)
93 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -080094 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
95 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080096
Alexander Afanasyev650ba282013-01-20 20:14:06 -080097 for (int i = 15; i < 25; i++)
98 {
99 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800100 }
101
102 int oneMore = 26;
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800103 ccnx->publishData (Name (baseName)(oneMore), reinterpret_cast<const unsigned char*> (&oneMore), sizeof(int), 30);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800104
105 FetcherTestData data;
106
107 Fetcher fetcher (ccnx,
108 bind (&FetcherTestData::onData, &data, _1, _2, _3, _4, _5),
109 bind (&FetcherTestData::onComplete, &data, _1),
110 bind (&FetcherTestData::onFail, &data, _1),
111 Name ("/base"), 0, 26,
112 boost::posix_time::seconds (5)); // this time is not precise
113
114 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
115 fetcher.RestartPipeline ();
116 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
117
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800118 usleep(7000000);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800119 BOOST_CHECK_EQUAL (data.m_failed, true);
120 BOOST_CHECK_EQUAL (data.differentNames.size (), 1);
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800121 BOOST_CHECK_EQUAL (data.segmentNames.size (), 20);
122 BOOST_CHECK_EQUAL (data.recvData.size (), 20);
123 BOOST_CHECK_EQUAL (data.recvContent.size (), 20);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800124
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800125 {
126 ostringstream recvData;
127 for (set<uint32_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
128 recvData << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800129
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800130 ostringstream recvContent;
131 for (set<uint32_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
132 recvContent << *i << ", ";
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800133
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800134 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, ");
135 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
136 }
137
138 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
139 fetcher.RestartPipeline ();
140 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
141
142 usleep(7000000);
143 BOOST_CHECK_EQUAL (data.m_failed, true);
144
145 // publishing missing pieces
146 for (int i = 0; i < 27; i++)
147 {
148 ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 1);
149 }
150 BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
151 fetcher.RestartPipeline ();
152 BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
153
154 usleep(1000000);
155 BOOST_CHECK_EQUAL (data.m_done, true);
156
157 {
158 ostringstream recvData;
159 for (set<uint32_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++)
160 recvData << *i << ", ";
161
162 ostringstream recvContent;
163 for (set<uint32_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++)
164 recvContent << *i << ", ";
165
166 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, ");
167 BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ());
168 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800169}
170
171// BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
172// {
173
174// Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
175
176// Selectors selectors;
177// selectors.interestLifetime(1);
178
179// string n1 = "/random/01";
180// c1->sendInterest(Name(n1), closure, selectors);
181// sleep(2);
182// c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
183// usleep(100000);
184// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
185// BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
186
187// string n2 = "/random/02";
188// selectors.interestLifetime(2);
189// c1->sendInterest(Name(n2), closure, selectors);
190// sleep(1);
191// c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
192// usleep(100000);
193// BOOST_CHECK_EQUAL(g_timeout_counter, 1);
194// BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
195
196// // reset
197// g_dataCallback_counter = 0;
198// g_timeout_counter = 0;
199// }
200
201BOOST_AUTO_TEST_SUITE_END()