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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 30 | #include "sync-log.h" |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 32 | #include "sync-app-socket.h" |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 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 | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame^] | 43 | #define PRINT |
| 44 | //std::cout << "Line: " << __LINE__ << std::endl; |
| 45 | |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 46 | class TestSocketApp { |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 47 | public: |
| 48 | map<string, string> data; |
| 49 | void set(string str1, string str2) { |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 50 | // _LOG_FUNCTION (this << ", " << str1); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 51 | data.insert(make_pair(str1, str2)); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 52 | // cout << str1 << ", " << str2 << endl; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Zhenkai Zhu | 43ae5c7 | 2012-05-31 23:18:45 -0700 | [diff] [blame] | 55 | void fetchAll(const vector<MissingDataInfo> &v, SyncAppSocket *socket) { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 56 | int n = v.size(); |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame^] | 57 | |
| 58 | PRINT |
| 59 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 60 | for (int i = 0; i < n; i++) { |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame^] | 61 | for(SeqNo s = v[i].low; s <= v[i].high; ++s) { |
| 62 | PRINT |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 63 | socket->fetchString(v[i].prefix, s, bind(&TestSocketApp::set, this, _1, _2)); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void pass(const string &prefix) { |
| 69 | } |
| 70 | |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 71 | string toString(){ |
| 72 | map<string, string>::iterator it = data.begin(); |
Zhenkai Zhu | 1a0fd5d | 2012-03-12 21:34:42 -0700 | [diff] [blame] | 73 | string str = "\n"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 74 | for (; it != data.end(); ++it){ |
| 75 | str += "<"; |
| 76 | str += it->first; |
| 77 | str += "|"; |
| 78 | str += it->second; |
| 79 | str += ">"; |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 80 | str += "\n"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 81 | } |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 82 | |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 83 | return str; |
| 84 | } |
| 85 | |
| 86 | }; |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE (AppSocketTest) |
| 89 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 90 | INIT_LOGGERS (); |
| 91 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 92 | TestSocketApp a1, a2, a3; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 94 | string syncPrefix("/let/us/sync"); |
| 95 | string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com"); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 97 | _LOG_DEBUG ("s1"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 98 | SyncAppSocket s1 (syncPrefix, bind(&TestSocketApp::fetchAll, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1)); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 99 | this_thread::sleep (posix_time::milliseconds (50)); |
| 100 | _LOG_DEBUG ("s2"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 101 | SyncAppSocket s2 (syncPrefix, bind(&TestSocketApp::fetchAll, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1)); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 102 | this_thread::sleep (posix_time::milliseconds (50)); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 103 | SyncAppSocket s3 (syncPrefix, bind(&TestSocketApp::fetchAll, &a3, _1, _2), bind(&TestSocketApp::pass, &a3, _1)); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 104 | this_thread::sleep (posix_time::milliseconds (50)); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 106 | // single source |
| 107 | string data0 = "Very funny Scotty, now beam down my clothes"; |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 108 | _LOG_DEBUG ("s1 publish"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 109 | s1.publishString (p1, 0, data0, 10); |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame^] | 110 | this_thread::sleep (posix_time::milliseconds (1000)); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 111 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 112 | // from code logic, we won't be fetching our own data |
| 113 | a1.set(p1 + "/0/0", data0); |
| 114 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 117 | // single source, multiple data at once |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 118 | string data1 = "Yes, give me that ketchup"; |
| 119 | string data2 = "Don't look conspicuous, it draws fire"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 120 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 121 | _LOG_DEBUG ("s1 publish"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 122 | s1.publishString (p1, 0, data1, 10); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 123 | _LOG_DEBUG ("s1 publish"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 124 | s1.publishString (p1, 0, data2, 10); |
Zhenkai Zhu | 699d740 | 2012-05-29 16:47:39 -0700 | [diff] [blame] | 125 | this_thread::sleep (posix_time::milliseconds (1000)); |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 127 | // from code logic, we won't be fetching our own data |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 128 | a1.set(p1 + "/0/1", data1); |
| 129 | a1.set(p1 + "/0/2", data2); |
| 130 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 131 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 133 | // another single source |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 134 | string data3 = "You surf the Internet, I surf the real world"; |
| 135 | string data4 = "I got a fortune cookie once that said 'You like Chinese food'"; |
| 136 | string data5 = "Real men wear pink. Why? Because their wives make them"; |
| 137 | _LOG_DEBUG ("s3 publish"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 138 | s3.publishString(p3, 0, data3, 10); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 139 | this_thread::sleep (posix_time::milliseconds (200)); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 140 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 141 | // another single source, multiple data at once |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 142 | s2.publishString(p2, 0, data4, 10); |
| 143 | s2.publishString(p2, 0, data5, 10); |
Zhenkai Zhu | 699d740 | 2012-05-29 16:47:39 -0700 | [diff] [blame] | 144 | this_thread::sleep (posix_time::milliseconds (1000)); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 146 | // from code logic, we won't be fetching our own data |
| 147 | a3.set(p3 + "/0/0", data3); |
| 148 | a2.set(p2 + "/0/0", data4); |
| 149 | a2.set(p2 + "/0/1", data5); |
| 150 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 151 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 152 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 153 | // not sure weither this is simultanous data generation from multiple sources |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 154 | _LOG_DEBUG ("Simultaneous publishing"); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 155 | string data6 = "Shakespeare says: 'Prose before hos.'"; |
| 156 | string data7 = "Pick good people, talent never wears out"; |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 157 | s1.publishString(p1, 0, data6, 10); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 158 | // this_thread::sleep (posix_time::milliseconds (1000)); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 159 | s2.publishString(p2, 0, data7, 10); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 160 | this_thread::sleep (posix_time::milliseconds (1500)); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 162 | // from code logic, we won't be fetching our own data |
| 163 | a1.set(p1 + "/0/3", data6); |
| 164 | a2.set(p2 + "/0/2", data7); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 165 | // a1.set(p1 + "/0/1", data6); |
| 166 | // a2.set(p2 + "/0/0", data7); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 168 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 169 | |
| 170 | _LOG_DEBUG ("Finish"); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 171 | } |