blob: 473d117da74bb11e472a3c40ab68391ea5db1f82 [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 Afanasyev158ec0d2012-04-05 13:48:55 -070030#include "sync-log.h"
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070031
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070032#include "sync-app-socket.h"
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070033extern "C" {
34#include <unistd.h>
35}
36
37using namespace Sync;
38using namespace std;
39using namespace boost;
40
Alexander Afanasyev7df73332012-03-15 12:31:49 -070041INIT_LOGGER ("Test.AppSocket");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070042
Zhenkai Zhua30e1782012-06-01 11:52:57 -070043#define PRINT
44//std::cout << "Line: " << __LINE__ << std::endl;
45
Zhenkai Zhu00304002012-03-12 21:32:30 -070046class TestSocketApp {
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070047public:
48 map<string, string> data;
49 void set(string str1, string str2) {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -070050 // _LOG_FUNCTION (this << ", " << str1);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070051 data.insert(make_pair(str1, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070052 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070053 }
Zhenkai Zhudc70a292012-06-01 14:00:59 -070054
55 void setNum(string str1, const char *buf, size_t len) {
56 int n = len / 4;
57 int *numbers = new int [n];
58 memcpy(numbers, buf, len);
59 for (int i = 0; i < n; i++) {
60 sum += numbers[i];
61 }
62 delete numbers;
63
64 }
65
66 int sum;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070067
Zhenkai Zhu43ae5c72012-05-31 23:18:45 -070068 void fetchAll(const vector<MissingDataInfo> &v, SyncAppSocket *socket) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070069 int n = v.size();
Zhenkai Zhua30e1782012-06-01 11:52:57 -070070
71 PRINT
72
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070073 for (int i = 0; i < n; i++) {
Zhenkai Zhua30e1782012-06-01 11:52:57 -070074 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -070075 //PRINT
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070076 socket->fetchString(v[i].prefix, s, bind(&TestSocketApp::set, this, _1, _2));
77 }
78 }
79 }
80
Zhenkai Zhudc70a292012-06-01 14:00:59 -070081 void fetchNumbers(const vector<MissingDataInfo> &v, SyncAppSocket *socket) {
82 int n = v.size();
83
84 PRINT
85
86 std::cout << "In fetchNumbers. size of v is: " << n << std::endl;
87 for (int i = 0; i < n; i++) {
Zhenkai Zhu68f04d52012-06-05 14:07:41 -070088 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 -070089 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
90 PRINT
91 socket->fetchRaw(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1, _2, _3));
92 }
93 }
94 }
95
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070096 void pass(const string &prefix) {
97 }
98
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070099 string toString(){
100 map<string, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -0700101 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700102 for (; it != data.end(); ++it){
103 str += "<";
104 str += it->first;
105 str += "|";
106 str += it->second;
107 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700108 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700109 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700110
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700111 return str;
112 }
113
114};
115
116BOOST_AUTO_TEST_CASE (AppSocketTest)
117{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700118 INIT_LOGGERS ();
119
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700120 TestSocketApp a1, a2, a3;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700121
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700122 string syncPrefix("/let/us/sync");
123 string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700124
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700125 _LOG_DEBUG ("s1");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700126 SyncAppSocket s1 (syncPrefix, bind(&TestSocketApp::fetchAll, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700127 this_thread::sleep (posix_time::milliseconds (50));
128 _LOG_DEBUG ("s2");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700129 SyncAppSocket s2 (syncPrefix, bind(&TestSocketApp::fetchAll, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700130 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700131 SyncAppSocket s3 (syncPrefix, bind(&TestSocketApp::fetchAll, &a3, _1, _2), bind(&TestSocketApp::pass, &a3, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700132 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700133
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700134 // single source
135 string data0 = "Very funny Scotty, now beam down my clothes";
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700136 _LOG_DEBUG ("s1 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700137 s1.publishString (p1, 0, data0, 10);
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700138 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700139
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700140 // from code logic, we won't be fetching our own data
141 a1.set(p1 + "/0/0", data0);
142 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700143 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu00304002012-03-12 21:32:30 -0700144
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700145 // single source, multiple data at once
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700146 string data1 = "Yes, give me that ketchup";
147 string data2 = "Don't look conspicuous, it draws fire";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700148
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700149 _LOG_DEBUG ("s1 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700150 s1.publishString (p1, 0, data1, 10);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700151 _LOG_DEBUG ("s1 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700152 s1.publishString (p1, 0, data2, 10);
Zhenkai Zhu699d7402012-05-29 16:47:39 -0700153 this_thread::sleep (posix_time::milliseconds (1000));
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700154
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700155 // from code logic, we won't be fetching our own data
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700156 a1.set(p1 + "/0/1", data1);
157 a1.set(p1 + "/0/2", data2);
158 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
159 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700160
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700161 // another single source
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700162 string data3 = "You surf the Internet, I surf the real world";
163 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
164 string data5 = "Real men wear pink. Why? Because their wives make them";
165 _LOG_DEBUG ("s3 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700166 s3.publishString(p3, 0, data3, 10);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700167 this_thread::sleep (posix_time::milliseconds (200));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700168
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700169 // another single source, multiple data at once
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700170 s2.publishString(p2, 0, data4, 10);
171 s2.publishString(p2, 0, data5, 10);
Zhenkai Zhu699d7402012-05-29 16:47:39 -0700172 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700173
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700174 // from code logic, we won't be fetching our own data
175 a3.set(p3 + "/0/0", data3);
176 a2.set(p2 + "/0/0", data4);
177 a2.set(p2 + "/0/1", data5);
178 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
179 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700180
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700181 // not sure weither this is simultanous data generation from multiple sources
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700182 _LOG_DEBUG ("Simultaneous publishing");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700183 string data6 = "Shakespeare says: 'Prose before hos.'";
184 string data7 = "Pick good people, talent never wears out";
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700185 s1.publishString(p1, 0, data6, 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700186 // this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700187 s2.publishString(p2, 0, data7, 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700188 this_thread::sleep (posix_time::milliseconds (1500));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700189
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700190 // from code logic, we won't be fetching our own data
191 a1.set(p1 + "/0/3", data6);
192 a2.set(p2 + "/0/2", data7);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700193 // a1.set(p1 + "/0/1", data6);
194 // a2.set(p2 + "/0/0", data7);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700195 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
196 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700197
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700198 _LOG_DEBUG("Begin new test");
199 std::cout << "Begin new Test " << std::endl;
200 string syncRawPrefix = "/this/is/the/prefix";
201 a1.sum = 0;
202 a2.sum = 0;
203 SyncAppSocket s4 (syncRawPrefix, bind(&TestSocketApp::fetchNumbers, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
204 SyncAppSocket s5 (syncRawPrefix, bind(&TestSocketApp::fetchNumbers, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
205
206 int num[5] = {0, 1, 2, 3, 4};
207
208 string p4 = "/xiaonei.com";
209 string p5 = "/mitbbs.com";
210
211 s4.publishRaw(p4, 0,(const char *) num, sizeof(num), 10);
212 a1.setNum(p4, (const char *) num, sizeof (num));
213
Zhenkai Zhu7c1fe782012-06-06 15:24:13 -0700214 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700215 BOOST_CHECK(a1.sum == a2.sum && a1.sum == 10);
216
217 int newNum[5] = {9, 7, 2, 1, 1};
218
219 s5.publishRaw(p5, 0,(const char *) newNum, sizeof(newNum), 10);
220 a2.setNum(p5, (const char *)newNum, sizeof (newNum));
221 this_thread::sleep (posix_time::milliseconds (1000));
222 BOOST_CHECK_EQUAL(a1.sum, a2.sum);
223 BOOST_CHECK_EQUAL(a1.sum, 30);
224
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700225 _LOG_DEBUG ("Finish");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700226}