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 | |
| 38 | class TestStructApp { |
| 39 | public: |
| 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(); |
| 47 | string str = ""; |
| 48 | for (; it != data.end(); ++it){ |
| 49 | str += "<"; |
| 50 | str += it->first; |
| 51 | str += "|"; |
| 52 | str += it->second; |
| 53 | str += ">"; |
| 54 | } |
| 55 | return str; |
| 56 | } |
| 57 | |
| 58 | }; |
| 59 | |
| 60 | BOOST_AUTO_TEST_CASE (AppSocketTest) |
| 61 | { |
| 62 | |
| 63 | TestStructApp a1, a2, a3; |
| 64 | boost::function<void(string, string)> f1 = bind(&TestStructApp::set, &a1, _1, |
| 65 | _2); |
| 66 | boost::function<void(string, string)> f2 = bind(&TestStructApp::set, &a2, _1, |
| 67 | _2); |
| 68 | boost::function<void(string, string)> f3 = bind(&TestStructApp::set, &a3, _1, |
| 69 | _2); |
| 70 | |
| 71 | string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com"); |
| 72 | |
| 73 | SyncAppSocket s1(p1, f1), s2(p2, f2), s3(p3, f3); |
| 74 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 75 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
| 76 | |
| 77 | // single source |
| 78 | s1.publish(p1, 0, "Very funny Scotty, now beam down my clothes", 10); |
| 79 | usleep(10000); |
| 80 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 81 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
| 82 | |
| 83 | // single source, multiple data at once |
| 84 | s1.publish(p1, 0, "Yes, give me that ketchup", 10); |
| 85 | s1.publish(p1, 0, "Don't look conspicuous, it draws fire", 10); |
| 86 | usleep(10000); |
| 87 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 88 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
| 89 | |
| 90 | // another single source |
| 91 | s3.publish(p3, 0, "You surf the Internet, I surf the real world", 10); |
| 92 | usleep(10000); |
| 93 | // another single source, multiple data at once |
| 94 | s2.publish(p2, 0, "I got a fortune cookie once that said 'You like Chinese food'", 10); |
| 95 | s2.publish(p2, 0, "Real men wear pink. Why? Because their wives make them", 10); |
| 96 | usleep(10000); |
| 97 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 98 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
| 99 | |
| 100 | // not sure weither this is simultanous data generation from multiple sources |
| 101 | s1.publish(p1, 0, "Shakespeare says: 'Prose before hos.'", 10); |
| 102 | s2.publish(p2, 0, "Pick good people, talent never wears out", 10); |
| 103 | usleep(10000); |
| 104 | BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| 105 | BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
| 106 | } |
| 107 | |
| 108 | |