blob: 4773c36814887afc0ee9b7e0f9ca62eafc3dda3b [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>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * 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
30#include "../model/sync-log.h"
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070031
32#include "../model/sync-app-socket.h"
33extern "C" {
34#include <unistd.h>
35}
36
37using namespace Sync;
38using namespace std;
39using namespace boost;
40
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070041INIT_LOGGER ("Test::AppSocket");
42
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) {
47 data.insert(make_pair(str1, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070048 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070049 }
50
51 string toString(){
52 map<string, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -070053 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070054 for (; it != data.end(); ++it){
55 str += "<";
56 str += it->first;
57 str += "|";
58 str += it->second;
59 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070060 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070061 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070062
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070063 return str;
64 }
65
66};
67
68BOOST_AUTO_TEST_CASE (AppSocketTest)
69{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070070 INIT_LOGGERS ();
71
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070072 TestSocketApp a1, a2, a3;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070073
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070074 string syncPrefix("/let/us/sync");
75 string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070076
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070077 _LOG_DEBUG ("s1");
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070078 SyncAppSocket s1 (syncPrefix, bind(&TestSocketApp::set, &a1, _1, _2));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070079 this_thread::sleep (posix_time::milliseconds (50));
80 _LOG_DEBUG ("s2");
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070081 SyncAppSocket s2 (syncPrefix, bind(&TestSocketApp::set, &a2, _1, _2));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070082 this_thread::sleep (posix_time::milliseconds (50));
83 SyncAppSocket s3 (syncPrefix, bind(&TestSocketApp::set, &a3, _1, _2));
84 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070085
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070086 // single source
87 string data0 = "Very funny Scotty, now beam down my clothes";
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070088 _LOG_DEBUG ("s1 publish");
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070089 s1.publish (p1, 0, data0, 10);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070090 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu00304002012-03-12 21:32:30 -070091
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070092 // from code logic, we won't be fetching our own data
93 a1.set(p1 + "/0/0", data0);
94 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070095 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu00304002012-03-12 21:32:30 -070096
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070097 // // single source, multiple data at once
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070098 string data1 = "Yes, give me that ketchup";
99 string data2 = "Don't look conspicuous, it draws fire";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700100
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700101 _LOG_DEBUG ("s1 publish");
102 s1.publish (p1, 0, data1, 10);
103 _LOG_DEBUG ("s1 publish");
104 s1.publish (p1, 0, data2, 10);
105 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700106
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700107 // // // from code logic, we won't be fetching our own data
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700108 // a1.set(p1 + "/0/1", data1);
109 // a1.set(p1 + "/0/2", data2);
110 // BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700111 // // BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700112
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700113 // // another single source
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700114 // // string data3 = "You surf the Internet, I surf the real world";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700115 // string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
116 // string data5 = "Real men wear pink. Why? Because their wives make them";
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700117 // // s3.publish(p3, 0, data3, 10);
118 // // this_thread::sleep (posix_time::milliseconds (1000));
119
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700120 // // another single source, multiple data at once
121 // s2.publish(p2, 0, data4, 10);
122 // s2.publish(p2, 0, data5, 10);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700123 // this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700124
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700125 // // from code logic, we won't be fetching our own data
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700126 // // a3.set(p3 + "/0/0", data3);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700127 // a2.set(p2 + "/0/0", data4);
128 // a2.set(p2 + "/0/1", data5);
129 // BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700130 // // BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700131
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700132 // // not sure weither this is simultanous data generation from multiple sources
133 // string data6 = "Shakespeare says: 'Prose before hos.'";
134 // string data7 = "Pick good people, talent never wears out";
135 // s1.publish(p1, 0, data6, 10);
136 // s2.publish(p2, 0, data7, 10);
137 // usleep(10000);
Zhenkai Zhu00304002012-03-12 21:32:30 -0700138
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700139 // // from code logic, we won't be fetching our own data
140 // a1.set(p1 + "/0/3", data6);
141 // a2.set(p2 + "/0/2", data7);
142 // BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
143 // BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700144}
145
146