blob: 2305230479ff557f5d5c197fb7f42dc2cf3d160c [file] [log] [blame]
Zhenkai Zhu64db03a2012-03-12 19:25:56 -07001/* -*- 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: Zhenkai Zhu <zhenkai@cs.ucla.edu>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include <boost/test/unit_test.hpp>
24#include <boost/test/output_test_stream.hpp>
25using boost::test_tools::output_test_stream;
26
27#include <boost/make_shared.hpp>
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070028#include <boost/date_time/posix_time/posix_time.hpp>
29
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070030#include "sync-logging.h"
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070031
Yingdi Yu43e71612013-10-30 22:19:31 -070032#include "ccnx/sync-socket.h"
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070033
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070034extern "C" {
35#include <unistd.h>
36}
37
38using namespace Sync;
39using namespace std;
40using namespace boost;
41
Alexander Afanasyev7df73332012-03-15 12:31:49 -070042INIT_LOGGER ("Test.AppSocket");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070043
Zhenkai Zhua30e1782012-06-01 11:52:57 -070044#define PRINT
45//std::cout << "Line: " << __LINE__ << std::endl;
46
Zhenkai Zhu00304002012-03-12 21:32:30 -070047class TestSocketApp {
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070048public:
49 map<string, string> data;
Yingdi Yu43e71612013-10-30 22:19:31 -070050 void set(ndn::Ptr<ndn::Data> dataPacket) {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -070051 // _LOG_FUNCTION (this << ", " << str1);
Yingdi Yu43e71612013-10-30 22:19:31 -070052 string str1(dataPacket->getName().toUri());
53 string str2(dataPacket->content().buf(), dataPacket->content().size());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070054 data.insert(make_pair(str1, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070055 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070056 }
Yingdi Yu43e71612013-10-30 22:19:31 -070057
58 void set(string str1, const char * buf, int len) {
59 string str2(buf, len);
60 data.insert(make_pair(str1, str2));
61 }
Zhenkai Zhudc70a292012-06-01 14:00:59 -070062
Yingdi Yu43e71612013-10-30 22:19:31 -070063 void setNum(ndn::Ptr<ndn::Data> dataPacket) {
64 int n = dataPacket->content().size() / 4;
Zhenkai Zhudc70a292012-06-01 14:00:59 -070065 int *numbers = new int [n];
Yingdi Yu43e71612013-10-30 22:19:31 -070066 memcpy(numbers, dataPacket->content().buf(), dataPacket->content().size());
Zhenkai Zhudc70a292012-06-01 14:00:59 -070067 for (int i = 0; i < n; i++) {
68 sum += numbers[i];
69 }
70 delete numbers;
71
72 }
73
Yingdi Yu43e71612013-10-30 22:19:31 -070074 void setNum(string str1, const char * buf, int len) {
75 int n = len / 4;
76 int *numbers = new int [n];
77 memcpy(numbers, buf, len);
78 for (int i = 0; i < n; i++) {
79 sum += numbers[i];
80 }
81 delete numbers;
82 }
83
Zhenkai Zhudc70a292012-06-01 14:00:59 -070084 int sum;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070085
Yingdi Yu43e71612013-10-30 22:19:31 -070086 void fetchAll(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070087 int n = v.size();
Zhenkai Zhua30e1782012-06-01 11:52:57 -070088
89 PRINT
90
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070091 for (int i = 0; i < n; i++) {
Zhenkai Zhua30e1782012-06-01 11:52:57 -070092 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -070093 //PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -070094 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::set, this, _1));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070095 }
96 }
97 }
98
Yingdi Yu43e71612013-10-30 22:19:31 -070099 void fetchNumbers(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700100 int n = v.size();
101
102 PRINT
103
104 std::cout << "In fetchNumbers. size of v is: " << n << std::endl;
105 for (int i = 0; i < n; i++) {
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700106 std::cout << "In fetchNumbers. v[i].low is (" <<v[i].low.getSession() <<", " << v[i].low.getSeq() << ") v[i].high is ("<<v[i].high.getSession() <<", " <<v[i].high.getSeq()<<")" << std::endl;
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700107 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
108 PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -0700109 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700110 }
111 }
112 }
113
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700114 void pass(const string &prefix) {
115 }
116
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700117 string toString(){
118 map<string, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -0700119 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700120 for (; it != data.end(); ++it){
121 str += "<";
122 str += it->first;
123 str += "|";
124 str += it->second;
125 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700126 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700127 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700128
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700129 return str;
130 }
131
132};
133
134BOOST_AUTO_TEST_CASE (AppSocketTest)
135{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700136 INIT_LOGGERS ();
137
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700138 TestSocketApp a1, a2, a3;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700139
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700140 string syncPrefix("/let/us/sync");
141 string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700142
Yingdi Yu43e71612013-10-30 22:19:31 -0700143 ndn::Ptr<SyncPolicyManager> policyManager1 = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(ndn::Name("/ndn/ucla.edu/alice"), ndn::Name("/ndn/ucla.edu/alice/KEY/dsk-1382934202/ID-CERT/%FD%FF%FF%FF%FF%DEk%C0%0B"), ndn::Name(syncPrefix)));
144
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700145 _LOG_DEBUG ("s1");
Yingdi Yu43e71612013-10-30 22:19:31 -0700146 SyncSocket s1 (syncPrefix, policyManager1, bind(&TestSocketApp::fetchAll, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700147 this_thread::sleep (posix_time::milliseconds (50));
148 _LOG_DEBUG ("s2");
Yingdi Yu43e71612013-10-30 22:19:31 -0700149 SyncSocket s2 (syncPrefix, policyManager1, bind(&TestSocketApp::fetchAll, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700150 this_thread::sleep (posix_time::milliseconds (50));
Yingdi Yu43e71612013-10-30 22:19:31 -0700151 SyncSocket s3 (syncPrefix, policyManager1, bind(&TestSocketApp::fetchAll, &a3, _1, _2), bind(&TestSocketApp::pass, &a3, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700152 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700153
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700154 // single source
155 string data0 = "Very funny Scotty, now beam down my clothes";
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700156 _LOG_DEBUG ("s1 publish");
Yingdi Yu43e71612013-10-30 22:19:31 -0700157 s1.publishData (p1, 0, data0.c_str(), data0.size(), 10);
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700158 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700159
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700160 // from code logic, we won't be fetching our own data
Yingdi Yu43e71612013-10-30 22:19:31 -0700161 a1.set(p1 + "/0/0", data0.c_str(), data0.size());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700162 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700163 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu00304002012-03-12 21:32:30 -0700164
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700165 // single source, multiple data at once
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700166 string data1 = "Yes, give me that ketchup";
167 string data2 = "Don't look conspicuous, it draws fire";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700168
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700169 _LOG_DEBUG ("s1 publish");
Yingdi Yu43e71612013-10-30 22:19:31 -0700170 s1.publishData (p1, 0, data1.c_str(), data1.size(), 10);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700171 _LOG_DEBUG ("s1 publish");
Yingdi Yu43e71612013-10-30 22:19:31 -0700172 s1.publishData (p1, 0, data2.c_str(), data2.size(), 10);
Zhenkai Zhu699d7402012-05-29 16:47:39 -0700173 this_thread::sleep (posix_time::milliseconds (1000));
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700174
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700175 // from code logic, we won't be fetching our own data
Yingdi Yu43e71612013-10-30 22:19:31 -0700176 a1.set(p1 + "/0/1", data1.c_str(), data1.size());
177 a1.set(p1 + "/0/2", data2.c_str(), data2.size());
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700178 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
179 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700180
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700181 // another single source
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700182 string data3 = "You surf the Internet, I surf the real world";
183 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
184 string data5 = "Real men wear pink. Why? Because their wives make them";
185 _LOG_DEBUG ("s3 publish");
Yingdi Yu43e71612013-10-30 22:19:31 -0700186 s3.publishData(p3, 0, data3.c_str(), data3.size(), 10);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700187 this_thread::sleep (posix_time::milliseconds (200));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700188
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700189 // another single source, multiple data at once
Yingdi Yu43e71612013-10-30 22:19:31 -0700190 s2.publishData(p2, 0, data4.c_str(), data4.size(), 10);
191 s2.publishData(p2, 0, data5.c_str(), data5.size(), 10);
Zhenkai Zhu699d7402012-05-29 16:47:39 -0700192 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700193
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700194 // from code logic, we won't be fetching our own data
Yingdi Yu43e71612013-10-30 22:19:31 -0700195 a3.set(p3 + "/0/0", data3.c_str(), data3.size());
196 a2.set(p2 + "/0/0", data4.c_str(), data4.size());
197 a2.set(p2 + "/0/1", data5.c_str(), data5.size());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700198 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
199 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700200
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700201 // not sure weither this is simultanous data generation from multiple sources
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700202 _LOG_DEBUG ("Simultaneous publishing");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700203 string data6 = "Shakespeare says: 'Prose before hos.'";
204 string data7 = "Pick good people, talent never wears out";
Yingdi Yu43e71612013-10-30 22:19:31 -0700205 s1.publishData(p1, 0, data6.c_str(), data6.size(), 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700206 // this_thread::sleep (posix_time::milliseconds (1000));
Yingdi Yu43e71612013-10-30 22:19:31 -0700207 s2.publishData(p2, 0, data7.c_str(), data7.size(), 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700208 this_thread::sleep (posix_time::milliseconds (1500));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700209
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700210 // from code logic, we won't be fetching our own data
Yingdi Yu43e71612013-10-30 22:19:31 -0700211 a1.set(p1 + "/0/3", data6.c_str(), data6.size());
212 a2.set(p2 + "/0/2", data7.c_str(), data7.size());
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700213 // a1.set(p1 + "/0/1", data6);
214 // a2.set(p2 + "/0/0", data7);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700215 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
216 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700217
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700218 _LOG_DEBUG("Begin new test");
219 std::cout << "Begin new Test " << std::endl;
220 string syncRawPrefix = "/this/is/the/prefix";
Yingdi Yu43e71612013-10-30 22:19:31 -0700221 ndn::Ptr<SyncPolicyManager> policyManager2 = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(ndn::Name("/ndn/ucla.edu/alice"), ndn::Name("/ndn/ucla.edu/alice/KEY/dsk-1382934202/ID-CERT/%FD%FF%FF%FF%FF%DEk%C0%0B"), ndn::Name(syncRawPrefix)));
222
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700223 a1.sum = 0;
224 a2.sum = 0;
Yingdi Yu43e71612013-10-30 22:19:31 -0700225 SyncSocket s4 (syncRawPrefix, policyManager2, bind(&TestSocketApp::fetchNumbers, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
226 SyncSocket s5 (syncRawPrefix, policyManager2, bind(&TestSocketApp::fetchNumbers, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700227
228 int num[5] = {0, 1, 2, 3, 4};
229
230 string p4 = "/xiaonei.com";
231 string p5 = "/mitbbs.com";
232
Yingdi Yu43e71612013-10-30 22:19:31 -0700233 s4.publishData(p4, 0,(const char *) num, sizeof(num), 10);
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700234 a1.setNum(p4, (const char *) num, sizeof (num));
235
Zhenkai Zhu7c1fe782012-06-06 15:24:13 -0700236 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700237 BOOST_CHECK(a1.sum == a2.sum && a1.sum == 10);
238
239 int newNum[5] = {9, 7, 2, 1, 1};
240
Yingdi Yu43e71612013-10-30 22:19:31 -0700241 s5.publishData(p5, 0,(const char *) newNum, sizeof(newNum), 10);
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700242 a2.setNum(p5, (const char *)newNum, sizeof (newNum));
243 this_thread::sleep (posix_time::milliseconds (1000));
244 BOOST_CHECK_EQUAL(a1.sum, a2.sum);
245 BOOST_CHECK_EQUAL(a1.sum, 30);
246
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700247 _LOG_DEBUG ("Finish");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700248}