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