Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 3 | * Copyright (c) 2012-2018 University of California, Los Angeles |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
| 7 | * |
| 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "logic.hpp" |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame^] | 21 | #include "bzip2-helper.hpp" |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 22 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 23 | #include "boost-test.hpp" |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 24 | #include "../identity-management-fixture.hpp" |
| 25 | |
| 26 | #include "dummy-forwarder.hpp" |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame^] | 28 | #include <ndn-cxx/util/random.hpp> |
| 29 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 30 | namespace chronosync { |
| 31 | namespace test { |
| 32 | |
| 33 | using std::vector; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 34 | using ndn::chronosync::DummyForwarder; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 35 | |
| 36 | class Handler |
| 37 | { |
| 38 | public: |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 39 | Handler(ndn::Face& face, |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 40 | const Name& syncPrefix, |
| 41 | const Name& userPrefix) |
| 42 | : logic(face, |
| 43 | syncPrefix, |
| 44 | userPrefix, |
| 45 | bind(&Handler::onUpdate, this, _1)) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | onUpdate(const vector<MissingDataInfo>& v) |
| 51 | { |
| 52 | for (size_t i = 0; i < v.size(); i++) { |
| 53 | update(v[i].session, v[i].high, v[i].low); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | update(const Name& p, const SeqNo& high, const SeqNo& low) |
| 59 | { |
| 60 | map[p] = high; |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | updateSeqNo(const SeqNo& seqNo) |
| 65 | { |
| 66 | logic.updateSeqNo(seqNo); |
| 67 | } |
| 68 | |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame] | 69 | public: |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 70 | Logic logic; |
| 71 | std::map<Name, SeqNo> map; |
| 72 | }; |
| 73 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 74 | class LogicFixture : public ndn::tests::IdentityManagementTimeFixture |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 75 | { |
| 76 | public: |
| 77 | LogicFixture() |
| 78 | : syncPrefix("/ndn/broadcast/sync") |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 79 | , fw(io, m_keyChain) |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 80 | { |
| 81 | syncPrefix.appendVersion(); |
| 82 | userPrefix[0] = Name("/user0"); |
| 83 | userPrefix[1] = Name("/user1"); |
| 84 | userPrefix[2] = Name("/user2"); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 85 | userPrefix[3] = Name("/user3"); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame] | 88 | public: |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 89 | Name syncPrefix; |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 90 | Name userPrefix[4]; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 91 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 92 | DummyForwarder fw; |
| 93 | // std::unique_ptr<DummyClientFace> faces[4]; |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 94 | shared_ptr<Handler> handler[4]; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 95 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 96 | // size_t readInterestOffset[4]; |
| 97 | // size_t readDataOffset[4]; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | BOOST_FIXTURE_TEST_SUITE(LogicTests, LogicFixture) |
| 101 | |
| 102 | void |
| 103 | onUpdate(const vector<MissingDataInfo>& v) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(Constructor) |
| 108 | { |
| 109 | Name syncPrefix("/ndn/broadcast/sync"); |
| 110 | Name userPrefix("/user"); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 111 | ndn::util::DummyClientFace face(io, {true, true}); |
| 112 | BOOST_REQUIRE_NO_THROW(Logic(face, syncPrefix, userPrefix, bind(onUpdate, _1))); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | BOOST_AUTO_TEST_CASE(TwoBasic) |
| 116 | { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 117 | handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]); |
| 118 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 119 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 120 | handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]); |
| 121 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 122 | |
| 123 | handler[0]->updateSeqNo(1); |
| 124 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 125 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 126 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 127 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 128 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 129 | |
| 130 | handler[0]->updateSeqNo(2); |
| 131 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 132 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 133 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 2); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 134 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 135 | |
| 136 | handler[1]->updateSeqNo(2); |
| 137 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 138 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2); |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_CASE(ThreeBasic) |
| 143 | { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 144 | handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]); |
| 145 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 146 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 147 | handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]); |
| 148 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 149 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 150 | handler[2] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]); |
| 151 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 152 | |
| 153 | handler[0]->updateSeqNo(1); |
| 154 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 155 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 157 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1); |
| 158 | |
| 159 | handler[1]->updateSeqNo(2); |
| 160 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 161 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2); |
| 163 | BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2); |
| 164 | |
| 165 | handler[2]->updateSeqNo(4); |
| 166 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 167 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4); |
| 169 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4); |
| 170 | } |
| 171 | |
| 172 | BOOST_AUTO_TEST_CASE(ResetRecover) |
| 173 | { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 174 | handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]); |
| 175 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 176 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 177 | handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]); |
| 178 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 179 | |
| 180 | handler[0]->updateSeqNo(1); |
| 181 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 182 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 183 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 184 | |
| 185 | handler[1]->updateSeqNo(2); |
| 186 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 187 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 188 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2); |
| 189 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 190 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 191 | handler[2] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 192 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 193 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 194 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1); |
| 195 | BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 196 | |
| 197 | handler[2]->updateSeqNo(4); |
| 198 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 199 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 200 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4); |
| 201 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4); |
| 202 | } |
| 203 | |
| 204 | BOOST_AUTO_TEST_CASE(RecoverConflict) |
| 205 | { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 206 | handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]); |
| 207 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 208 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 209 | handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]); |
| 210 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 211 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 212 | handler[2] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]); |
| 213 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 214 | |
| 215 | handler[0]->updateSeqNo(1); |
| 216 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 217 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 218 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 219 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1); |
| 220 | |
| 221 | handler[1]->updateSeqNo(2); |
| 222 | handler[2]->updateSeqNo(4); |
| 223 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 224 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 225 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2); |
| 226 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4); |
| 227 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4); |
| 228 | BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2); |
| 229 | } |
| 230 | |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 231 | BOOST_AUTO_TEST_CASE(PartitionRecover) |
| 232 | { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 233 | handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]); |
| 234 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 235 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 236 | handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]); |
| 237 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 238 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 239 | handler[2] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]); |
| 240 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 241 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 242 | handler[3] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[3]); |
| 243 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 244 | |
| 245 | handler[0]->updateSeqNo(1); |
| 246 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 247 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 248 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 249 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1); |
| 250 | BOOST_CHECK_EQUAL(handler[3]->map[handler[0]->logic.getSessionName()], 1); |
| 251 | |
| 252 | handler[2]->updateSeqNo(2); |
| 253 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 254 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 255 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 2); |
| 256 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 2); |
| 257 | BOOST_CHECK_EQUAL(handler[3]->map[handler[2]->logic.getSessionName()], 2); |
| 258 | |
| 259 | // Network Partition start |
| 260 | |
| 261 | handler[1]->updateSeqNo(3); |
| 262 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 263 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 264 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 3); |
| 265 | handler[2]->map[handler[1]->logic.getSessionName()] = 0; |
| 266 | handler[3]->map[handler[1]->logic.getSessionName()] = 0; |
| 267 | |
| 268 | handler[3]->updateSeqNo(4); |
| 269 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 270 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 271 | BOOST_CHECK_EQUAL(handler[2]->map[handler[3]->logic.getSessionName()], 4); |
| 272 | handler[0]->map[handler[3]->logic.getSessionName()] = 0; |
| 273 | handler[1]->map[handler[3]->logic.getSessionName()] = 0; |
| 274 | |
| 275 | // Network partition over |
| 276 | |
| 277 | handler[0]->updateSeqNo(5); |
| 278 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 279 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 280 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 5); |
| 281 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 5); |
| 282 | BOOST_CHECK_EQUAL(handler[3]->map[handler[0]->logic.getSessionName()], 5); |
| 283 | |
| 284 | handler[2]->updateSeqNo(6); |
| 285 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 286 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 287 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 6); |
| 288 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 6); |
| 289 | BOOST_CHECK_EQUAL(handler[3]->map[handler[2]->logic.getSessionName()], 6); |
| 290 | } |
| 291 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 292 | BOOST_AUTO_TEST_CASE(MultipleUserUnderOneLogic) |
| 293 | { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 294 | handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]); |
| 295 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 296 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 297 | handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]); |
| 298 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 299 | |
| 300 | handler[0]->logic.addUserNode(userPrefix[1]); |
| 301 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 302 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 303 | |
| 304 | handler[0]->updateSeqNo(1); |
| 305 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 306 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 307 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 308 | |
| 309 | handler[0]->logic.updateSeqNo(2, userPrefix[1]); |
| 310 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 311 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 312 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName(userPrefix[1])], 2); |
| 313 | |
| 314 | handler[1]->updateSeqNo(4); |
| 315 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 316 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 317 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 4); |
| 318 | |
| 319 | handler[0]->logic.removeUserNode(userPrefix[0]); |
| 320 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 321 | advanceClocks(ndn::time::milliseconds(50), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 322 | BOOST_CHECK_EQUAL(handler[1]->logic.getSessionNames().size(), 2); |
| 323 | } |
| 324 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame^] | 325 | BOOST_FIXTURE_TEST_CASE(TrimState, ndn::tests::IdentityManagementTimeFixture) |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 326 | { |
| 327 | Name syncPrefix("/ndn/broadcast/sync"); |
| 328 | Name userPrefix("/user"); |
| 329 | ndn::util::DummyClientFace face; |
| 330 | Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)); |
| 331 | |
| 332 | State state; |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame^] | 333 | for (size_t i = 0; i != 100; ++i) { |
| 334 | state.update(Name("/to/trim").appendNumber(i), 42); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 335 | } |
| 336 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame^] | 337 | State partial; |
| 338 | logic.trimState(partial, state, 1); |
| 339 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 99); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 340 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame^] | 341 | logic.trimState(partial, state, 100); |
| 342 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 343 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame^] | 344 | logic.trimState(partial, state, 101); |
| 345 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 346 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame^] | 347 | logic.trimState(partial, state, 42); |
| 348 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 58); |
| 349 | } |
| 350 | |
| 351 | BOOST_FIXTURE_TEST_CASE(VeryLargeState, ndn::tests::IdentityManagementTimeFixture) |
| 352 | { |
| 353 | addIdentity("/bla"); |
| 354 | Name syncPrefix("/ndn/broadcast/sync"); |
| 355 | Name userPrefix("/user"); |
| 356 | ndn::util::DummyClientFace face; |
| 357 | Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)); |
| 358 | |
| 359 | State state; |
| 360 | for (size_t i = 0; i < 50000 && bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().wire()), |
| 361 | state.wireEncode().size())->size() < ndn::MAX_NDN_PACKET_SIZE; |
| 362 | i += 10) { |
| 363 | Name prefix("/to/trim"); |
| 364 | prefix.appendNumber(i); |
| 365 | for (size_t j = 0; j != 20; ++j) { |
| 366 | prefix.appendNumber(ndn::random::generateWord32()); |
| 367 | } |
| 368 | state.update(prefix, ndn::random::generateWord32()); |
| 369 | } |
| 370 | BOOST_TEST_MESSAGE("Got state with " << state.getLeaves().size() << " leaves"); |
| 371 | |
| 372 | auto data = logic.encodeSyncReply(userPrefix, "/fake/prefix/of/interest", state); |
| 373 | BOOST_CHECK_LE(data.wireEncode().size(), ndn::MAX_NDN_PACKET_SIZE); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 374 | } |
| 375 | |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 376 | class MaxPacketCustomizationFixture |
| 377 | { |
| 378 | public: |
| 379 | MaxPacketCustomizationFixture() |
| 380 | { |
| 381 | if (getenv("CHRONOSYNC_MAX_PACKET_SIZE") != nullptr) { |
| 382 | oldSize = std::string(getenv("CHRONOSYNC_MAX_PACKET_SIZE")); |
| 383 | unsetenv("CHRONOSYNC_MAX_PACKET_SIZE"); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | ~MaxPacketCustomizationFixture() |
| 388 | { |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame^] | 389 | unsetenv("CHRONOSYNC_MAX_PACKET_SIZE"); |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 390 | if (oldSize) { |
| 391 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", oldSize->c_str(), 1); |
| 392 | } |
| 393 | } |
| 394 | private: |
| 395 | ndn::optional<std::string> oldSize; |
| 396 | }; |
| 397 | |
| 398 | BOOST_FIXTURE_TEST_CASE(MaxPacketCustomization, MaxPacketCustomizationFixture) |
| 399 | { |
| 400 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE); |
| 401 | |
| 402 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1500", 1); |
| 403 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), 1500); |
| 404 | |
| 405 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", ndn::to_string(ndn::MAX_NDN_PACKET_SIZE * 100).c_str(), 1); |
| 406 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE); |
| 407 | |
| 408 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1", 1); |
| 409 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), 500); |
| 410 | } |
| 411 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 412 | BOOST_AUTO_TEST_SUITE_END() |
| 413 | |
| 414 | } // namespace test |
| 415 | } // namespace chronosync |