Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 28 | using namespace Ccnx; |
| 29 | using namespace std; |
| 30 | using namespace boost; |
| 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(TestFetchManager) |
| 33 | |
| 34 | struct FetcherTestData |
| 35 | { |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 36 | set<uint64_t> recvData; |
| 37 | set<uint64_t> recvContent; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 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 |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 52 | onData (const Ccnx::Name &deviceName, const Ccnx::Name &basename, uint64_t seqno, Ccnx::PcoPtr pco) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 53 | { |
| 54 | recvData.insert (seqno); |
| 55 | differentNames.insert (basename); |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 56 | Name name = basename; |
| 57 | name.appendComp(seqno); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 58 | segmentNames.insert (name); |
| 59 | |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 60 | BytesPtr data = pco->contentPtr (); |
| 61 | |
| 62 | if (data->size () == sizeof(int)) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 63 | { |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 64 | recvContent.insert (*reinterpret_cast<const int*> (head(*data))); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 67 | // cout << "<<< " << basename << ", " << name << ", " << seqno << endl; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 71 | finish(const Ccnx::Name &deviceName, const Ccnx::Name &baseName) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | void |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 76 | 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 | |
| 91 | BOOST_AUTO_TEST_CASE (TestFetcher) |
| 92 | { |
| 93 | CcnxWrapperPtr ccnx = make_shared<CcnxWrapper> (); |
| 94 | |
| 95 | Name baseName ("/base"); |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 96 | Name deviceName ("/device"); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 97 | /* 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 Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 101 | ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30); |
| 102 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 104 | for (int i = 15; i < 25; i++) |
| 105 | { |
| 106 | ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int oneMore = 26; |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 110 | ccnx->publishData (Name (baseName)(oneMore), reinterpret_cast<const unsigned char*> (&oneMore), sizeof(int), 30); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 111 | |
| 112 | FetcherTestData data; |
| 113 | |
| 114 | Fetcher fetcher (ccnx, |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 115 | bind (&FetcherTestData::onData, &data, _1, _2, _3, _4), |
| 116 | bind (&FetcherTestData::finish, &data, _1, _2), |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 117 | bind (&FetcherTestData::onComplete, &data, _1), |
| 118 | bind (&FetcherTestData::onFail, &data, _1), |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 119 | deviceName, Name ("/base"), 0, 26, |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 120 | 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 Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 126 | usleep(7000000); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 127 | BOOST_CHECK_EQUAL (data.m_failed, true); |
| 128 | BOOST_CHECK_EQUAL (data.differentNames.size (), 1); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL (data.segmentNames.size (), 20); |
| 130 | BOOST_CHECK_EQUAL (data.recvData.size (), 20); |
| 131 | BOOST_CHECK_EQUAL (data.recvContent.size (), 20); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 133 | { |
| 134 | ostringstream recvData; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 135 | for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++) |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 136 | recvData << *i << ", "; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 137 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 138 | ostringstream recvContent; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 139 | for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++) |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 140 | recvContent << *i << ", "; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 142 | 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 Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 167 | for (set<uint64_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++) |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 168 | recvData << *i << ", "; |
| 169 | |
| 170 | ostringstream recvContent; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 171 | for (set<uint64_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++) |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 172 | 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 Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 177 | } |
| 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 | |
| 209 | BOOST_AUTO_TEST_SUITE_END() |