blob: b8316c1b52f5265b063723d6c47a5d2713452564 [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>
Alexander Afanasyevf278db32013-01-21 14:41:01 -08006#include <boost/make_shared.hpp>
Zhenkai Zhue29616f2013-01-14 15:40:57 -08007
8using namespace std;
9using namespace Ccnx;
Alexander Afanasyevf278db32013-01-21 14:41:01 -080010using namespace boost;
Zhenkai Zhue29616f2013-01-14 15:40:57 -080011using namespace boost::filesystem;
12
13BOOST_AUTO_TEST_SUITE(SyncCoreTests)
14
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080015void callback(const SyncStateMsgPtr &msg)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080016{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080017 BOOST_CHECK(msg->state_size() > 0);
Zhenkai Zhu085aae72013-01-17 21:09:01 -080018 int size = msg->state_size();
19 int index = 0;
20 while (index < size)
21 {
22 SyncState state = msg->state(index);
23 BOOST_CHECK(state.has_old_seq());
24 BOOST_CHECK(state.old_seq() >= 0);
25 if (state.seq() != 0)
26 {
Alexander Afanasyevdac84922013-01-20 23:32:17 -080027 BOOST_CHECK(state.old_seq() != state.seq());
Zhenkai Zhu085aae72013-01-17 21:09:01 -080028 }
29 index++;
30 }
Zhenkai Zhue29616f2013-01-14 15:40:57 -080031}
32
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080033void checkRoots(const HashPtr &root1, const HashPtr &root2)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080034{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080035 BOOST_CHECK_EQUAL(*root1, *root2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080036}
37
38BOOST_AUTO_TEST_CASE(SyncCoreTest)
39{
40 string dir = "./SyncCoreTest";
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080041 // clean the test dir
42 path d(dir);
43 if (exists(d))
44 {
45 remove_all(d);
46 }
47
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080048 string dir1 = "./SyncCoreTest/1";
49 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080050 Name user1("/joker");
51 Name loc1("/gotham1");
52 Name user2("/darkknight");
53 Name loc2("/gotham2");
54 Name syncPrefix("/broadcast/darkknight");
55 CcnxWrapperPtr c1(new CcnxWrapper());
56 CcnxWrapperPtr c2(new CcnxWrapper());
Zhenkai Zhub330aed2013-01-17 13:29:37 -080057 SyncLogPtr log1(new SyncLog(dir1, user1.toString()));
58 SyncLogPtr log2(new SyncLog(dir2, user2.toString()));
59
Alexander Afanasyevf278db32013-01-21 14:41:01 -080060 // should not have used the same scheduler...
61 SchedulerPtr scheduler1 = make_shared<Scheduler> ();
62 SchedulerPtr scheduler2 = make_shared<Scheduler> ();
Zhenkai Zhue29616f2013-01-14 15:40:57 -080063
Alexander Afanasyevf278db32013-01-21 14:41:01 -080064 SyncCore *core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1, scheduler1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080065 usleep(10000);
Alexander Afanasyevf278db32013-01-21 14:41:01 -080066 SyncCore *core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2, scheduler2);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080067 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080068 checkRoots(core1->root(), core2->root());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080069
Alexander Afanasyevf278db32013-01-21 14:41:01 -080070 // cout << "\n\n\n\n\n\n----------\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080071 core1->updateLocalState(1);
72 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080073 checkRoots(core1->root(), core2->root());
74 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080075 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080076
77 core1->updateLocalState(5);
78 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080079 checkRoots(core1->root(), core2->root());
80 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080081 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080082
83 core2->updateLocalState(10);
84 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080085 checkRoots(core1->root(), core2->root());
86 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080087 BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080088
89 // simple simultaneous data generation
Alexander Afanasyevf278db32013-01-21 14:41:01 -080090 // cout << "\n\n\n\n\n\n----------Simultaneous\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080091 core1->updateLocalState(11);
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080092 usleep(100);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080093 core2->updateLocalState(15);
Alexander Afanasyevf278db32013-01-21 14:41:01 -080094 usleep(2000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080095 checkRoots(core1->root(), core2->root());
96 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
97 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080098
Alexander Afanasyevdac84922013-01-20 23:32:17 -080099 BOOST_CHECK_EQUAL(log1->LookupLocator (user1), loc1);
100 BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2);
101 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
102 BOOST_CHECK_EQUAL(log2->LookupLocator (user2), loc2);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800103
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800104 // clean the test dir
105 if (exists(d))
106 {
107 remove_all(d);
108 }
109}
110
111BOOST_AUTO_TEST_SUITE_END()