blob: cdec04d720452f04ca5945f645824517ed23a239 [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";
28 Name user1("/joker");
29 Name loc1("/gotham1");
30 Name user2("/darkknight");
31 Name loc2("/gotham2");
32 Name syncPrefix("/broadcast/darkknight");
33 CcnxWrapperPtr c1(new CcnxWrapper());
34 CcnxWrapperPtr c2(new CcnxWrapper());
35 SchedulerPtr scheduler(new Scheduler());
36 scheduler->start();
37
38 // clean the test dir
39 path d(dir);
40 if (exists(d))
41 {
42 remove_all(d);
43 }
44
45 SyncCore *core1 = new SyncCore(dir, user1, loc1, syncPrefix, bind(callback1, _1), c1, scheduler);
46 usleep(10000);
47 SyncCore *core2 = new SyncCore(dir, user2, loc2, syncPrefix, bind(callback2, _1), c2, scheduler);
48 usleep(10000);
49
50 SyncState state;
51
52 core1->updateLocalState(1);
53 usleep(100000);
54 BOOST_CHECK_EQUAL(msg2->state_size(), 1);
55 state = msg2->state(0);
56 BOOST_CHECK_EQUAL(state.seq(), 1);
57 BOOST_CHECK_EQUAL(user1, state.name());
58 BOOST_CHECK_EQUAL(loc1, state.locator());
59
60 core1->updateLocalState(5);
61 usleep(100000);
62 state = msg2->state(0);
63 BOOST_CHECK_EQUAL(state.seq(), 5);
64
65 core2->updateLocalState(10);
66 usleep(100000);
67 state = msg1->state(0);
68 BOOST_CHECK_EQUAL(state.seq(), 10);
69
70 // simple simultaneous data generation
71 core1->updateLocalState(11);
72 core2->updateLocalState(12);
73 usleep(100000);
74 state = msg1->state(0);
75 BOOST_CHECK_EQUAL(state.seq(), 12);
76 state = msg2->state(0);
77 BOOST_CHECK_EQUAL(state.seq(), 11);
78
79 // clean the test dir
80 if (exists(d))
81 {
82 remove_all(d);
83 }
84}
85
86BOOST_AUTO_TEST_SUITE_END()