blob: 2caa99b5b7aa849cc4f2066e6f1540ffd7841fdb [file] [log] [blame]
Zhenkai Zhue29616f2013-01-14 15:40:57 -08001#include "sync-core.h"
2
Alexander Afanasyevabe952a2013-01-17 17:06:32 -08003
Zhenkai Zhue29616f2013-01-14 15:40:57 -08004#include <boost/test/unit_test.hpp>
5#include <boost/filesystem.hpp>
6
7using namespace std;
8using namespace Ccnx;
9using namespace boost::filesystem;
10
11BOOST_AUTO_TEST_SUITE(SyncCoreTests)
12
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080013void callback(const SyncStateMsgPtr &msg)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080014{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080015 BOOST_CHECK(msg->state_size() > 0);
Zhenkai Zhu085aae72013-01-17 21:09:01 -080016 int size = msg->state_size();
17 int index = 0;
18 while (index < size)
19 {
20 SyncState state = msg->state(index);
21 BOOST_CHECK(state.has_old_seq());
22 BOOST_CHECK(state.old_seq() >= 0);
23 if (state.seq() != 0)
24 {
Alexander Afanasyevdac84922013-01-20 23:32:17 -080025 BOOST_CHECK(state.old_seq() != state.seq());
Zhenkai Zhu085aae72013-01-17 21:09:01 -080026 }
27 index++;
28 }
Zhenkai Zhue29616f2013-01-14 15:40:57 -080029}
30
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080031void checkRoots(const HashPtr &root1, const HashPtr &root2)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080032{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080033 BOOST_CHECK_EQUAL(*root1, *root2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080034}
35
36BOOST_AUTO_TEST_CASE(SyncCoreTest)
37{
38 string dir = "./SyncCoreTest";
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080039 // clean the test dir
40 path d(dir);
41 if (exists(d))
42 {
43 remove_all(d);
44 }
45
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080046 string dir1 = "./SyncCoreTest/1";
47 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080048 Name user1("/joker");
49 Name loc1("/gotham1");
50 Name user2("/darkknight");
51 Name loc2("/gotham2");
52 Name syncPrefix("/broadcast/darkknight");
53 CcnxWrapperPtr c1(new CcnxWrapper());
54 CcnxWrapperPtr c2(new CcnxWrapper());
Zhenkai Zhub330aed2013-01-17 13:29:37 -080055 SyncLogPtr log1(new SyncLog(dir1, user1.toString()));
56 SyncLogPtr log2(new SyncLog(dir2, user2.toString()));
57
58 SchedulerPtr scheduler(new Scheduler());
Zhenkai Zhue29616f2013-01-14 15:40:57 -080059
Zhenkai Zhue29616f2013-01-14 15:40:57 -080060
Zhenkai Zhub330aed2013-01-17 13:29:37 -080061 SyncCore *core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1, scheduler);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080062 usleep(10000);
Zhenkai Zhub330aed2013-01-17 13:29:37 -080063 SyncCore *core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2, scheduler);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080064 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080065 checkRoots(core1->root(), core2->root());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080066
67 cout << "\n\n\n\n\n\n----------\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080068 core1->updateLocalState(1);
69 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080070 checkRoots(core1->root(), core2->root());
71 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080072 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080073
74 core1->updateLocalState(5);
75 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080076 checkRoots(core1->root(), core2->root());
77 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080078 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080079
80 core2->updateLocalState(10);
81 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080082 checkRoots(core1->root(), core2->root());
83 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080084 BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080085
86 // simple simultaneous data generation
Zhenkai Zhu9f31e862013-01-15 23:56:31 -080087 cout << "\n\n\n\n\n\n----------Simultaneous\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080088 core1->updateLocalState(11);
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080089 usleep(100);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080090 core2->updateLocalState(15);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080091 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080092 checkRoots(core1->root(), core2->root());
93 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
94 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080095
Alexander Afanasyevdac84922013-01-20 23:32:17 -080096 BOOST_CHECK_EQUAL(log1->LookupLocator (user1), loc1);
97 BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2);
98 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
99 BOOST_CHECK_EQUAL(log2->LookupLocator (user2), loc2);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800100
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800101 // clean the test dir
102 if (exists(d))
103 {
104 remove_all(d);
105 }
106}
107
108BOOST_AUTO_TEST_SUITE_END()