blob: 0492264bba1a4b584ff838b83a171a101f1c6601 [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
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070032#include "ccnx/sync-app-socket.h"
33
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;
50 void set(string str1, string str2) {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -070051 // _LOG_FUNCTION (this << ", " << str1);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070052 data.insert(make_pair(str1, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070053 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070054 }
Zhenkai Zhudc70a292012-06-01 14:00:59 -070055
56 void setNum(string str1, const char *buf, size_t len) {
57 int n = len / 4;
58 int *numbers = new int [n];
59 memcpy(numbers, buf, len);
60 for (int i = 0; i < n; i++) {
61 sum += numbers[i];
62 }
63 delete numbers;
64
65 }
66
67 int sum;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070068
Zhenkai Zhu43ae5c72012-05-31 23:18:45 -070069 void fetchAll(const vector<MissingDataInfo> &v, SyncAppSocket *socket) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070070 int n = v.size();
Zhenkai Zhua30e1782012-06-01 11:52:57 -070071
72 PRINT
73
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070074 for (int i = 0; i < n; i++) {
Zhenkai Zhua30e1782012-06-01 11:52:57 -070075 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -070076 //PRINT
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070077 socket->fetchString(v[i].prefix, s, bind(&TestSocketApp::set, this, _1, _2));
78 }
79 }
80 }
81
Zhenkai Zhudc70a292012-06-01 14:00:59 -070082 void fetchNumbers(const vector<MissingDataInfo> &v, SyncAppSocket *socket) {
83 int n = v.size();
84
85 PRINT
86
87 std::cout << "In fetchNumbers. size of v is: " << n << std::endl;
88 for (int i = 0; i < n; i++) {
Zhenkai Zhu68f04d52012-06-05 14:07:41 -070089 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 -070090 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
91 PRINT
92 socket->fetchRaw(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1, _2, _3));
93 }
94 }
95 }
96
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070097 void pass(const string &prefix) {
98 }
99
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700100 string toString(){
101 map<string, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -0700102 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700103 for (; it != data.end(); ++it){
104 str += "<";
105 str += it->first;
106 str += "|";
107 str += it->second;
108 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700109 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700110 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700111
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700112 return str;
113 }
114
115};
116
117BOOST_AUTO_TEST_CASE (AppSocketTest)
118{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700119 INIT_LOGGERS ();
120
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700121 TestSocketApp a1, a2, a3;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700122
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700123 string syncPrefix("/let/us/sync");
124 string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700125
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700126 _LOG_DEBUG ("s1");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700127 SyncAppSocket s1 (syncPrefix, bind(&TestSocketApp::fetchAll, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700128 this_thread::sleep (posix_time::milliseconds (50));
129 _LOG_DEBUG ("s2");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700130 SyncAppSocket s2 (syncPrefix, bind(&TestSocketApp::fetchAll, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700131 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700132 SyncAppSocket s3 (syncPrefix, bind(&TestSocketApp::fetchAll, &a3, _1, _2), bind(&TestSocketApp::pass, &a3, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700133 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700134
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700135 // single source
136 string data0 = "Very funny Scotty, now beam down my clothes";
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700137 _LOG_DEBUG ("s1 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700138 s1.publishString (p1, 0, data0, 10);
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700139 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700140
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700141 // from code logic, we won't be fetching our own data
142 a1.set(p1 + "/0/0", data0);
143 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700144 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu00304002012-03-12 21:32:30 -0700145
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700146 // single source, multiple data at once
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700147 string data1 = "Yes, give me that ketchup";
148 string data2 = "Don't look conspicuous, it draws fire";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700149
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700150 _LOG_DEBUG ("s1 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700151 s1.publishString (p1, 0, data1, 10);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700152 _LOG_DEBUG ("s1 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700153 s1.publishString (p1, 0, data2, 10);
Zhenkai Zhu699d7402012-05-29 16:47:39 -0700154 this_thread::sleep (posix_time::milliseconds (1000));
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700155
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700156 // from code logic, we won't be fetching our own data
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700157 a1.set(p1 + "/0/1", data1);
158 a1.set(p1 + "/0/2", data2);
159 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
160 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700161
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700162 // another single source
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700163 string data3 = "You surf the Internet, I surf the real world";
164 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
165 string data5 = "Real men wear pink. Why? Because their wives make them";
166 _LOG_DEBUG ("s3 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700167 s3.publishString(p3, 0, data3, 10);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700168 this_thread::sleep (posix_time::milliseconds (200));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700169
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700170 // another single source, multiple data at once
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700171 s2.publishString(p2, 0, data4, 10);
172 s2.publishString(p2, 0, data5, 10);
Zhenkai Zhu699d7402012-05-29 16:47:39 -0700173 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700174
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700175 // from code logic, we won't be fetching our own data
176 a3.set(p3 + "/0/0", data3);
177 a2.set(p2 + "/0/0", data4);
178 a2.set(p2 + "/0/1", data5);
179 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
180 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700181
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700182 // not sure weither this is simultanous data generation from multiple sources
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700183 _LOG_DEBUG ("Simultaneous publishing");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700184 string data6 = "Shakespeare says: 'Prose before hos.'";
185 string data7 = "Pick good people, talent never wears out";
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700186 s1.publishString(p1, 0, data6, 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700187 // this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700188 s2.publishString(p2, 0, data7, 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700189 this_thread::sleep (posix_time::milliseconds (1500));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700190
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700191 // from code logic, we won't be fetching our own data
192 a1.set(p1 + "/0/3", data6);
193 a2.set(p2 + "/0/2", data7);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700194 // a1.set(p1 + "/0/1", data6);
195 // a2.set(p2 + "/0/0", data7);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700196 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
197 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700198
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700199 _LOG_DEBUG("Begin new test");
200 std::cout << "Begin new Test " << std::endl;
201 string syncRawPrefix = "/this/is/the/prefix";
202 a1.sum = 0;
203 a2.sum = 0;
204 SyncAppSocket s4 (syncRawPrefix, bind(&TestSocketApp::fetchNumbers, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
205 SyncAppSocket s5 (syncRawPrefix, bind(&TestSocketApp::fetchNumbers, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
206
207 int num[5] = {0, 1, 2, 3, 4};
208
209 string p4 = "/xiaonei.com";
210 string p5 = "/mitbbs.com";
211
212 s4.publishRaw(p4, 0,(const char *) num, sizeof(num), 10);
213 a1.setNum(p4, (const char *) num, sizeof (num));
214
Zhenkai Zhu7c1fe782012-06-06 15:24:13 -0700215 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700216 BOOST_CHECK(a1.sum == a2.sum && a1.sum == 10);
217
218 int newNum[5] = {9, 7, 2, 1, 1};
219
220 s5.publishRaw(p5, 0,(const char *) newNum, sizeof(newNum), 10);
221 a2.setNum(p5, (const char *)newNum, sizeof (newNum));
222 this_thread::sleep (posix_time::milliseconds (1000));
223 BOOST_CHECK_EQUAL(a1.sum, a2.sum);
224 BOOST_CHECK_EQUAL(a1.sum, 30);
225
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700226 _LOG_DEBUG ("Finish");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700227}