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