blob: bfdaec26a5299830a96074ed2f31f6b4fedc70de [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 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "sync-core.hpp"
22#include "logging.hpp"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080023
Zhenkai Zhue29616f2013-01-14 15:40:57 -080024#include <boost/test/unit_test.hpp>
25#include <boost/filesystem.hpp>
Alexander Afanasyevf278db32013-01-21 14:41:01 -080026#include <boost/make_shared.hpp>
Zhenkai Zhue29616f2013-01-14 15:40:57 -080027
28using namespace std;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070029using namespace Ndnx;
Alexander Afanasyevf278db32013-01-21 14:41:01 -080030using namespace boost;
Zhenkai Zhue29616f2013-01-14 15:40:57 -080031using namespace boost::filesystem;
32
Alexander Afanasyevfc720362013-01-24 21:49:48 -080033INIT_LOGGER("Test.SyncCore");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080034
Zhenkai Zhue29616f2013-01-14 15:40:57 -080035BOOST_AUTO_TEST_SUITE(SyncCoreTests)
36
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080037void callback(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;
42 while (index < size)
43 {
44 SyncState state = msg->state(index);
45 BOOST_CHECK(state.has_old_seq());
46 BOOST_CHECK(state.old_seq() >= 0);
47 if (state.seq() != 0)
48 {
Alexander Afanasyevdac84922013-01-20 23:32:17 -080049 BOOST_CHECK(state.old_seq() != state.seq());
Zhenkai Zhu085aae72013-01-17 21:09:01 -080050 }
51 index++;
52 }
Zhenkai Zhue29616f2013-01-14 15:40:57 -080053}
54
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080055void checkRoots(const HashPtr &root1, const HashPtr &root2)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080056{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080057 BOOST_CHECK_EQUAL(*root1, *root2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080058}
59
60BOOST_AUTO_TEST_CASE(SyncCoreTest)
61{
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080062 INIT_LOGGERS();
63
Zhenkai Zhue29616f2013-01-14 15:40:57 -080064 string dir = "./SyncCoreTest";
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080065 // clean the test dir
66 path d(dir);
67 if (exists(d))
68 {
69 remove_all(d);
70 }
71
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080072 string dir1 = "./SyncCoreTest/1";
73 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080074 Name user1("/joker");
75 Name loc1("/gotham1");
76 Name user2("/darkknight");
77 Name loc2("/gotham2");
78 Name syncPrefix("/broadcast/darkknight");
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070079 NdnxWrapperPtr c1(new NdnxWrapper());
80 NdnxWrapperPtr c2(new NdnxWrapper());
Zhenkai Zhub330aed2013-01-17 13:29:37 -080081 SyncLogPtr log1(new SyncLog(dir1, user1.toString()));
82 SyncLogPtr log2(new SyncLog(dir2, user2.toString()));
83
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080084 SyncCore *core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080085 usleep(10000);
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080086 SyncCore *core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080087
88 sleep(1);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080089 checkRoots(core1->root(), core2->root());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080090
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080091 // _LOG_TRACE ("\n\n\n\n\n\n----------\n");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080092
Zhenkai Zhue29616f2013-01-14 15:40:57 -080093 core1->updateLocalState(1);
94 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080095 checkRoots(core1->root(), core2->root());
96 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080097 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080098
99 core1->updateLocalState(5);
100 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800101 checkRoots(core1->root(), core2->root());
102 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800103 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800104
105 core2->updateLocalState(10);
106 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800107 checkRoots(core1->root(), core2->root());
108 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800109 BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800110
111 // simple simultaneous data generation
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800112 // _LOG_TRACE ("\n\n\n\n\n\n----------Simultaneous\n");
113 _LOG_TRACE ("Simultaneous");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800114
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800115 core1->updateLocalState(11);
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800116 usleep(100);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800117 core2->updateLocalState(15);
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800118 usleep(2000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800119 checkRoots(core1->root(), core2->root());
120 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
121 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800122
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800123 BOOST_CHECK_EQUAL(log1->LookupLocator (user1), loc1);
124 BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2);
125 BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1);
126 BOOST_CHECK_EQUAL(log2->LookupLocator (user2), loc2);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800127
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800128 // clean the test dir
129 if (exists(d))
130 {
131 remove_all(d);
132 }
133}
134
135BOOST_AUTO_TEST_SUITE_END()