Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 1 | /* -*- 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> |
| 25 | using boost::test_tools::output_test_stream; |
| 26 | |
| 27 | #include <boost/make_shared.hpp> |
| 28 | |
| 29 | #include "../model/sync-app-socket.h" |
| 30 | extern "C" { |
| 31 | #include <unistd.h> |
| 32 | } |
| 33 | |
| 34 | using namespace Sync; |
| 35 | using namespace std; |
| 36 | using namespace boost; |
| 37 | |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 38 | class TestSocketApp { |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 39 | public: |
| 40 | map<string, string> data; |
| 41 | void set(string str1, string str2) { |
| 42 | data.insert(make_pair(str1, str2)); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 43 | // cout << str1 << ", " << str2 << endl; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | string toString(){ |
| 47 | map<string, string>::iterator it = data.begin(); |
Zhenkai Zhu | 1a0fd5d | 2012-03-12 21:34:42 -0700 | [diff] [blame] | 48 | string str = "\n"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 49 | for (; it != data.end(); ++it){ |
| 50 | str += "<"; |
| 51 | str += it->first; |
| 52 | str += "|"; |
| 53 | str += it->second; |
| 54 | str += ">"; |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 55 | str += "\n"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 56 | } |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 57 | |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 58 | return str; |
| 59 | } |
| 60 | |
| 61 | }; |
| 62 | |
| 63 | BOOST_AUTO_TEST_CASE (AppSocketTest) |
| 64 | { |
| 65 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 66 | TestSocketApp a1, a2, a3; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 68 | string syncPrefix("/let/us/sync"); |
| 69 | string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com"); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 71 | SyncAppSocket s1 (syncPrefix, bind(&TestSocketApp::set, &a1, _1, _2)); |
| 72 | this_thread::sleep (posix_time::milliseconds (500)); |
| 73 | SyncAppSocket s2 (syncPrefix, bind(&TestSocketApp::set, &a2, _1, _2)); |
| 74 | // SyncAppSocket s3 (syncPrefix, bind(&TestSocketApp::set, &a3, _1, _2)); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 76 | // single source |
| 77 | string data0 = "Very funny Scotty, now beam down my clothes"; |
| 78 | s1.publish (p1, 0, data0, 10); |
| 79 | this_thread::sleep (posix_time::milliseconds (1)); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 80 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 81 | // from code logic, we won't be fetching our own data |
| 82 | a1.set(p1 + "/0/0", data0); |
| 83 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 84 | // BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 86 | // // single source, multiple data at once |
| 87 | // string data1 = "Yes, give me that ketchup"; |
| 88 | // string data2 = "Don't look conspicuous, it draws fire"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 90 | // s1.publish (p1, 0, data1, 10); |
| 91 | // s1.publish(p1, 0, data2, 10); |
| 92 | // sleep (1); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 94 | // // from code logic, we won't be fetching our own data |
| 95 | // a1.set(p1 + "/0/1", data1); |
| 96 | // a1.set(p1 + "/0/2", data2); |
| 97 | // BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 98 | // BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 100 | // // another single source |
| 101 | // string data3 = "You surf the Internet, I surf the real world"; |
| 102 | // string data4 = "I got a fortune cookie once that said 'You like Chinese food'"; |
| 103 | // string data5 = "Real men wear pink. Why? Because their wives make them"; |
| 104 | // s3.publish(p3, 0, data3, 10); |
| 105 | // usleep(20000); |
| 106 | // // another single source, multiple data at once |
| 107 | // s2.publish(p2, 0, data4, 10); |
| 108 | // s2.publish(p2, 0, data5, 10); |
| 109 | // usleep(20000); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 111 | // // from code logic, we won't be fetching our own data |
| 112 | // a3.set(p3 + "/0/0", data3); |
| 113 | // a2.set(p2 + "/0/0", data4); |
| 114 | // a2.set(p2 + "/0/1", data5); |
| 115 | // BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 116 | // BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 118 | // // not sure weither this is simultanous data generation from multiple sources |
| 119 | // string data6 = "Shakespeare says: 'Prose before hos.'"; |
| 120 | // string data7 = "Pick good people, talent never wears out"; |
| 121 | // s1.publish(p1, 0, data6, 10); |
| 122 | // s2.publish(p2, 0, data7, 10); |
| 123 | // usleep(10000); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 125 | // // from code logic, we won't be fetching our own data |
| 126 | // a1.set(p1 + "/0/3", data6); |
| 127 | // a2.set(p2 + "/0/2", data7); |
| 128 | // BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 129 | // BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |