blob: 05782c2093e97ea98837a2b454523c3705fc73c0 [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
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 Zhu13d224d2013-01-23 19:40:25 -080037#include <ctime>
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080038
39using namespace Ccnx;
40using namespace std;
41using namespace boost;
42using namespace boost::filesystem;
43
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080044BOOST_AUTO_TEST_SUITE(TestServeAndFetch)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080045
46path root("test-server-and-fetch");
47path filePath = root / "random-file";
48unsigned char magic = 'm';
49int repeat = 1024 * 400;
50mutex mut;
51condition_variable cond;
52bool finished;
53int ack;
54
55void 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
76void teardown()
77{
78 if (exists(root))
79 {
80 remove_all(root);
81 }
82
83 ack = 0;
84 finished = false;
85}
86
87Name
88simpleMap(const Name &deviceName)
89{
90 return Name("/local");
91}
92
93void
94segmentCallback(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
105void
106finishCallback(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
114BOOST_AUTO_TEST_CASE (TestServeAndFetch)
115{
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800116 cout << "Setting up test environment ..." << endl;
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800117 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 Zhu13d224d2013-01-23 19:40:25 -0800124 Name deviceName("/test/device");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800125 Name localPrefix("/local");
126 Name broadcastPrefix("/broadcast");
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800127
128 time_t start = time(NULL);
129 cout << "At time " << start << ", publish local file to database, this is extremely slow ..." << endl;
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800130 // publish file to db
131 tuple<HashPtr, size_t> pub = om.localFileToObjects(filePath, deviceName);
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800132 time_t end = time(NULL);
133 cout << "At time " << end <<", publish finally finished, used " << end - start << " seconds ..."<< endl;
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800134
135 ActionLogPtr dummyLog;
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800136 ContentServer server(ccnx_serve, dummyLog, root, 5);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800137 server.registerPrefix(localPrefix);
138 server.registerPrefix(broadcastPrefix);
139
140 FetchManager fm(ccnx_fetch, bind(simpleMap, _1));
Alexander Afanasyev678f3502013-01-23 17:09:37 -0800141 HashPtr hash = pub.get<0> ();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800142 Name baseName = Name (deviceName)("file")(hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800143 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 -0800144
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 Zhu13d224d2013-01-23 19:40:25 -0800156 cout << "ack : " << ack << endl;
157
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800158 teardown();
159}
160
161BOOST_AUTO_TEST_SUITE_END()