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