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 | { |
| 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 | |
| 64 | // cout << basename << ", " << name << ", " << seqno << endl; |
| 65 | } |
| 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 | |
| 83 | BOOST_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 | { |
| 92 | ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 10); |
| 93 | |
| 94 | int other = 10 + i+5; |
| 95 | ccnx->publishData (Name (baseName)(other), reinterpret_cast<const unsigned char*> (&other), sizeof(int), 10); |
| 96 | } |
| 97 | |
| 98 | int oneMore = 26; |
| 99 | ccnx->publishData (Name (baseName)(oneMore), reinterpret_cast<const unsigned char*> (&oneMore), sizeof(int), 10); |
| 100 | |
| 101 | FetcherTestData data; |
| 102 | |
| 103 | Fetcher fetcher (ccnx, |
| 104 | bind (&FetcherTestData::onData, &data, _1, _2, _3, _4, _5), |
| 105 | bind (&FetcherTestData::onComplete, &data, _1), |
| 106 | bind (&FetcherTestData::onFail, &data, _1), |
| 107 | Name ("/base"), 0, 26, |
| 108 | boost::posix_time::seconds (5)); // this time is not precise |
| 109 | |
| 110 | BOOST_CHECK_EQUAL (fetcher.IsActive (), false); |
| 111 | fetcher.RestartPipeline (); |
| 112 | BOOST_CHECK_EQUAL (fetcher.IsActive (), true); |
| 113 | |
| 114 | usleep(13000000); |
| 115 | BOOST_CHECK_EQUAL (data.m_failed, true); |
| 116 | BOOST_CHECK_EQUAL (data.differentNames.size (), 1); |
| 117 | BOOST_CHECK_EQUAL (data.segmentNames.size (), 10); |
| 118 | BOOST_CHECK_EQUAL (data.recvData.size (), 10); |
| 119 | BOOST_CHECK_EQUAL (data.recvContent.size (), 10); |
| 120 | |
| 121 | ostringstream recvData; |
| 122 | for (set<uint32_t>::iterator i = data.recvData.begin (); i != data.recvData.end (); i++) |
| 123 | recvData << *i << ", "; |
| 124 | |
| 125 | ostringstream recvContent; |
| 126 | for (set<uint32_t>::iterator i = data.recvContent.begin (); i != data.recvContent.end (); i++) |
| 127 | recvContent << *i << ", "; |
| 128 | |
| 129 | BOOST_CHECK_EQUAL (recvData.str (), recvContent.str ()); |
| 130 | } |
| 131 | |
| 132 | // BOOST_AUTO_TEST_CASE (CcnxWrapperSelector) |
| 133 | // { |
| 134 | |
| 135 | // Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1)); |
| 136 | |
| 137 | // Selectors selectors; |
| 138 | // selectors.interestLifetime(1); |
| 139 | |
| 140 | // string n1 = "/random/01"; |
| 141 | // c1->sendInterest(Name(n1), closure, selectors); |
| 142 | // sleep(2); |
| 143 | // c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4); |
| 144 | // usleep(100000); |
| 145 | // BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 146 | // BOOST_CHECK_EQUAL(g_dataCallback_counter, 0); |
| 147 | |
| 148 | // string n2 = "/random/02"; |
| 149 | // selectors.interestLifetime(2); |
| 150 | // c1->sendInterest(Name(n2), closure, selectors); |
| 151 | // sleep(1); |
| 152 | // c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4); |
| 153 | // usleep(100000); |
| 154 | // BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 155 | // BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
| 156 | |
| 157 | // // reset |
| 158 | // g_dataCallback_counter = 0; |
| 159 | // g_timeout_counter = 0; |
| 160 | // } |
| 161 | |
| 162 | BOOST_AUTO_TEST_SUITE_END() |