blob: 6a6e04a4742d9796e928fd39d52ee5cde400f80d [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 Zhu00304002012-03-12 21:32:30 -070043class TestSocketApp {
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070044public:
45 map<string, string> data;
46 void set(string str1, string str2) {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -070047 // _LOG_FUNCTION (this << ", " << str1);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070048 data.insert(make_pair(str1, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070049 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070050 }
51
Zhenkai Zhu43ae5c72012-05-31 23:18:45 -070052 void fetchAll(const vector<MissingDataInfo> &v, SyncAppSocket *socket) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070053 int n = v.size();
54 for (int i = 0; i < n; i++) {
Zhenkai Zhu43ae5c72012-05-31 23:18:45 -070055 SeqNo s = v[i].low;
56 for(++s; s <= v[i].high; ++s) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070057 socket->fetchString(v[i].prefix, s, bind(&TestSocketApp::set, this, _1, _2));
58 }
59 }
60 }
61
62 void pass(const string &prefix) {
63 }
64
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070065 string toString(){
66 map<string, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -070067 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070068 for (; it != data.end(); ++it){
69 str += "<";
70 str += it->first;
71 str += "|";
72 str += it->second;
73 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070074 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070075 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070076
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070077 return str;
78 }
79
80};
81
82BOOST_AUTO_TEST_CASE (AppSocketTest)
83{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070084 INIT_LOGGERS ();
85
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070086 TestSocketApp a1, a2, a3;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070087
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070088 string syncPrefix("/let/us/sync");
89 string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070090
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070091 _LOG_DEBUG ("s1");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070092 SyncAppSocket s1 (syncPrefix, bind(&TestSocketApp::fetchAll, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070093 this_thread::sleep (posix_time::milliseconds (50));
94 _LOG_DEBUG ("s2");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070095 SyncAppSocket s2 (syncPrefix, bind(&TestSocketApp::fetchAll, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070096 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070097 SyncAppSocket s3 (syncPrefix, bind(&TestSocketApp::fetchAll, &a3, _1, _2), bind(&TestSocketApp::pass, &a3, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070098 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070099
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700100 // single source
101 string data0 = "Very funny Scotty, now beam down my clothes";
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700102 _LOG_DEBUG ("s1 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700103 s1.publishString (p1, 0, data0, 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700104 this_thread::sleep (posix_time::milliseconds (120));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700105
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700106 // from code logic, we won't be fetching our own data
107 a1.set(p1 + "/0/0", data0);
108 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700109 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu00304002012-03-12 21:32:30 -0700110
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700111 // single source, multiple data at once
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700112 string data1 = "Yes, give me that ketchup";
113 string data2 = "Don't look conspicuous, it draws fire";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700114
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700115 _LOG_DEBUG ("s1 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700116 s1.publishString (p1, 0, data1, 10);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700117 _LOG_DEBUG ("s1 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700118 s1.publishString (p1, 0, data2, 10);
Zhenkai Zhu699d7402012-05-29 16:47:39 -0700119 this_thread::sleep (posix_time::milliseconds (1000));
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700120
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700121 // from code logic, we won't be fetching our own data
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700122 a1.set(p1 + "/0/1", data1);
123 a1.set(p1 + "/0/2", data2);
124 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
125 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700126
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700127 // another single source
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700128 string data3 = "You surf the Internet, I surf the real world";
129 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
130 string data5 = "Real men wear pink. Why? Because their wives make them";
131 _LOG_DEBUG ("s3 publish");
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700132 s3.publishString(p3, 0, data3, 10);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700133 this_thread::sleep (posix_time::milliseconds (200));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700134
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700135 // another single source, multiple data at once
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700136 s2.publishString(p2, 0, data4, 10);
137 s2.publishString(p2, 0, data5, 10);
Zhenkai Zhu699d7402012-05-29 16:47:39 -0700138 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700139
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700140 // from code logic, we won't be fetching our own data
141 a3.set(p3 + "/0/0", data3);
142 a2.set(p2 + "/0/0", data4);
143 a2.set(p2 + "/0/1", data5);
144 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
145 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700146
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700147 // not sure weither this is simultanous data generation from multiple sources
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700148 _LOG_DEBUG ("Simultaneous publishing");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700149 string data6 = "Shakespeare says: 'Prose before hos.'";
150 string data7 = "Pick good people, talent never wears out";
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700151 s1.publishString(p1, 0, data6, 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700152 // this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700153 s2.publishString(p2, 0, data7, 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700154 this_thread::sleep (posix_time::milliseconds (1500));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700155
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700156 // from code logic, we won't be fetching our own data
157 a1.set(p1 + "/0/3", data6);
158 a2.set(p2 + "/0/2", data7);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700159 // a1.set(p1 + "/0/1", data6);
160 // a2.set(p2 + "/0/0", data7);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700161 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
162 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700163
164 _LOG_DEBUG ("Finish");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700165}