blob: 2a2dc002a776b8e008126ea8fab92a53296b32ca [file] [log] [blame]
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -08001/* -*- 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
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080022#include "fetch-manager.hpp"
23#include "ccnx-wrapper.hpp"
24#include "ccnx-common.hpp"
25#include "scheduler.hpp"
26#include "object-db.hpp"
27#include "object-manager.hpp"
28#include "content-server.hpp"
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080029#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 Zhu13d224d2013-01-23 19:40:25 -080037#include <ctime>
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080038
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080039#include "logging.hpp"
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080040
41INIT_LOGGER("Test.ServerAndFetch");
42
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070043using namespace Ndnx;
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080044using namespace std;
45using namespace boost;
46using namespace boost::filesystem;
47
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080048BOOST_AUTO_TEST_SUITE(TestServeAndFetch)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080049
50path root("test-server-and-fetch");
51path filePath = root / "random-file";
52unsigned char magic = 'm';
53int repeat = 1024 * 400;
54mutex mut;
55condition_variable cond;
56bool finished;
57int ack;
58
59void 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
80void teardown()
81{
82 if (exists(root))
83 {
84 remove_all(root);
85 }
86
87 ack = 0;
88 finished = false;
89}
90
91Name
92simpleMap(const Name &deviceName)
93{
94 return Name("/local");
95}
96
97void
98segmentCallback(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
109void
110finishCallback(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
118BOOST_AUTO_TEST_CASE (TestServeAndFetch)
119{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800120 INIT_LOGGERS ();
121
122 _LOG_DEBUG ("Setting up test environment ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800123 setup();
124
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700125 NdnxWrapperPtr ndnx_serve = make_shared<NdnxWrapper>();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800126 usleep(1000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700127 NdnxWrapperPtr ndnx_fetch = make_shared<NdnxWrapper>();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800128
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800129 Name deviceName("/test/device");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800130 Name localPrefix("/local");
131 Name broadcastPrefix("/broadcast");
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800132
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800133 const string APPNAME = "test-chronoshare";
134
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800135 time_t start = time(NULL);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800136 _LOG_DEBUG ("At time " << start << ", publish local file to database, this is extremely slow ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800137 // publish file to db
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700138 ObjectManager om(ndnx_serve, root, APPNAME);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800139 tuple<HashPtr, size_t> pub = om.localFileToObjects(filePath, deviceName);
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800140 time_t end = time(NULL);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800141 _LOG_DEBUG ("At time " << end <<", publish finally finished, used " << end - start << " seconds ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800142
143 ActionLogPtr dummyLog;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700144 ContentServer server(ndnx_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, 5);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800145 server.registerPrefix(localPrefix);
146 server.registerPrefix(broadcastPrefix);
147
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700148 FetchManager fm(ndnx_fetch, bind(simpleMap, _1), Name("/local/broadcast"));
Alexander Afanasyev678f3502013-01-23 17:09:37 -0800149 HashPtr hash = pub.get<0> ();
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800150 Name baseName = Name ("/")(deviceName)(APPNAME)("file")(hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800151
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800152 fm.Enqueue(deviceName, baseName, bind(segmentCallback, _1, _2, _3, _4), bind(finishCallback, _1, _2), 0, pub.get<1>() - 1);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800153
154 unique_lock<mutex> lock(mut);
155 system_time timeout = get_system_time() + posix_time::milliseconds(5000);
156 while (!finished)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800157 {
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800158 if (!cond.timed_wait(lock, timeout))
159 {
160 BOOST_FAIL ("Fetching has not finished after 5 seconds");
161 break;
162 }
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800163 }
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700164 ndnx_fetch->shutdown ();
165 ndnx_serve->shutdown ();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800166
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800167 _LOG_DEBUG ("Finish");
168 usleep(100000);
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800169
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800170 teardown();
171}
172
173BOOST_AUTO_TEST_SUITE_END()