Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- 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 Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "sync-core.hpp" |
| 22 | #include "logging.hpp" |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 23 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 24 | #include <boost/test/unit_test.hpp> |
| 25 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 26 | #include <boost/make_shared.hpp> |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 27 | |
| 28 | using namespace std; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 29 | using namespace Ndnx; |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 30 | using namespace boost; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 31 | using namespace boost::filesystem; |
| 32 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 33 | INIT_LOGGER("Test.SyncCore"); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 34 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(SyncCoreTests) |
| 36 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 37 | void callback(const SyncStateMsgPtr &msg) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 38 | { |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 39 | BOOST_CHECK(msg->state_size() > 0); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 40 | 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 Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 49 | BOOST_CHECK(state.old_seq() != state.seq()); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 50 | } |
| 51 | index++; |
| 52 | } |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 55 | void checkRoots(const HashPtr &root1, const HashPtr &root2) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 56 | { |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(*root1, *root2); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | BOOST_AUTO_TEST_CASE(SyncCoreTest) |
| 61 | { |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 62 | INIT_LOGGERS(); |
| 63 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 64 | string dir = "./SyncCoreTest"; |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 65 | // clean the test dir |
| 66 | path d(dir); |
| 67 | if (exists(d)) |
| 68 | { |
| 69 | remove_all(d); |
| 70 | } |
| 71 | |
Zhenkai Zhu | 05de64a | 2013-01-14 15:48:23 -0800 | [diff] [blame] | 72 | string dir1 = "./SyncCoreTest/1"; |
| 73 | string dir2 = "./SyncCoreTest/2"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 74 | Name user1("/joker"); |
| 75 | Name loc1("/gotham1"); |
| 76 | Name user2("/darkknight"); |
| 77 | Name loc2("/gotham2"); |
| 78 | Name syncPrefix("/broadcast/darkknight"); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 79 | NdnxWrapperPtr c1(new NdnxWrapper()); |
| 80 | NdnxWrapperPtr c2(new NdnxWrapper()); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 81 | SyncLogPtr log1(new SyncLog(dir1, user1.toString())); |
| 82 | SyncLogPtr log2(new SyncLog(dir2, user2.toString())); |
| 83 | |
Alexander Afanasyev | d94a8c6 | 2013-01-24 13:53:40 -0800 | [diff] [blame] | 84 | SyncCore *core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 85 | usleep(10000); |
Alexander Afanasyev | d94a8c6 | 2013-01-24 13:53:40 -0800 | [diff] [blame] | 86 | SyncCore *core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 87 | |
| 88 | sleep(1); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 89 | checkRoots(core1->root(), core2->root()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 91 | // _LOG_TRACE ("\n\n\n\n\n\n----------\n"); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 92 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 93 | core1->updateLocalState(1); |
| 94 | usleep(100000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 95 | checkRoots(core1->root(), core2->root()); |
| 96 | BOOST_CHECK_EQUAL(core2->seq(user1), 1); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 98 | |
| 99 | core1->updateLocalState(5); |
| 100 | usleep(100000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 101 | checkRoots(core1->root(), core2->root()); |
| 102 | BOOST_CHECK_EQUAL(core2->seq(user1), 5); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 104 | |
| 105 | core2->updateLocalState(10); |
| 106 | usleep(100000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 107 | checkRoots(core1->root(), core2->root()); |
| 108 | BOOST_CHECK_EQUAL(core1->seq(user2), 10); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 109 | BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 110 | |
| 111 | // simple simultaneous data generation |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 112 | // _LOG_TRACE ("\n\n\n\n\n\n----------Simultaneous\n"); |
| 113 | _LOG_TRACE ("Simultaneous"); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 114 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 115 | core1->updateLocalState(11); |
Alexander Afanasyev | beee0b4 | 2013-01-16 18:25:08 -0800 | [diff] [blame] | 116 | usleep(100); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 117 | core2->updateLocalState(15); |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 118 | usleep(2000000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 119 | checkRoots(core1->root(), core2->root()); |
| 120 | BOOST_CHECK_EQUAL(core1->seq(user2), 15); |
| 121 | BOOST_CHECK_EQUAL(core2->seq(user1), 11); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 123 | 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 Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 127 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 128 | // clean the test dir |
| 129 | if (exists(d)) |
| 130 | { |
| 131 | remove_all(d); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | BOOST_AUTO_TEST_SUITE_END() |