Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "fetch-manager.hpp" |
| 22 | #include "fetcher.hpp" |
| 23 | #include "ccnx-wrapper.hpp" |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 24 | #include <boost/test/unit_test.hpp> |
| 25 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 26 | #include "logging.hpp" |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 27 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 29 | INIT_LOGGER ("Test.FetchManager"); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 31 | using namespace Ndnx; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 32 | using namespace std; |
| 33 | using namespace boost; |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(TestFetchManager) |
| 36 | |
| 37 | struct FetcherTestData |
| 38 | { |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 39 | set<uint64_t> recvData; |
| 40 | set<uint64_t> recvContent; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 41 | |
| 42 | set<Name> differentNames; |
| 43 | set<Name> segmentNames; |
| 44 | |
| 45 | bool m_done; |
| 46 | bool m_failed; |
| 47 | |
| 48 | FetcherTestData () |
| 49 | : m_done (false) |
| 50 | , m_failed (false) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 55 | 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] | 56 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 57 | _LOG_TRACE ("onData: " << seqno); |
| 58 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 59 | recvData.insert (seqno); |
| 60 | differentNames.insert (basename); |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 61 | Name name = basename; |
| 62 | name.appendComp(seqno); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 63 | segmentNames.insert (name); |
| 64 | |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 65 | BytesPtr data = pco->contentPtr (); |
| 66 | |
| 67 | if (data->size () == sizeof(int)) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 68 | { |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 69 | recvContent.insert (*reinterpret_cast<const int*> (head(*data))); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 72 | // cout << "<<< " << basename << ", " << name << ", " << seqno << endl; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 76 | finish(const Ndnx::Name &deviceName, const Ndnx::Name &baseName) |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 77 | { |
| 78 | } |
| 79 | |
| 80 | void |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 81 | onComplete (Fetcher &fetcher) |
| 82 | { |
| 83 | m_done = true; |
| 84 | // cout << "Done" << endl; |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | onFail (Fetcher &fetcher) |
| 89 | { |
| 90 | m_failed = true; |
| 91 | // cout << "Failed" << endl; |
| 92 | } |
| 93 | }; |
| 94 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 95 | void run() |
| 96 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 97 | NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> (); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 98 | |
| 99 | Name baseName ("/base"); |
| 100 | Name deviceName ("/device"); |
| 101 | |
| 102 | for (int i = 0; i < 10; i++) |
| 103 | { |
| 104 | usleep(100000); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 105 | 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] | 106 | } |
| 107 | |
| 108 | for (int i = 11; i < 50; i++) |
| 109 | { |
| 110 | usleep(100000); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 111 | 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] | 112 | } |
| 113 | |
| 114 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 115 | |
| 116 | BOOST_AUTO_TEST_CASE (TestFetcher) |
| 117 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 118 | INIT_LOGGERS (); |
| 119 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 120 | NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> (); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 121 | |
| 122 | Name baseName ("/base"); |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 123 | Name deviceName ("/device"); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 124 | /* 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 */ |
| 125 | // this will allow us to test our pipeline of 6 |
| 126 | for (int i = 0; i < 10; i++) |
| 127 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 128 | 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] | 129 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 131 | for (int i = 15; i < 25; i++) |
| 132 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 133 | 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] | 134 | } |
| 135 | |
| 136 | int oneMore = 26; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 137 | 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] | 138 | |
| 139 | FetcherTestData data; |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 140 | ExecutorPtr executor = make_shared<Executor>(1); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 141 | executor->start (); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 143 | Fetcher fetcher (ndnx, |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 144 | executor, |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 145 | bind (&FetcherTestData::onData, &data, _1, _2, _3, _4), |
| 146 | bind (&FetcherTestData::finish, &data, _1, _2), |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 147 | bind (&FetcherTestData::onComplete, &data, _1), |
| 148 | bind (&FetcherTestData::onFail, &data, _1), |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 149 | deviceName, Name ("/base"), 0, 26, |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 150 | boost::posix_time::seconds (5)); // this time is not precise |
| 151 | |
| 152 | BOOST_CHECK_EQUAL (fetcher.IsActive (), false); |
| 153 | fetcher.RestartPipeline (); |
| 154 | BOOST_CHECK_EQUAL (fetcher.IsActive (), true); |
| 155 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 156 | usleep(7000000); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL (data.m_failed, true); |
| 158 | BOOST_CHECK_EQUAL (data.differentNames.size (), 1); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL (data.segmentNames.size (), 20); |
| 160 | BOOST_CHECK_EQUAL (data.recvData.size (), 20); |
| 161 | BOOST_CHECK_EQUAL (data.recvContent.size (), 20); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 163 | { |
| 164 | ostringstream recvData; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 165 | 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] | 166 | recvData << *i << ", "; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 167 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 168 | ostringstream recvContent; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 169 | 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] | 170 | recvContent << *i << ", "; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 172 | 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, "); |
| 173 | BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ()); |
| 174 | } |
| 175 | |
| 176 | BOOST_CHECK_EQUAL (fetcher.IsActive (), false); |
| 177 | fetcher.RestartPipeline (); |
| 178 | BOOST_CHECK_EQUAL (fetcher.IsActive (), true); |
| 179 | |
| 180 | usleep(7000000); |
| 181 | BOOST_CHECK_EQUAL (data.m_failed, true); |
| 182 | |
| 183 | // publishing missing pieces |
| 184 | for (int i = 0; i < 27; i++) |
| 185 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 186 | 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] | 187 | } |
| 188 | BOOST_CHECK_EQUAL (fetcher.IsActive (), false); |
| 189 | fetcher.RestartPipeline (); |
| 190 | BOOST_CHECK_EQUAL (fetcher.IsActive (), true); |
| 191 | |
| 192 | usleep(1000000); |
| 193 | BOOST_CHECK_EQUAL (data.m_done, true); |
| 194 | |
| 195 | { |
| 196 | ostringstream recvData; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 197 | 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] | 198 | recvData << *i << ", "; |
| 199 | |
| 200 | ostringstream recvContent; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 201 | 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] | 202 | recvContent << *i << ", "; |
| 203 | |
| 204 | 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, "); |
| 205 | BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ()); |
| 206 | } |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 207 | |
| 208 | executor->shutdown (); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 211 | |
| 212 | BOOST_AUTO_TEST_CASE (TestFetcher2) |
| 213 | { |
| 214 | INIT_LOGGERS (); |
| 215 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 216 | NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> (); |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 217 | |
| 218 | Name baseName ("/base"); |
| 219 | Name deviceName ("/device"); |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 220 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 221 | thread publishThread(run); |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 222 | |
| 223 | FetcherTestData data; |
| 224 | ExecutorPtr executor = make_shared<Executor>(1); |
| 225 | executor->start (); |
| 226 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 227 | Fetcher fetcher (ndnx, |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 228 | executor, |
| 229 | bind (&FetcherTestData::onData, &data, _1, _2, _3, _4), |
| 230 | bind (&FetcherTestData::finish, &data, _1, _2), |
| 231 | bind (&FetcherTestData::onComplete, &data, _1), |
| 232 | bind (&FetcherTestData::onFail, &data, _1), |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 233 | deviceName, baseName, 0, 49, |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 234 | boost::posix_time::seconds (5)); // this time is not precise |
| 235 | |
| 236 | BOOST_CHECK_EQUAL (fetcher.IsActive (), false); |
| 237 | fetcher.RestartPipeline (); |
| 238 | BOOST_CHECK_EQUAL (fetcher.IsActive (), true); |
| 239 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 240 | usleep(20000000); |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL (data.m_failed, true); |
| 242 | |
| 243 | executor->shutdown (); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 248 | // BOOST_AUTO_TEST_CASE (NdnxWrapperSelector) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 249 | // { |
| 250 | |
| 251 | // Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1)); |
| 252 | |
| 253 | // Selectors selectors; |
| 254 | // selectors.interestLifetime(1); |
| 255 | |
| 256 | // string n1 = "/random/01"; |
| 257 | // c1->sendInterest(Name(n1), closure, selectors); |
| 258 | // sleep(2); |
| 259 | // c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4); |
| 260 | // usleep(100000); |
| 261 | // BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 262 | // BOOST_CHECK_EQUAL(g_dataCallback_counter, 0); |
| 263 | |
| 264 | // string n2 = "/random/02"; |
| 265 | // selectors.interestLifetime(2); |
| 266 | // c1->sendInterest(Name(n2), closure, selectors); |
| 267 | // sleep(1); |
| 268 | // c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4); |
| 269 | // usleep(100000); |
| 270 | // BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 271 | // BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
| 272 | |
| 273 | // // reset |
| 274 | // g_dataCallback_counter = 0; |
| 275 | // g_timeout_counter = 0; |
| 276 | // } |
| 277 | |
| 278 | BOOST_AUTO_TEST_SUITE_END() |