blob: 0d55fd10f5323e3356315a7b288ab5e8f2892bed [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
4 *
5 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
6 *
7 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
19 */
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080020#include "logging.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080021#include "sync-core.hpp"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080022
Zhenkai Zhue29616f2013-01-14 15:40:57 -080023#include <boost/filesystem.hpp>
Alexander Afanasyevf278db32013-01-21 14:41:01 -080024#include <boost/make_shared.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080025#include <boost/test/unit_test.hpp>
Zhenkai Zhue29616f2013-01-14 15:40:57 -080026
27using namespace std;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070028using namespace Ndnx;
Alexander Afanasyevf278db32013-01-21 14:41:01 -080029using namespace boost;
Zhenkai Zhue29616f2013-01-14 15:40:57 -080030using namespace boost::filesystem;
31
Alexander Afanasyevfc720362013-01-24 21:49:48 -080032INIT_LOGGER("Test.SyncCore");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080033
Zhenkai Zhue29616f2013-01-14 15:40:57 -080034BOOST_AUTO_TEST_SUITE(SyncCoreTests)
35
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080036void
37callback(const SyncStateMsgPtr& msg)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080038{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080039 BOOST_CHECK(msg->state_size() > 0);
Zhenkai Zhu085aae72013-01-17 21:09:01 -080040 int size = msg->state_size();
41 int index = 0;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080042 while (index < size) {
Zhenkai Zhu085aae72013-01-17 21:09:01 -080043 SyncState state = msg->state(index);
44 BOOST_CHECK(state.has_old_seq());
45 BOOST_CHECK(state.old_seq() >= 0);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080046 if (state.seq() != 0) {
Alexander Afanasyevdac84922013-01-20 23:32:17 -080047 BOOST_CHECK(state.old_seq() != state.seq());
Zhenkai Zhu085aae72013-01-17 21:09:01 -080048 }
49 index++;
50 }
Zhenkai Zhue29616f2013-01-14 15:40:57 -080051}
52
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080053void
54checkRoots(const HashPtr& root1, const HashPtr& root2)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080055{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080056 BOOST_CHECK_EQUAL(*root1, *root2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080057}
58
59BOOST_AUTO_TEST_CASE(SyncCoreTest)
60{
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080061 INIT_LOGGERS();
62
Zhenkai Zhue29616f2013-01-14 15:40:57 -080063 string dir = "./SyncCoreTest";
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080064 // clean the test dir
65 path d(dir);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080066 if (exists(d)) {
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080067 remove_all(d);
68 }
69
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080070 string dir1 = "./SyncCoreTest/1";
71 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080072 Name user1("/joker");
73 Name loc1("/gotham1");
74 Name user2("/darkknight");
75 Name loc2("/gotham2");
76 Name syncPrefix("/broadcast/darkknight");
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070077 NdnxWrapperPtr c1(new NdnxWrapper());
78 NdnxWrapperPtr c2(new NdnxWrapper());
Zhenkai Zhub330aed2013-01-17 13:29:37 -080079 SyncLogPtr log1(new SyncLog(dir1, user1.toString()));
80 SyncLogPtr log2(new SyncLog(dir2, user2.toString()));
81
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080082 SyncCore* core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080083 usleep(10000);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080084 SyncCore* core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080085
86 sleep(1);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080087 checkRoots(core1->root(), core2->root());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080088
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080089 // _LOG_TRACE ("\n\n\n\n\n\n----------\n");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080090
Zhenkai Zhue29616f2013-01-14 15:40:57 -080091 core1->updateLocalState(1);
92 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080093 checkRoots(core1->root(), core2->root());
94 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080095 BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080096
97 core1->updateLocalState(5);
98 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080099 checkRoots(core1->root(), core2->root());
100 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101 BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800102
103 core2->updateLocalState(10);
104 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800105 checkRoots(core1->root(), core2->root());
106 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 BOOST_CHECK_EQUAL(log1->LookupLocator(user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800108
109 // simple simultaneous data generation
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800110 // _LOG_TRACE ("\n\n\n\n\n\n----------Simultaneous\n");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800111 _LOG_TRACE("Simultaneous");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800112
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800113 core1->updateLocalState(11);
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800114 usleep(100);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800115 core2->updateLocalState(15);
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800116 usleep(2000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800117 checkRoots(core1->root(), core2->root());
118 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
119 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800120
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800121 BOOST_CHECK_EQUAL(log1->LookupLocator(user1), loc1);
122 BOOST_CHECK_EQUAL(log1->LookupLocator(user2), loc2);
123 BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
124 BOOST_CHECK_EQUAL(log2->LookupLocator(user2), loc2);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800125
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800126 // clean the test dir
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800127 if (exists(d)) {
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800128 remove_all(d);
129 }
130}
131
132BOOST_AUTO_TEST_SUITE_END()