blob: d65cafe3d1709c1f509dc2acf7334e83c12cb710 [file] [log] [blame]
Zhenkai Zhue29616f2013-01-14 15:40:57 -08001#include "sync-core.h"
2
3#include <boost/test/unit_test.hpp>
4#include <boost/filesystem.hpp>
5
6using namespace std;
7using namespace Ccnx;
8using namespace boost::filesystem;
9
10BOOST_AUTO_TEST_SUITE(SyncCoreTests)
11
Zhenkai Zhue573ae82013-01-15 13:15:52 -080012typedef struct
Zhenkai Zhue29616f2013-01-14 15:40:57 -080013{
Zhenkai Zhue573ae82013-01-15 13:15:52 -080014 Name deviceName;
15 Name locator;
16 int64_t seq;
17} Result;
18
19Result result1;
20Result result2;
21
22void setResult(const SyncStateMsgPtr &msg, Result &result)
23{
24 if (msg->state_size() > 0)
25 {
26 SyncState state = msg->state(0);
27 string strName = state.name();
28 result.deviceName = Name((const unsigned char *)strName.c_str(), strName.size());
29 string strLoc = state.locator();
30 result.locator = Name((const unsigned char *)strLoc.c_str(), strName.size());
31 result.seq = state.seq();
32 }
33 else
34 {
35 cout << "Msg state size: " << msg->state_size() << endl;
36 }
Zhenkai Zhue29616f2013-01-14 15:40:57 -080037}
38
Zhenkai Zhue573ae82013-01-15 13:15:52 -080039void callback1(const SyncStateMsgPtr &msg)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080040{
Zhenkai Zhue573ae82013-01-15 13:15:52 -080041 setResult(msg, result1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080042}
43
Zhenkai Zhue573ae82013-01-15 13:15:52 -080044void callback2(const SyncStateMsgPtr &msg)
45{
46 setResult(msg, result2);
47}
48
49
50
Zhenkai Zhue29616f2013-01-14 15:40:57 -080051BOOST_AUTO_TEST_CASE(SyncCoreTest)
52{
53 string dir = "./SyncCoreTest";
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080054 string dir1 = "./SyncCoreTest/1";
55 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080056 Name user1("/joker");
57 Name loc1("/gotham1");
58 Name user2("/darkknight");
59 Name loc2("/gotham2");
60 Name syncPrefix("/broadcast/darkknight");
61 CcnxWrapperPtr c1(new CcnxWrapper());
62 CcnxWrapperPtr c2(new CcnxWrapper());
Zhenkai Zhue29616f2013-01-14 15:40:57 -080063
64 // clean the test dir
65 path d(dir);
66 if (exists(d))
67 {
68 remove_all(d);
69 }
70
Zhenkai Zhue573ae82013-01-15 13:15:52 -080071 SyncCore *core1 = new SyncCore(dir1, user1, loc1, syncPrefix, bind(callback1, _1), c1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080072 usleep(10000);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080073 SyncCore *core2 = new SyncCore(dir2, user2, loc2, syncPrefix, bind(callback2, _1), c2);
74 usleep(1000000);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080075
76 SyncState state;
77
Zhenkai Zhue573ae82013-01-15 13:15:52 -080078 HashPtr root1 = core1->root();
79 HashPtr root2 = core2->root();
80 BOOST_CHECK_EQUAL(*root1, *root2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080081 core1->updateLocalState(1);
82 usleep(100000);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080083 BOOST_CHECK_EQUAL(result2.seq, 1);
84 BOOST_CHECK_EQUAL(result2.deviceName, user1);
85 BOOST_CHECK_EQUAL(result2.locator, loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080086
87 core1->updateLocalState(5);
88 usleep(100000);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080089 BOOST_CHECK_EQUAL(result2.seq, 5);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080090
91 core2->updateLocalState(10);
92 usleep(100000);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080093 BOOST_CHECK_EQUAL(result1.seq, 10);
94 BOOST_CHECK_EQUAL(result1.deviceName, user2);
95 BOOST_CHECK_EQUAL(result1.locator, loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080096
97 // simple simultaneous data generation
98 core1->updateLocalState(11);
99 core2->updateLocalState(12);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800100 usleep(1000000);
101 BOOST_CHECK_EQUAL(result1.seq, 12);
102 BOOST_CHECK_EQUAL(result2.seq, 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800103
104 // clean the test dir
105 if (exists(d))
106 {
107 remove_all(d);
108 }
109}
110
111BOOST_AUTO_TEST_SUITE_END()