blob: d65cafe3d1709c1f509dc2acf7334e83c12cb710 [file] [log] [blame]
#include "sync-core.h"
#include <boost/test/unit_test.hpp>
#include <boost/filesystem.hpp>
using namespace std;
using namespace Ccnx;
using namespace boost::filesystem;
BOOST_AUTO_TEST_SUITE(SyncCoreTests)
typedef struct
{
Name deviceName;
Name locator;
int64_t seq;
} Result;
Result result1;
Result result2;
void setResult(const SyncStateMsgPtr &msg, Result &result)
{
if (msg->state_size() > 0)
{
SyncState state = msg->state(0);
string strName = state.name();
result.deviceName = Name((const unsigned char *)strName.c_str(), strName.size());
string strLoc = state.locator();
result.locator = Name((const unsigned char *)strLoc.c_str(), strName.size());
result.seq = state.seq();
}
else
{
cout << "Msg state size: " << msg->state_size() << endl;
}
}
void callback1(const SyncStateMsgPtr &msg)
{
setResult(msg, result1);
}
void callback2(const SyncStateMsgPtr &msg)
{
setResult(msg, result2);
}
BOOST_AUTO_TEST_CASE(SyncCoreTest)
{
string dir = "./SyncCoreTest";
string dir1 = "./SyncCoreTest/1";
string dir2 = "./SyncCoreTest/2";
Name user1("/joker");
Name loc1("/gotham1");
Name user2("/darkknight");
Name loc2("/gotham2");
Name syncPrefix("/broadcast/darkknight");
CcnxWrapperPtr c1(new CcnxWrapper());
CcnxWrapperPtr c2(new CcnxWrapper());
// clean the test dir
path d(dir);
if (exists(d))
{
remove_all(d);
}
SyncCore *core1 = new SyncCore(dir1, user1, loc1, syncPrefix, bind(callback1, _1), c1);
usleep(10000);
SyncCore *core2 = new SyncCore(dir2, user2, loc2, syncPrefix, bind(callback2, _1), c2);
usleep(1000000);
SyncState state;
HashPtr root1 = core1->root();
HashPtr root2 = core2->root();
BOOST_CHECK_EQUAL(*root1, *root2);
core1->updateLocalState(1);
usleep(100000);
BOOST_CHECK_EQUAL(result2.seq, 1);
BOOST_CHECK_EQUAL(result2.deviceName, user1);
BOOST_CHECK_EQUAL(result2.locator, loc1);
core1->updateLocalState(5);
usleep(100000);
BOOST_CHECK_EQUAL(result2.seq, 5);
core2->updateLocalState(10);
usleep(100000);
BOOST_CHECK_EQUAL(result1.seq, 10);
BOOST_CHECK_EQUAL(result1.deviceName, user2);
BOOST_CHECK_EQUAL(result1.locator, loc2);
// simple simultaneous data generation
core1->updateLocalState(11);
core2->updateLocalState(12);
usleep(1000000);
BOOST_CHECK_EQUAL(result1.seq, 12);
BOOST_CHECK_EQUAL(result2.seq, 11);
// clean the test dir
if (exists(d))
{
remove_all(d);
}
}
BOOST_AUTO_TEST_SUITE_END()