blob: 6adbfab1465c145616fc9c88c748a762f13df672 [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();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080030 result.locator = Name((const unsigned char *)strLoc.c_str(), strLoc.size());
Zhenkai Zhue573ae82013-01-15 13:15:52 -080031 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 Zhu6e7d4d22013-01-15 18:18:18 -080081
82 cout << "\n\n\n\n\n\n----------\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080083 core1->updateLocalState(1);
84 usleep(100000);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080085 BOOST_CHECK_EQUAL(result2.seq, 1);
86 BOOST_CHECK_EQUAL(result2.deviceName, user1);
87 BOOST_CHECK_EQUAL(result2.locator, loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080088
89 core1->updateLocalState(5);
90 usleep(100000);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080091 BOOST_CHECK_EQUAL(result2.seq, 5);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080092
93 core2->updateLocalState(10);
94 usleep(100000);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080095 BOOST_CHECK_EQUAL(result1.seq, 10);
96 BOOST_CHECK_EQUAL(result1.deviceName, user2);
97 BOOST_CHECK_EQUAL(result1.locator, loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080098
99 // simple simultaneous data generation
Zhenkai Zhu9f31e862013-01-15 23:56:31 -0800100 cout << "\n\n\n\n\n\n----------Simultaneous\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800101 core1->updateLocalState(11);
Zhenkai Zhu9f31e862013-01-15 23:56:31 -0800102 // change the value here 100, 2000, 3000, 5000, each with different error (for 2000 and 3000 run at least 3 times)
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800103 usleep(100);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800104 core2->updateLocalState(12);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800105 usleep(1000000);
106 BOOST_CHECK_EQUAL(result1.seq, 12);
107 BOOST_CHECK_EQUAL(result2.seq, 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800108
109 // clean the test dir
110 if (exists(d))
111 {
112 remove_all(d);
113 }
114}
115
116BOOST_AUTO_TEST_SUITE_END()