Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39: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 "ccnx-wrapper.h" |
| 24 | #include "ccnx-common.h" |
| 25 | #include "scheduler.h" |
| 26 | #include "object-db.h" |
| 27 | #include "object-manager.h" |
| 28 | #include "content-server.h" |
| 29 | #include <boost/test/unit_test.hpp> |
| 30 | #include <boost/make_shared.hpp> |
| 31 | #include <boost/filesystem.hpp> |
| 32 | #include <boost/thread/mutex.hpp> |
| 33 | #include <boost/thread/locks.hpp> |
| 34 | #include <boost/thread/thread_time.hpp> |
| 35 | #include <boost/thread/condition_variable.hpp> |
| 36 | #include <stdio.h> |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 37 | #include <ctime> |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 39 | #include "logging.h" |
| 40 | |
| 41 | INIT_LOGGER("Test.ServerAndFetch"); |
| 42 | |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 43 | using namespace Ccnx; |
| 44 | using namespace std; |
| 45 | using namespace boost; |
| 46 | using namespace boost::filesystem; |
| 47 | |
Alexander Afanasyev | 2a6fa2b | 2013-01-23 16:51:55 -0800 | [diff] [blame] | 48 | BOOST_AUTO_TEST_SUITE(TestServeAndFetch) |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 49 | |
| 50 | path root("test-server-and-fetch"); |
| 51 | path filePath = root / "random-file"; |
| 52 | unsigned char magic = 'm'; |
| 53 | int repeat = 1024 * 400; |
| 54 | mutex mut; |
| 55 | condition_variable cond; |
| 56 | bool finished; |
| 57 | int ack; |
| 58 | |
| 59 | void setup() |
| 60 | { |
| 61 | if (exists(root)) |
| 62 | { |
| 63 | remove_all(root); |
| 64 | } |
| 65 | |
| 66 | create_directory(root); |
| 67 | |
| 68 | // create file |
| 69 | FILE *fp = fopen(filePath.string().c_str(), "w"); |
| 70 | for (int i = 0; i < repeat; i++) |
| 71 | { |
| 72 | fwrite(&magic, 1, sizeof(magic), fp); |
| 73 | } |
| 74 | fclose(fp); |
| 75 | |
| 76 | ack = 0; |
| 77 | finished = false; |
| 78 | } |
| 79 | |
| 80 | void teardown() |
| 81 | { |
| 82 | if (exists(root)) |
| 83 | { |
| 84 | remove_all(root); |
| 85 | } |
| 86 | |
| 87 | ack = 0; |
| 88 | finished = false; |
| 89 | } |
| 90 | |
| 91 | Name |
| 92 | simpleMap(const Name &deviceName) |
| 93 | { |
| 94 | return Name("/local"); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | segmentCallback(const Name &deviceName, const Name &baseName, uint64_t seq, PcoPtr pco) |
| 99 | { |
| 100 | ack++; |
| 101 | Bytes co = pco->content(); |
| 102 | int size = co.size(); |
| 103 | for (int i = 0; i < size; i++) |
| 104 | { |
| 105 | BOOST_CHECK_EQUAL(co[i], magic); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | finishCallback(Name &deviceName, Name &baseName) |
| 111 | { |
| 112 | BOOST_CHECK_EQUAL(ack, repeat / 1024); |
| 113 | unique_lock<mutex> lock(mut); |
| 114 | finished = true; |
| 115 | cond.notify_one(); |
| 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_CASE (TestServeAndFetch) |
| 119 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 120 | INIT_LOGGERS (); |
| 121 | |
| 122 | _LOG_DEBUG ("Setting up test environment ..."); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 123 | setup(); |
| 124 | |
| 125 | CcnxWrapperPtr ccnx_serve = make_shared<CcnxWrapper>(); |
| 126 | usleep(1000); |
| 127 | CcnxWrapperPtr ccnx_fetch = make_shared<CcnxWrapper>(); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 128 | |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 129 | Name deviceName("/test/device"); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 130 | Name localPrefix("/local"); |
| 131 | Name broadcastPrefix("/broadcast"); |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 133 | const string APPNAME = "test-chronoshare"; |
| 134 | |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 135 | time_t start = time(NULL); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 136 | _LOG_DEBUG ("At time " << start << ", publish local file to database, this is extremely slow ..."); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 137 | // publish file to db |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 138 | ObjectManager om(ccnx_serve, root, APPNAME); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 139 | tuple<HashPtr, size_t> pub = om.localFileToObjects(filePath, deviceName); |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 140 | time_t end = time(NULL); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 141 | _LOG_DEBUG ("At time " << end <<", publish finally finished, used " << end - start << " seconds ..."); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 142 | |
| 143 | ActionLogPtr dummyLog; |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 144 | ContentServer server(ccnx_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, 5); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 145 | server.registerPrefix(localPrefix); |
| 146 | server.registerPrefix(broadcastPrefix); |
| 147 | |
| 148 | FetchManager fm(ccnx_fetch, bind(simpleMap, _1)); |
Alexander Afanasyev | 678f350 | 2013-01-23 17:09:37 -0800 | [diff] [blame] | 149 | HashPtr hash = pub.get<0> (); |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame^] | 150 | Name baseName = Name ("/")(deviceName)(APPNAME)("file")(hash->GetHash(), hash->GetHashBytes()); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 151 | |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 152 | fm.Enqueue(deviceName, baseName, bind(segmentCallback, _1, _2, _3, _4), bind(finishCallback, _1, _2), 0, pub.get<1>() - 1); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 153 | |
| 154 | unique_lock<mutex> lock(mut); |
| 155 | system_time timeout = get_system_time() + posix_time::milliseconds(5000); |
| 156 | while (!finished) |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 157 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 158 | if (!cond.timed_wait(lock, timeout)) |
| 159 | { |
| 160 | BOOST_FAIL ("Fetching has not finished after 5 seconds"); |
| 161 | break; |
| 162 | } |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 163 | } |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 164 | ccnx_fetch->shutdown (); |
| 165 | ccnx_serve->shutdown (); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 166 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 167 | _LOG_DEBUG ("Finish"); |
| 168 | usleep(100000); |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 169 | |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 170 | teardown(); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_SUITE_END() |