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" |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 24 | #include "ndnx-wrapper.h" |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 25 | #include <boost/test/unit_test.hpp> |
| 26 | #include <boost/make_shared.hpp> |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 27 | #include <boost/thread/thread.hpp> |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 28 | #include "logging.h" |
| 29 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 31 | INIT_LOGGER ("Test.FetchManager"); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 33 | using namespace Ndnx; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 34 | using namespace std; |
| 35 | using namespace boost; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(TestFetchManager) |
| 38 | |
| 39 | struct FetcherTestData |
| 40 | { |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 41 | set<uint64_t> recvData; |
| 42 | set<uint64_t> recvContent; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 43 | |
| 44 | set<Name> differentNames; |
| 45 | set<Name> segmentNames; |
| 46 | |
| 47 | bool m_done; |
| 48 | bool m_failed; |
| 49 | |
| 50 | FetcherTestData () |
| 51 | : m_done (false) |
| 52 | , m_failed (false) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 57 | onData (const Ndnx::Name &deviceName, const Ndnx::Name &basename, uint64_t seqno, Ndnx::PcoPtr pco) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 58 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 59 | _LOG_TRACE ("onData: " << seqno); |
| 60 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 61 | recvData.insert (seqno); |
| 62 | differentNames.insert (basename); |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 63 | Name name = basename; |
| 64 | name.appendComp(seqno); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 65 | segmentNames.insert (name); |
| 66 | |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 67 | BytesPtr data = pco->contentPtr (); |
| 68 | |
| 69 | if (data->size () == sizeof(int)) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 71 | recvContent.insert (*reinterpret_cast<const int*> (head(*data))); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 74 | // cout << "<<< " << basename << ", " << name << ", " << seqno << endl; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 78 | finish(const Ndnx::Name &deviceName, const Ndnx::Name &baseName) |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 79 | { |
| 80 | } |
| 81 | |
| 82 | void |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 83 | onComplete (Fetcher &fetcher) |
| 84 | { |
| 85 | m_done = true; |
| 86 | // cout << "Done" << endl; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | onFail (Fetcher &fetcher) |
| 91 | { |
| 92 | m_failed = true; |
| 93 | // cout << "Failed" << endl; |
| 94 | } |
| 95 | }; |
| 96 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 97 | void run() |
| 98 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 99 | NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> (); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 100 | |
| 101 | Name baseName ("/base"); |
| 102 | Name deviceName ("/device"); |
| 103 | |
| 104 | for (int i = 0; i < 10; i++) |
| 105 | { |
| 106 | usleep(100000); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 107 | ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | for (int i = 11; i < 50; i++) |
| 111 | { |
| 112 | usleep(100000); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 113 | ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 117 | |
| 118 | BOOST_AUTO_TEST_CASE (TestFetcher) |
| 119 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 120 | INIT_LOGGERS (); |
| 121 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 122 | NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> (); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 123 | |
| 124 | Name baseName ("/base"); |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 125 | Name deviceName ("/device"); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 126 | /* 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 */ |
| 127 | // this will allow us to test our pipeline of 6 |
| 128 | for (int i = 0; i < 10; i++) |
| 129 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 130 | ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 131 | } |
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 | for (int i = 15; i < 25; i++) |
| 134 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 135 | ndnx->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] | 136 | } |
| 137 | |
| 138 | int oneMore = 26; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 139 | ndnx->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] | 140 | |
| 141 | FetcherTestData data; |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 142 | ExecutorPtr executor = make_shared<Executor>(1); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 143 | executor->start (); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 145 | Fetcher fetcher (ndnx, |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 146 | executor, |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 147 | bind (&FetcherTestData::onData, &data, _1, _2, _3, _4), |
| 148 | bind (&FetcherTestData::finish, &data, _1, _2), |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 149 | bind (&FetcherTestData::onComplete, &data, _1), |
| 150 | bind (&FetcherTestData::onFail, &data, _1), |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 151 | deviceName, Name ("/base"), 0, 26, |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 152 | boost::posix_time::seconds (5)); // this time is not precise |
| 153 | |
| 154 | BOOST_CHECK_EQUAL (fetcher.IsActive (), false); |
| 155 | fetcher.RestartPipeline (); |
| 156 | BOOST_CHECK_EQUAL (fetcher.IsActive (), true); |
| 157 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 158 | usleep(7000000); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL (data.m_failed, true); |
| 160 | BOOST_CHECK_EQUAL (data.differentNames.size (), 1); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL (data.segmentNames.size (), 20); |
| 162 | BOOST_CHECK_EQUAL (data.recvData.size (), 20); |
| 163 | BOOST_CHECK_EQUAL (data.recvContent.size (), 20); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 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 << ", "; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 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 << ", "; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 173 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 174 | 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, "); |
| 175 | BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ()); |
| 176 | } |
| 177 | |
| 178 | BOOST_CHECK_EQUAL (fetcher.IsActive (), false); |
| 179 | fetcher.RestartPipeline (); |
| 180 | BOOST_CHECK_EQUAL (fetcher.IsActive (), true); |
| 181 | |
| 182 | usleep(7000000); |
| 183 | BOOST_CHECK_EQUAL (data.m_failed, true); |
| 184 | |
| 185 | // publishing missing pieces |
| 186 | for (int i = 0; i < 27; i++) |
| 187 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 188 | ndnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 1); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 189 | } |
| 190 | BOOST_CHECK_EQUAL (fetcher.IsActive (), false); |
| 191 | fetcher.RestartPipeline (); |
| 192 | BOOST_CHECK_EQUAL (fetcher.IsActive (), true); |
| 193 | |
| 194 | usleep(1000000); |
| 195 | BOOST_CHECK_EQUAL (data.m_done, true); |
| 196 | |
| 197 | { |
| 198 | ostringstream recvData; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 199 | 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] | 200 | recvData << *i << ", "; |
| 201 | |
| 202 | ostringstream recvContent; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 203 | 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] | 204 | recvContent << *i << ", "; |
| 205 | |
| 206 | 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, "); |
| 207 | BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ()); |
| 208 | } |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 209 | |
| 210 | executor->shutdown (); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 213 | |
| 214 | BOOST_AUTO_TEST_CASE (TestFetcher2) |
| 215 | { |
| 216 | INIT_LOGGERS (); |
| 217 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 218 | NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> (); |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 219 | |
| 220 | Name baseName ("/base"); |
| 221 | Name deviceName ("/device"); |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 222 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 223 | thread publishThread(run); |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 224 | |
| 225 | FetcherTestData data; |
| 226 | ExecutorPtr executor = make_shared<Executor>(1); |
| 227 | executor->start (); |
| 228 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 229 | Fetcher fetcher (ndnx, |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 230 | executor, |
| 231 | bind (&FetcherTestData::onData, &data, _1, _2, _3, _4), |
| 232 | bind (&FetcherTestData::finish, &data, _1, _2), |
| 233 | bind (&FetcherTestData::onComplete, &data, _1), |
| 234 | bind (&FetcherTestData::onFail, &data, _1), |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 235 | deviceName, baseName, 0, 49, |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 236 | boost::posix_time::seconds (5)); // this time is not precise |
| 237 | |
| 238 | BOOST_CHECK_EQUAL (fetcher.IsActive (), false); |
| 239 | fetcher.RestartPipeline (); |
| 240 | BOOST_CHECK_EQUAL (fetcher.IsActive (), true); |
| 241 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 242 | usleep(20000000); |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 243 | BOOST_CHECK_EQUAL (data.m_failed, true); |
| 244 | |
| 245 | executor->shutdown (); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 250 | // BOOST_AUTO_TEST_CASE (NdnxWrapperSelector) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 251 | // { |
| 252 | |
| 253 | // Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1)); |
| 254 | |
| 255 | // Selectors selectors; |
| 256 | // selectors.interestLifetime(1); |
| 257 | |
| 258 | // string n1 = "/random/01"; |
| 259 | // c1->sendInterest(Name(n1), closure, selectors); |
| 260 | // sleep(2); |
| 261 | // c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4); |
| 262 | // usleep(100000); |
| 263 | // BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 264 | // BOOST_CHECK_EQUAL(g_dataCallback_counter, 0); |
| 265 | |
| 266 | // string n2 = "/random/02"; |
| 267 | // selectors.interestLifetime(2); |
| 268 | // c1->sendInterest(Name(n2), closure, selectors); |
| 269 | // sleep(1); |
| 270 | // c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4); |
| 271 | // usleep(100000); |
| 272 | // BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 273 | // BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
| 274 | |
| 275 | // // reset |
| 276 | // g_dataCallback_counter = 0; |
| 277 | // g_timeout_counter = 0; |
| 278 | // } |
| 279 | |
| 280 | BOOST_AUTO_TEST_SUITE_END() |