| /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| /* |
| * Copyright (c) 2012 University of California, Los Angeles |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 as |
| * published by the Free Software Foundation; |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| * |
| * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn> |
| * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| */ |
| |
| #include <boost/test/unit_test.hpp> |
| #include <boost/test/output_test_stream.hpp> |
| using boost::test_tools::output_test_stream; |
| |
| #include <boost/make_shared.hpp> |
| |
| #include "../model/sync-app-socket.h" |
| extern "C" { |
| #include <unistd.h> |
| } |
| |
| using namespace Sync; |
| using namespace std; |
| using namespace boost; |
| |
| class TestStructApp { |
| public: |
| map<string, string> data; |
| void set(string str1, string str2) { |
| data.insert(make_pair(str1, str2)); |
| cout << "Got: "<< str1 <<" => "<< str2 <<endl; |
| } |
| |
| string toString(){ |
| map<string, string>::iterator it = data.begin(); |
| string str = ""; |
| for (; it != data.end(); ++it){ |
| str += "<"; |
| str += it->first; |
| str += "|"; |
| str += it->second; |
| str += ">"; |
| } |
| return str; |
| } |
| |
| }; |
| |
| BOOST_AUTO_TEST_CASE (AppSocketTest) |
| { |
| |
| TestStructApp a1, a2, a3; |
| boost::function<void(string, string)> f1 = bind(&TestStructApp::set, &a1, _1, |
| _2); |
| boost::function<void(string, string)> f2 = bind(&TestStructApp::set, &a2, _1, |
| _2); |
| boost::function<void(string, string)> f3 = bind(&TestStructApp::set, &a3, _1, |
| _2); |
| |
| string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com"); |
| |
| SyncAppSocket s1(p1, f1), s2(p2, f2), s3(p3, f3); |
| |
| // single source |
| s1.publish(p1, 0, "Very funny Scotty, now beam down my clothes", 10); |
| usleep(10000); |
| BOOST_CHECK(a1.toString() != ""); |
| BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
| |
| // single source, multiple data at once |
| s1.publish(p1, 0, "Yes, give me that ketchup", 10); |
| s1.publish(p1, 0, "Don't look conspicuous, it draws fire", 10); |
| usleep(10000); |
| BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
| |
| // another single source |
| s3.publish(p3, 0, "You surf the Internet, I surf the real world", 10); |
| usleep(10000); |
| // another single source, multiple data at once |
| s2.publish(p2, 0, "I got a fortune cookie once that said 'You like Chinese food'", 10); |
| s2.publish(p2, 0, "Real men wear pink. Why? Because their wives make them", 10); |
| usleep(10000); |
| BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
| |
| // not sure weither this is simultanous data generation from multiple sources |
| s1.publish(p1, 0, "Shakespeare says: 'Prose before hos.'", 10); |
| s2.publish(p2, 0, "Pick good people, talent never wears out", 10); |
| usleep(10000); |
| BOOST_CHECK_EQUAL(a1.toString(), a2.toString()); |
| BOOST_CHECK_EQUAL(a2.toString(), a3.toString()); |
| |
| } |
| |
| |