blob: a7ab3c0e6a7e64324e21e854665f3f3616647cb7 [file] [log] [blame]
Zhenkai Zhue29616f2013-01-14 15:40:57 -08001#include "sync-core.h"
Alexander Afanasyevc507ac22013-01-21 16:01:58 -08002#include "logging.h"
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
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080013INIT_LOGGER("Test.Sync.Core");
14
Zhenkai Zhue29616f2013-01-14 15:40:57 -080015BOOST_AUTO_TEST_SUITE(SyncCoreTests)
16
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080017void callback(const SyncStateMsgPtr &msg)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080018{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080019 BOOST_CHECK(msg->state_size() > 0);
Zhenkai Zhu085aae72013-01-17 21:09:01 -080020 int size = msg->state_size();
21 int index = 0;
22 while (index < size)
23 {
24 SyncState state = msg->state(index);
25 BOOST_CHECK(state.has_old_seq());
26 BOOST_CHECK(state.old_seq() >= 0);
27 if (state.seq() != 0)
28 {
Alexander Afanasyevdac84922013-01-20 23:32:17 -080029 BOOST_CHECK(state.old_seq() != state.seq());
Zhenkai Zhu085aae72013-01-17 21:09:01 -080030 }
31 index++;
32 }
Zhenkai Zhue29616f2013-01-14 15:40:57 -080033}
34
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080035void checkRoots(const HashPtr &root1, const HashPtr &root2)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080036{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080037 BOOST_CHECK_EQUAL(*root1, *root2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080038}
39
40BOOST_AUTO_TEST_CASE(SyncCoreTest)
41{
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080042 INIT_LOGGERS();
43
Zhenkai Zhue29616f2013-01-14 15:40:57 -080044 string dir = "./SyncCoreTest";
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080045 // clean the test dir
46 path d(dir);
47 if (exists(d))
48 {
49 remove_all(d);
50 }
51
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080052 string dir1 = "./SyncCoreTest/1";
53 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080054 Name user1("/joker");
55 Name loc1("/gotham1");
56 Name user2("/darkknight");
57 Name loc2("/gotham2");
58 Name syncPrefix("/broadcast/darkknight");
59 CcnxWrapperPtr c1(new CcnxWrapper());
60 CcnxWrapperPtr c2(new CcnxWrapper());
Zhenkai Zhub330aed2013-01-17 13:29:37 -080061 SyncLogPtr log1(new SyncLog(dir1, user1.toString()));
62 SyncLogPtr log2(new SyncLog(dir2, user2.toString()));
63
Alexander Afanasyevf278db32013-01-21 14:41:01 -080064 // should not have used the same scheduler...
65 SchedulerPtr scheduler1 = make_shared<Scheduler> ();
66 SchedulerPtr scheduler2 = make_shared<Scheduler> ();
Zhenkai Zhue29616f2013-01-14 15:40:57 -080067
Alexander Afanasyevf278db32013-01-21 14:41:01 -080068 SyncCore *core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1, scheduler1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080069 usleep(10000);
Alexander Afanasyevf278db32013-01-21 14:41:01 -080070 SyncCore *core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2, scheduler2);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080071 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080072 checkRoots(core1->root(), core2->root());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080073
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080074 // _LOG_TRACE ("\n\n\n\n\n\n----------\n");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080075
Zhenkai Zhue29616f2013-01-14 15:40:57 -080076 core1->updateLocalState(1);
77 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080078 checkRoots(core1->root(), core2->root());
79 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080080 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080081
82 core1->updateLocalState(5);
83 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080084 checkRoots(core1->root(), core2->root());
85 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080086 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080087
88 core2->updateLocalState(10);
89 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080090 checkRoots(core1->root(), core2->root());
91 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080092 BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080093
94 // simple simultaneous data generation
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080095 // _LOG_TRACE ("\n\n\n\n\n\n----------Simultaneous\n");
96 _LOG_TRACE ("Simultaneous");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080097
Zhenkai Zhue29616f2013-01-14 15:40:57 -080098 core1->updateLocalState(11);
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080099 usleep(100);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800100 core2->updateLocalState(15);
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800101 usleep(2000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800102 checkRoots(core1->root(), core2->root());
103 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
104 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800105
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800106 BOOST_CHECK_EQUAL(log1->LookupLocator (user1), loc1);
107 BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2);
108 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
109 BOOST_CHECK_EQUAL(log2->LookupLocator (user2), loc2);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800110
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800111 // clean the test dir
112 if (exists(d))
113 {
114 remove_all(d);
115 }
116}
117
118BOOST_AUTO_TEST_SUITE_END()