blob: 016ef54b38d3330a8eef559f930e2c7a1a1342b8 [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>
28
29#include "../model/sync-app-socket.h"
30extern "C" {
31#include <unistd.h>
32}
33
34using namespace Sync;
35using namespace std;
36using namespace boost;
37
Zhenkai Zhu00304002012-03-12 21:32:30 -070038class TestSocketApp {
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070039public:
40 map<string, string> data;
41 void set(string str1, string str2) {
42 data.insert(make_pair(str1, str2));
43 }
44
45 string toString(){
46 map<string, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -070047 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070048 for (; it != data.end(); ++it){
49 str += "<";
50 str += it->first;
51 str += "|";
52 str += it->second;
53 str += ">";
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -070054 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070055 }
56 return str;
57 }
58
59};
60
61BOOST_AUTO_TEST_CASE (AppSocketTest)
62{
63
Zhenkai Zhu00304002012-03-12 21:32:30 -070064 TestSocketApp a1, a2, a3;
65 boost::function<void(string, string)> f1 = bind(&TestSocketApp::set, &a1, _1,
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070066 _2);
Zhenkai Zhu00304002012-03-12 21:32:30 -070067 boost::function<void(string, string)> f2 = bind(&TestSocketApp::set, &a2, _1,
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070068 _2);
Zhenkai Zhu00304002012-03-12 21:32:30 -070069 boost::function<void(string, string)> f3 = bind(&TestSocketApp::set, &a3, _1,
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070070 _2);
71
Zhenkai Zhu0f334152012-03-12 21:42:29 -070072 string syncPrefix("/let/us/sync");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070073 string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com");
74
Zhenkai Zhu0f334152012-03-12 21:42:29 -070075 SyncAppSocket s1(syncPrefix, f1), s2(syncPrefix, f2), s3(syncPrefix, f3);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070076
Zhenkai Zhu88916c22012-03-12 22:10:41 -070077 s1.start();
78 s2.start();
79 s3.start();
Zhenkai Zhu00304002012-03-12 21:32:30 -070080
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070081 // single source
Zhenkai Zhu00304002012-03-12 21:32:30 -070082 string data0 = "Very funny Scotty, now beam down my clothes";
83 s1.publish(p1, 0, data0 , 10);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070084 usleep(10000);
Zhenkai Zhu00304002012-03-12 21:32:30 -070085
86 // from code logic, we won't be fetching our own data
87 a1.set(p1 + "/0/0", data0);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070088 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
89 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
90
91 // single source, multiple data at once
Zhenkai Zhu00304002012-03-12 21:32:30 -070092 string data1 = "Yes, give me that ketchup";
93 string data2 = "Don't look conspicuous, it draws fire";
94 s1.publish(p1, 0, data1, 10);
95 s1.publish(p1, 0, data2, 10);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070096 usleep(10000);
Zhenkai Zhu00304002012-03-12 21:32:30 -070097
98 // from code logic, we won't be fetching our own data
99 a1.set(p1 + "/0/1", data1);
100 a1.set(p1 + "/0/2", data2);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700101 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
102 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
103
104 // another single source
Zhenkai Zhu00304002012-03-12 21:32:30 -0700105 string data3 = "You surf the Internet, I surf the real world";
106 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
107 string data5 = "Real men wear pink. Why? Because their wives make them";
108 s3.publish(p3, 0, data3, 10);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700109 usleep(10000);
110 // another single source, multiple data at once
Zhenkai Zhu00304002012-03-12 21:32:30 -0700111 s2.publish(p2, 0, data4, 10);
112 s2.publish(p2, 0, data5, 10);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700113 usleep(10000);
Zhenkai Zhu00304002012-03-12 21:32:30 -0700114
115 // from code logic, we won't be fetching our own data
116 a3.set(p3 + "/0/0", data3);
117 a2.set(p2 + "/0/0", data4);
118 a2.set(p2 + "/0/1", data5);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700119 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
120 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
121
122 // not sure weither this is simultanous data generation from multiple sources
Zhenkai Zhu00304002012-03-12 21:32:30 -0700123 string data6 = "Shakespeare says: 'Prose before hos.'";
124 string data7 = "Pick good people, talent never wears out";
125 s1.publish(p1, 0, data6, 10);
126 s2.publish(p2, 0, data7, 10);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700127 usleep(10000);
Zhenkai Zhu00304002012-03-12 21:32:30 -0700128
129 // from code logic, we won't be fetching our own data
130 a1.set(p1 + "/0/3", data6);
131 a2.set(p2 + "/0/2", data7);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700132 BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
133 BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu47afc722012-03-12 19:35:45 -0700134
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700135}
136
137