blob: 3fd0d2600d8ec42e7a35dd4a9d4da1b72c761d06 [file] [log] [blame]
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012-2017 University of California, Los Angeles
*
* This file is part of ChronoSync, synchronization library for distributed realtime
* applications for NDN.
*
* ChronoSync is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* ChronoSync 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
* ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "socket.hpp"
#include "boost-test.hpp"
#include "../unit-test-time-fixture.hpp"
#include <ndn-cxx/util/dummy-client-face.hpp>
namespace chronosync {
namespace test {
using std::string;
using std::vector;
using std::map;
using ndn::util::DummyClientFace;
/**
* @brief Emulate an app that use the Socket class
*
* The app has two types of data set: one is simply string while the other is integer array.
* For each type of data set, the app has a specific fetching strategy.
*/
class SocketTestApp : noncopyable
{
public:
SocketTestApp(const Name& syncPrefix,
const Name& userPrefix,
DummyClientFace& face,
bool isNum)
: sum(0)
, socket(syncPrefix,
userPrefix,
face,
isNum ? bind(&SocketTestApp::fetchNumbers, this, _1) :
bind(&SocketTestApp::fetchAll, this, _1))
{
}
void
set(const Data& dataPacket)
{
// std::cerr << "set Data" << std::endl;
Name dataName(dataPacket.getName());
string str2(reinterpret_cast<const char*>(dataPacket.getContent().value()),
dataPacket.getContent().value_size());
data.insert(make_pair(dataName, str2));
}
void
set(Name name, const char* buf, int len)
{
string str2(buf, len);
data.insert(make_pair(name, str2));
}
void
setNum(const Data& dataPacket)
{
// std::cerr << "setNum Data" << std::endl;
size_t n = dataPacket.getContent().value_size() / 4;
const uint32_t* numbers = reinterpret_cast<const uint32_t*>(dataPacket.getContent().value());
for (size_t i = 0; i < n; i++) {
sum += numbers[i];
}
}
void
setNum(Name name, const uint8_t* buf, int len)
{
BOOST_ASSERT(len >= 4);
int n = len / 4;
const uint32_t* numbers = reinterpret_cast<const uint32_t*>(buf);
for (int i = 0; i < n; i++) {
sum += numbers[i];
}
}
void
fetchAll(const vector<MissingDataInfo>& v)
{
// std::cerr << "fetchAll" << std::endl;
for (size_t i = 0; i < v.size(); i++) {
for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
socket.fetchData(v[i].session, s, [this] (const Data& dataPacket) {
this->set(dataPacket);
});
}
}
}
void
fetchNumbers(const vector<MissingDataInfo>& v)
{
// std::cerr << "fetchNumbers" << std::endl;
for (size_t i = 0; i < v.size(); i++) {
for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
socket.fetchData(v[i].session, s, [this] (const Data& dataPacket) {
this->setNum(dataPacket);
});
}
}
}
string
toString()
{
string str = "\n";
for (map<Name, string>::iterator it = data.begin(); it != data.end(); ++it) {
str += "<";
str += it->first.toUri();
str += "|";
str += it->second;
str += ">";
str += "\n";
}
return str;
}
public:
map<ndn::Name, string> data;
uint32_t sum;
Socket socket;
};
class SocketFixture : public ndn::tests::UnitTestTimeFixture
{
public:
SocketFixture()
: syncPrefix("/ndn/broadcast/sync")
{
syncPrefix.appendVersion();
userPrefix[0] = Name("/user0");
userPrefix[1] = Name("/user1");
userPrefix[2] = Name("/user2");
faces[0].reset(new DummyClientFace(io, {true, true}));
faces[1].reset(new DummyClientFace(io, {true, true}));
faces[2].reset(new DummyClientFace(io, {true, true}));
for (int i = 0; i < 3; i++) {
readInterestOffset[i] = 0;
readDataOffset[i] = 0;
}
}
void
passPacket()
{
for (int i = 0; i < 3; i++)
checkFace(i);
}
void
checkFace(int sender)
{
while (faces[sender]->sentInterests.size() > readInterestOffset[sender]) {
for (int i = 0; i < 3; i++) {
if (sender != i)
faces[i]->receive(faces[sender]->sentInterests[readInterestOffset[sender]]);
}
readInterestOffset[sender]++;
}
while (faces[sender]->sentData.size() > readDataOffset[sender]) {
for (int i = 0; i < 3; i++) {
if (sender != i)
faces[i]->receive(faces[sender]->sentData[readDataOffset[sender]]);
}
readDataOffset[sender]++;
}
}
void
createSocket(size_t idx, bool isNum)
{
app[idx] = make_shared<SocketTestApp>(syncPrefix, userPrefix[idx], ref(*faces[idx]), isNum);
sessionName[idx] = app[idx]->socket.getLogic().getSessionName();
}
void
publishAppData(size_t idx, const string& data)
{
app[idx]->socket.publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(),
ndn::time::milliseconds(1000));
}
void
publishAppData(size_t idx, const string& data, SeqNo seqNo)
{
app[idx]->socket.publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(),
ndn::time::milliseconds(1000), seqNo);
}
void
setAppData(size_t idx, SeqNo seqNo, const string& data)
{
Name dataName = sessionName[idx];
dataName.appendNumber(seqNo);
app[idx]->set(dataName, data.c_str(), data.size());
}
void
publishAppNum(size_t idx, const uint8_t* buf, size_t size)
{
app[idx]->socket.publishData(buf, size, ndn::time::milliseconds(1000));
}
void
setAppNum(size_t idx, SeqNo seqNo, const uint8_t* buf, size_t size)
{
Name dataName = sessionName[idx];
dataName.appendNumber(seqNo);
app[idx]->setNum(dataName, buf, size);
}
public:
Name syncPrefix;
Name userPrefix[3];
Name sessionName[3];
std::unique_ptr<DummyClientFace> faces[3];
shared_ptr<SocketTestApp> app[3];
size_t readInterestOffset[3];
size_t readDataOffset[3];
};
BOOST_FIXTURE_TEST_SUITE(SocketTests, SocketFixture)
BOOST_AUTO_TEST_CASE(BasicData)
{
createSocket(0, false);
advanceClocks(ndn::time::milliseconds(10), 5);
createSocket(1, false);
advanceClocks(ndn::time::milliseconds(10), 5);
createSocket(2, false);
advanceClocks(ndn::time::milliseconds(10), 5);
string data0 = "Very funny Scotty, now beam down my clothes";
publishAppData(0, data0);
for (int i = 0; i < 50; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
setAppData(0, 1, data0);
advanceClocks(ndn::time::milliseconds(10), 1);
BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
string data1 = "Yes, give me that ketchup";
string data2 = "Don't look conspicuous, it draws fire";
publishAppData(0, data1);
advanceClocks(ndn::time::milliseconds(10), 1);
publishAppData(0, data2);
for (int i = 0; i < 50; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
setAppData(0, 2, data1);
advanceClocks(ndn::time::milliseconds(10), 1);
setAppData(0, 3, data2);
advanceClocks(ndn::time::milliseconds(10), 1);
BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
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";
publishAppData(2, data3);
advanceClocks(ndn::time::milliseconds(10), 2);
publishAppData(1, data4);
advanceClocks(ndn::time::milliseconds(10), 1);
publishAppData(1, data5);
for (int i = 0; i < 100; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
setAppData(2, 1, data3);
advanceClocks(ndn::time::milliseconds(10), 1);
setAppData(1, 1, data4);
advanceClocks(ndn::time::milliseconds(10), 1);
setAppData(1, 2, data5);
advanceClocks(ndn::time::milliseconds(10), 7);
BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
string data6 = "Shakespeare says: 'Prose before hos.'";
string data7 = "Pick good people, talent never wears out";
publishAppData(0, data6);
publishAppData(1, data7);
for (int i = 0; i < 100; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
setAppData(0, 4, data6);
setAppData(1, 3, data7);
BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
}
BOOST_AUTO_TEST_CASE(BasicNumber)
{
createSocket(0, true);
advanceClocks(ndn::time::milliseconds(10), 5);
createSocket(1, true);
advanceClocks(ndn::time::milliseconds(10), 5);
uint32_t num1[5] = {0, 1, 2, 3, 4};
uint8_t* buf1 = reinterpret_cast<uint8_t*>(num1);
size_t size1 = sizeof(num1);
publishAppNum(0, buf1, size1);
advanceClocks(ndn::time::milliseconds(10), 5);
setAppNum(0, 0, buf1, size1);
for (int i = 0; i < 100; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
BOOST_CHECK_EQUAL(app[0]->sum, app[1]->sum);
BOOST_CHECK_EQUAL(app[1]->sum, 10);
uint32_t num2[5] = {9, 7, 2, 1, 1};
uint8_t* buf2 = reinterpret_cast<uint8_t*>(num2);
size_t size2 = sizeof(num2);
publishAppNum(1, buf2, size2);
setAppNum(1, 0, buf2, size2);
for (int i = 0; i < 50; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
BOOST_CHECK_EQUAL(app[0]->sum, app[1]->sum);
BOOST_CHECK_EQUAL(app[1]->sum, 30);
}
BOOST_AUTO_TEST_CASE(BasicDataWithAppSeq)
{
createSocket(0, false);
advanceClocks(ndn::time::milliseconds(10), 5);
createSocket(1, false);
advanceClocks(ndn::time::milliseconds(10), 5);
createSocket(2, false);
advanceClocks(ndn::time::milliseconds(10), 5);
string data0 = "Very funny Scotty, now beam down my clothes";
publishAppData(0, data0, 100);
for (int i = 0; i < 50; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
setAppData(0, 100, data0);
advanceClocks(ndn::time::milliseconds(10), 1);
BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
string data1 = "Yes, give me that ketchup";
string data2 = "Don't look conspicuous, it draws fire";
publishAppData(0, data1, 200);
advanceClocks(ndn::time::milliseconds(10), 1);
publishAppData(0, data2, 300);
for (int i = 0; i < 50; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
setAppData(0, 200, data1);
advanceClocks(ndn::time::milliseconds(10), 1);
setAppData(0, 300, data2);
advanceClocks(ndn::time::milliseconds(10), 1);
BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
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";
publishAppData(2, data3, 100);
advanceClocks(ndn::time::milliseconds(10), 2);
publishAppData(1, data4, 100);
advanceClocks(ndn::time::milliseconds(10), 1);
publishAppData(1, data5, 200);
for (int i = 0; i < 100; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
setAppData(2, 100, data3);
advanceClocks(ndn::time::milliseconds(10), 1);
setAppData(1, 100, data4);
advanceClocks(ndn::time::milliseconds(10), 1);
setAppData(1, 200, data5);
advanceClocks(ndn::time::milliseconds(10), 7);
BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
string data6 = "Shakespeare says: 'Prose before hos.'";
string data7 = "Pick good people, talent never wears out";
publishAppData(0, data6, 500);
publishAppData(1, data7, 300);
for (int i = 0; i < 100; i++) {
advanceClocks(ndn::time::milliseconds(2), 10);
passPacket();
}
setAppData(0, 500, data6);
setAppData(1, 300, data7);
BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace test
} // namespace chronosync