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 | f46eac5 | 2013-07-26 11:27:39 -0700 | [diff] [blame^] | 30 | #include "sync-logging.h" |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | f46eac5 | 2013-07-26 11:27:39 -0700 | [diff] [blame^] | 32 | #include "ccnx/sync-app-socket.h" |
| 33 | |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 34 | extern "C" { |
| 35 | #include <unistd.h> |
| 36 | } |
| 37 | |
| 38 | using namespace Sync; |
| 39 | using namespace std; |
| 40 | using namespace boost; |
| 41 | |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 42 | INIT_LOGGER ("Test.AppSocket"); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 43 | |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame] | 44 | #define PRINT |
| 45 | //std::cout << "Line: " << __LINE__ << std::endl; |
| 46 | |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 47 | class TestSocketApp { |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 48 | public: |
| 49 | map<string, string> data; |
| 50 | void set(string str1, string str2) { |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 51 | // _LOG_FUNCTION (this << ", " << str1); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 52 | data.insert(make_pair(str1, str2)); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 53 | // cout << str1 << ", " << str2 << endl; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 54 | } |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 55 | |
| 56 | void setNum(string str1, const char *buf, size_t len) { |
| 57 | int n = len / 4; |
| 58 | int *numbers = new int [n]; |
| 59 | memcpy(numbers, buf, len); |
| 60 | for (int i = 0; i < n; i++) { |
| 61 | sum += numbers[i]; |
| 62 | } |
| 63 | delete numbers; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | int sum; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 68 | |
Zhenkai Zhu | 43ae5c7 | 2012-05-31 23:18:45 -0700 | [diff] [blame] | 69 | void fetchAll(const vector<MissingDataInfo> &v, SyncAppSocket *socket) { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 70 | int n = v.size(); |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame] | 71 | |
| 72 | PRINT |
| 73 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 74 | for (int i = 0; i < n; i++) { |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame] | 75 | for(SeqNo s = v[i].low; s <= v[i].high; ++s) { |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 76 | //PRINT |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 77 | socket->fetchString(v[i].prefix, s, bind(&TestSocketApp::set, this, _1, _2)); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 82 | void fetchNumbers(const vector<MissingDataInfo> &v, SyncAppSocket *socket) { |
| 83 | int n = v.size(); |
| 84 | |
| 85 | PRINT |
| 86 | |
| 87 | std::cout << "In fetchNumbers. size of v is: " << n << std::endl; |
| 88 | for (int i = 0; i < n; i++) { |
Zhenkai Zhu | 68f04d5 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 89 | std::cout << "In fetchNumbers. v[i].low is (" <<v[i].low.getSession() <<", " << v[i].low.getSeq() << ") v[i].high is ("<<v[i].high.getSession() <<", " <<v[i].high.getSeq()<<")" << std::endl; |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 90 | for(SeqNo s = v[i].low; s <= v[i].high; ++s) { |
| 91 | PRINT |
| 92 | socket->fetchRaw(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1, _2, _3)); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 97 | void pass(const string &prefix) { |
| 98 | } |
| 99 | |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 100 | string toString(){ |
| 101 | map<string, string>::iterator it = data.begin(); |
Zhenkai Zhu | 1a0fd5d | 2012-03-12 21:34:42 -0700 | [diff] [blame] | 102 | string str = "\n"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 103 | for (; it != data.end(); ++it){ |
| 104 | str += "<"; |
| 105 | str += it->first; |
| 106 | str += "|"; |
| 107 | str += it->second; |
| 108 | str += ">"; |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 109 | str += "\n"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 110 | } |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 111 | |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 112 | return str; |
| 113 | } |
| 114 | |
| 115 | }; |
| 116 | |
| 117 | BOOST_AUTO_TEST_CASE (AppSocketTest) |
| 118 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 119 | INIT_LOGGERS (); |
| 120 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 121 | TestSocketApp a1, a2, a3; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 123 | string syncPrefix("/let/us/sync"); |
| 124 | string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com"); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 126 | _LOG_DEBUG ("s1"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 127 | 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] | 128 | this_thread::sleep (posix_time::milliseconds (50)); |
| 129 | _LOG_DEBUG ("s2"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 130 | 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] | 131 | this_thread::sleep (posix_time::milliseconds (50)); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 132 | 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] | 133 | this_thread::sleep (posix_time::milliseconds (50)); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 134 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 135 | // single source |
| 136 | string data0 = "Very funny Scotty, now beam down my clothes"; |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 137 | _LOG_DEBUG ("s1 publish"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 138 | s1.publishString (p1, 0, data0, 10); |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame] | 139 | this_thread::sleep (posix_time::milliseconds (1000)); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 140 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 141 | // from code logic, we won't be fetching our own data |
| 142 | a1.set(p1 + "/0/0", data0); |
| 143 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 146 | // single source, multiple data at once |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 147 | string data1 = "Yes, give me that ketchup"; |
| 148 | string data2 = "Don't look conspicuous, it draws fire"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 150 | _LOG_DEBUG ("s1 publish"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 151 | s1.publishString (p1, 0, data1, 10); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 152 | _LOG_DEBUG ("s1 publish"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 153 | s1.publishString (p1, 0, data2, 10); |
Zhenkai Zhu | 699d740 | 2012-05-29 16:47:39 -0700 | [diff] [blame] | 154 | this_thread::sleep (posix_time::milliseconds (1000)); |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 155 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 156 | // from code logic, we won't be fetching our own data |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 157 | a1.set(p1 + "/0/1", data1); |
| 158 | a1.set(p1 + "/0/2", data2); |
| 159 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 160 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 162 | // another single source |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 163 | string data3 = "You surf the Internet, I surf the real world"; |
| 164 | string data4 = "I got a fortune cookie once that said 'You like Chinese food'"; |
| 165 | string data5 = "Real men wear pink. Why? Because their wives make them"; |
| 166 | _LOG_DEBUG ("s3 publish"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 167 | s3.publishString(p3, 0, data3, 10); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 168 | this_thread::sleep (posix_time::milliseconds (200)); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 169 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 170 | // another single source, multiple data at once |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 171 | s2.publishString(p2, 0, data4, 10); |
| 172 | s2.publishString(p2, 0, data5, 10); |
Zhenkai Zhu | 699d740 | 2012-05-29 16:47:39 -0700 | [diff] [blame] | 173 | this_thread::sleep (posix_time::milliseconds (1000)); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 175 | // from code logic, we won't be fetching our own data |
| 176 | a3.set(p3 + "/0/0", data3); |
| 177 | a2.set(p2 + "/0/0", data4); |
| 178 | a2.set(p2 + "/0/1", data5); |
| 179 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 180 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 181 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 182 | // not sure weither this is simultanous data generation from multiple sources |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 183 | _LOG_DEBUG ("Simultaneous publishing"); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 184 | string data6 = "Shakespeare says: 'Prose before hos.'"; |
| 185 | string data7 = "Pick good people, talent never wears out"; |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 186 | s1.publishString(p1, 0, data6, 10); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 187 | // this_thread::sleep (posix_time::milliseconds (1000)); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 188 | s2.publishString(p2, 0, data7, 10); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 189 | this_thread::sleep (posix_time::milliseconds (1500)); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 190 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 191 | // from code logic, we won't be fetching our own data |
| 192 | a1.set(p1 + "/0/3", data6); |
| 193 | a2.set(p2 + "/0/2", data7); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 194 | // a1.set(p1 + "/0/1", data6); |
| 195 | // a2.set(p2 + "/0/0", data7); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 196 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 197 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 198 | |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 199 | _LOG_DEBUG("Begin new test"); |
| 200 | std::cout << "Begin new Test " << std::endl; |
| 201 | string syncRawPrefix = "/this/is/the/prefix"; |
| 202 | a1.sum = 0; |
| 203 | a2.sum = 0; |
| 204 | SyncAppSocket s4 (syncRawPrefix, bind(&TestSocketApp::fetchNumbers, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1)); |
| 205 | SyncAppSocket s5 (syncRawPrefix, bind(&TestSocketApp::fetchNumbers, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1)); |
| 206 | |
| 207 | int num[5] = {0, 1, 2, 3, 4}; |
| 208 | |
| 209 | string p4 = "/xiaonei.com"; |
| 210 | string p5 = "/mitbbs.com"; |
| 211 | |
| 212 | s4.publishRaw(p4, 0,(const char *) num, sizeof(num), 10); |
| 213 | a1.setNum(p4, (const char *) num, sizeof (num)); |
| 214 | |
Zhenkai Zhu | 7c1fe78 | 2012-06-06 15:24:13 -0700 | [diff] [blame] | 215 | this_thread::sleep (posix_time::milliseconds (1000)); |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 216 | BOOST_CHECK(a1.sum == a2.sum && a1.sum == 10); |
| 217 | |
| 218 | int newNum[5] = {9, 7, 2, 1, 1}; |
| 219 | |
| 220 | s5.publishRaw(p5, 0,(const char *) newNum, sizeof(newNum), 10); |
| 221 | a2.setNum(p5, (const char *)newNum, sizeof (newNum)); |
| 222 | this_thread::sleep (posix_time::milliseconds (1000)); |
| 223 | BOOST_CHECK_EQUAL(a1.sum, a2.sum); |
| 224 | BOOST_CHECK_EQUAL(a1.sum, 30); |
| 225 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 226 | _LOG_DEBUG ("Finish"); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 227 | } |