blob: 0492264bba1a4b584ff838b83a171a101f1c6601 [file] [log] [blame]
/* -*- 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 <boost/date_time/posix_time/posix_time.hpp>
#include "sync-logging.h"
#include "ccnx/sync-app-socket.h"
extern "C" {
#include <unistd.h>
}
using namespace Sync;
using namespace std;
using namespace boost;
INIT_LOGGER ("Test.AppSocket");
#define PRINT
//std::cout << "Line: " << __LINE__ << std::endl;
class TestSocketApp {
public:
map<string, string> data;
void set(string str1, string str2) {
// _LOG_FUNCTION (this << ", " << str1);
data.insert(make_pair(str1, str2));
// cout << str1 << ", " << str2 << endl;
}
void setNum(string str1, const char *buf, size_t len) {
int n = len / 4;
int *numbers = new int [n];
memcpy(numbers, buf, len);
for (int i = 0; i < n; i++) {
sum += numbers[i];
}
delete numbers;
}
int sum;
void fetchAll(const vector<MissingDataInfo> &v, SyncAppSocket *socket) {
int n = v.size();
PRINT
for (int i = 0; i < n; i++) {
for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
//PRINT
socket->fetchString(v[i].prefix, s, bind(&TestSocketApp::set, this, _1, _2));
}
}
}
void fetchNumbers(const vector<MissingDataInfo> &v, SyncAppSocket *socket) {
int n = v.size();
PRINT
std::cout << "In fetchNumbers. size of v is: " << n << std::endl;
for (int i = 0; i < n; i++) {
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;
for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
PRINT
socket->fetchRaw(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1, _2, _3));
}
}
}
void pass(const string &prefix) {
}
string toString(){
map<string, string>::iterator it = data.begin();
string str = "\n";
for (; it != data.end(); ++it){
str += "<";
str += it->first;
str += "|";
str += it->second;
str += ">";
str += "\n";
}
return str;
}
};
BOOST_AUTO_TEST_CASE (AppSocketTest)
{
INIT_LOGGERS ();
TestSocketApp a1, a2, a3;
string syncPrefix("/let/us/sync");
string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com");
_LOG_DEBUG ("s1");
SyncAppSocket s1 (syncPrefix, bind(&TestSocketApp::fetchAll, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
this_thread::sleep (posix_time::milliseconds (50));
_LOG_DEBUG ("s2");
SyncAppSocket s2 (syncPrefix, bind(&TestSocketApp::fetchAll, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
this_thread::sleep (posix_time::milliseconds (50));
SyncAppSocket s3 (syncPrefix, bind(&TestSocketApp::fetchAll, &a3, _1, _2), bind(&TestSocketApp::pass, &a3, _1));
this_thread::sleep (posix_time::milliseconds (50));
// single source
string data0 = "Very funny Scotty, now beam down my clothes";
_LOG_DEBUG ("s1 publish");
s1.publishString (p1, 0, data0, 10);
this_thread::sleep (posix_time::milliseconds (1000));
// from code logic, we won't be fetching our own data
a1.set(p1 + "/0/0", data0);
BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
// single source, multiple data at once
string data1 = "Yes, give me that ketchup";
string data2 = "Don't look conspicuous, it draws fire";
_LOG_DEBUG ("s1 publish");
s1.publishString (p1, 0, data1, 10);
_LOG_DEBUG ("s1 publish");
s1.publishString (p1, 0, data2, 10);
this_thread::sleep (posix_time::milliseconds (1000));
// from code logic, we won't be fetching our own data
a1.set(p1 + "/0/1", data1);
a1.set(p1 + "/0/2", data2);
BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
// another single source
string data3 = "You surf the Internet, I surf the real world";
string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
string data5 = "Real men wear pink. Why? Because their wives make them";
_LOG_DEBUG ("s3 publish");
s3.publishString(p3, 0, data3, 10);
this_thread::sleep (posix_time::milliseconds (200));
// another single source, multiple data at once
s2.publishString(p2, 0, data4, 10);
s2.publishString(p2, 0, data5, 10);
this_thread::sleep (posix_time::milliseconds (1000));
// from code logic, we won't be fetching our own data
a3.set(p3 + "/0/0", data3);
a2.set(p2 + "/0/0", data4);
a2.set(p2 + "/0/1", data5);
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
_LOG_DEBUG ("Simultaneous publishing");
string data6 = "Shakespeare says: 'Prose before hos.'";
string data7 = "Pick good people, talent never wears out";
s1.publishString(p1, 0, data6, 10);
// this_thread::sleep (posix_time::milliseconds (1000));
s2.publishString(p2, 0, data7, 10);
this_thread::sleep (posix_time::milliseconds (1500));
// from code logic, we won't be fetching our own data
a1.set(p1 + "/0/3", data6);
a2.set(p2 + "/0/2", data7);
// a1.set(p1 + "/0/1", data6);
// a2.set(p2 + "/0/0", data7);
BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
_LOG_DEBUG("Begin new test");
std::cout << "Begin new Test " << std::endl;
string syncRawPrefix = "/this/is/the/prefix";
a1.sum = 0;
a2.sum = 0;
SyncAppSocket s4 (syncRawPrefix, bind(&TestSocketApp::fetchNumbers, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
SyncAppSocket s5 (syncRawPrefix, bind(&TestSocketApp::fetchNumbers, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
int num[5] = {0, 1, 2, 3, 4};
string p4 = "/xiaonei.com";
string p5 = "/mitbbs.com";
s4.publishRaw(p4, 0,(const char *) num, sizeof(num), 10);
a1.setNum(p4, (const char *) num, sizeof (num));
this_thread::sleep (posix_time::milliseconds (1000));
BOOST_CHECK(a1.sum == a2.sum && a1.sum == 10);
int newNum[5] = {9, 7, 2, 1, 1};
s5.publishRaw(p5, 0,(const char *) newNum, sizeof(newNum), 10);
a2.setNum(p5, (const char *)newNum, sizeof (newNum));
this_thread::sleep (posix_time::milliseconds (1000));
BOOST_CHECK_EQUAL(a1.sum, a2.sum);
BOOST_CHECK_EQUAL(a1.sum, 30);
_LOG_DEBUG ("Finish");
}