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