blob: 68cc69b80266bf8c5b735709d8f8376ef1c39353 [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
12SyncStateMsgPtr msg1;
13SyncStateMsgPtr msg2;
14
15void callback1(const SyncStateMsgPtr &ptr)
16{
17 msg1 = ptr;
18}
19
20void callback2(const SyncStateMsgPtr &ptr)
21{
22 msg2 = ptr;
23}
24
25BOOST_AUTO_TEST_CASE(SyncCoreTest)
26{
27 string dir = "./SyncCoreTest";
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080028 string dir1 = "./SyncCoreTest/1";
29 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080030 Name user1("/joker");
31 Name loc1("/gotham1");
32 Name user2("/darkknight");
33 Name loc2("/gotham2");
34 Name syncPrefix("/broadcast/darkknight");
35 CcnxWrapperPtr c1(new CcnxWrapper());
36 CcnxWrapperPtr c2(new CcnxWrapper());
37 SchedulerPtr scheduler(new Scheduler());
38 scheduler->start();
39
40 // clean the test dir
41 path d(dir);
42 if (exists(d))
43 {
44 remove_all(d);
45 }
46
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080047 SyncCore *core1 = new SyncCore(dir1, user1, loc1, syncPrefix, bind(callback1, _1), c1, scheduler);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080048 usleep(10000);
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080049 SyncCore *core2 = new SyncCore(dir2, user2, loc2, syncPrefix, bind(callback2, _1), c2, scheduler);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080050 usleep(10000);
51
52 SyncState state;
53
54 core1->updateLocalState(1);
55 usleep(100000);
56 BOOST_CHECK_EQUAL(msg2->state_size(), 1);
57 state = msg2->state(0);
58 BOOST_CHECK_EQUAL(state.seq(), 1);
59 BOOST_CHECK_EQUAL(user1, state.name());
60 BOOST_CHECK_EQUAL(loc1, state.locator());
61
62 core1->updateLocalState(5);
63 usleep(100000);
64 state = msg2->state(0);
65 BOOST_CHECK_EQUAL(state.seq(), 5);
66
67 core2->updateLocalState(10);
68 usleep(100000);
69 state = msg1->state(0);
70 BOOST_CHECK_EQUAL(state.seq(), 10);
71
72 // simple simultaneous data generation
73 core1->updateLocalState(11);
74 core2->updateLocalState(12);
75 usleep(100000);
76 state = msg1->state(0);
77 BOOST_CHECK_EQUAL(state.seq(), 12);
78 state = msg2->state(0);
79 BOOST_CHECK_EQUAL(state.seq(), 11);
80
81 // clean the test dir
82 if (exists(d))
83 {
84 remove_all(d);
85 }
86}
87
88BOOST_AUTO_TEST_SUITE_END()