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