Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2012-2021 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" |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame^] | 21 | #include "detail/bzip2-helper.hpp" |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 22 | |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 23 | #include "tests/boost-test.hpp" |
| 24 | #include "tests/identity-management-fixture.hpp" |
| 25 | #include "tests/unit-tests/dummy-forwarder.hpp" |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 27 | #include <ndn-cxx/util/random.hpp> |
| 28 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 29 | namespace chronosync { |
| 30 | namespace test { |
| 31 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 32 | using ndn::chronosync::DummyForwarder; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 33 | |
| 34 | class Handler |
| 35 | { |
| 36 | public: |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 37 | Handler(ndn::Face& face, |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 38 | const Name& syncPrefix, |
| 39 | const Name& userPrefix) |
| 40 | : logic(face, |
| 41 | syncPrefix, |
| 42 | userPrefix, |
| 43 | bind(&Handler::onUpdate, this, _1)) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 48 | onUpdate(const std::vector<MissingDataInfo>& v) |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 49 | { |
| 50 | for (size_t i = 0; i < v.size(); i++) { |
| 51 | update(v[i].session, v[i].high, v[i].low); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | update(const Name& p, const SeqNo& high, const SeqNo& low) |
| 57 | { |
| 58 | map[p] = high; |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | updateSeqNo(const SeqNo& seqNo) |
| 63 | { |
| 64 | logic.updateSeqNo(seqNo); |
| 65 | } |
| 66 | |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame] | 67 | public: |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 68 | Logic logic; |
| 69 | std::map<Name, SeqNo> map; |
| 70 | }; |
| 71 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 72 | class LogicFixture : public ndn::tests::IdentityManagementTimeFixture |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 73 | { |
| 74 | public: |
| 75 | LogicFixture() |
| 76 | : syncPrefix("/ndn/broadcast/sync") |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 77 | , fw(io, m_keyChain) |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 78 | { |
| 79 | syncPrefix.appendVersion(); |
| 80 | userPrefix[0] = Name("/user0"); |
| 81 | userPrefix[1] = Name("/user1"); |
| 82 | userPrefix[2] = Name("/user2"); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 83 | userPrefix[3] = Name("/user3"); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame] | 86 | public: |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 87 | Name syncPrefix; |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 88 | Name userPrefix[4]; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 89 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 90 | DummyForwarder fw; |
| 91 | // std::unique_ptr<DummyClientFace> faces[4]; |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 92 | shared_ptr<Handler> handler[4]; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 93 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 94 | // size_t readInterestOffset[4]; |
| 95 | // size_t readDataOffset[4]; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | BOOST_FIXTURE_TEST_SUITE(LogicTests, LogicFixture) |
| 99 | |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 100 | static void |
| 101 | onUpdate(const std::vector<MissingDataInfo>& v) |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 102 | { |
| 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(Constructor) |
| 106 | { |
| 107 | Name syncPrefix("/ndn/broadcast/sync"); |
| 108 | Name userPrefix("/user"); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 109 | ndn::util::DummyClientFace face(io, {true, true}); |
| 110 | BOOST_REQUIRE_NO_THROW(Logic(face, syncPrefix, userPrefix, bind(onUpdate, _1))); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE(TwoBasic) |
| 114 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 115 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 116 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 117 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 118 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 119 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 120 | |
| 121 | handler[0]->updateSeqNo(1); |
| 122 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 123 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 124 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 125 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 126 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 127 | |
| 128 | handler[0]->updateSeqNo(2); |
| 129 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 130 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 2); |
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 | |
| 134 | handler[1]->updateSeqNo(2); |
| 135 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 136 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2); |
| 138 | } |
| 139 | |
| 140 | BOOST_AUTO_TEST_CASE(ThreeBasic) |
| 141 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 142 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 143 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 144 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 145 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 146 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 147 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 148 | handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 149 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 150 | |
| 151 | handler[0]->updateSeqNo(1); |
| 152 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 153 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 154 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 155 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1); |
| 156 | |
| 157 | handler[1]->updateSeqNo(2); |
| 158 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 159 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2); |
| 161 | BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2); |
| 162 | |
| 163 | handler[2]->updateSeqNo(4); |
| 164 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 165 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4); |
| 167 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4); |
| 168 | } |
| 169 | |
| 170 | BOOST_AUTO_TEST_CASE(ResetRecover) |
| 171 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 172 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 173 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 174 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 175 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 176 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 177 | |
| 178 | handler[0]->updateSeqNo(1); |
| 179 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 180 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 181 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 182 | |
| 183 | handler[1]->updateSeqNo(2); |
| 184 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 185 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 186 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2); |
| 187 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 188 | advanceClocks(ndn::time::milliseconds(10), 100); |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 189 | handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 190 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 191 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 192 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1); |
| 193 | BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 194 | |
| 195 | handler[2]->updateSeqNo(4); |
| 196 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 197 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 198 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4); |
| 199 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4); |
| 200 | } |
| 201 | |
| 202 | BOOST_AUTO_TEST_CASE(RecoverConflict) |
| 203 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 204 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 205 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 206 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 207 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 208 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 209 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 210 | handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 211 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 212 | |
| 213 | handler[0]->updateSeqNo(1); |
| 214 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 215 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 216 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 217 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1); |
| 218 | |
| 219 | handler[1]->updateSeqNo(2); |
| 220 | handler[2]->updateSeqNo(4); |
| 221 | |
Davide Pesavento | 0d837e3 | 2019-03-16 22:31:14 -0400 | [diff] [blame] | 222 | advanceClocks(ndn::time::milliseconds(50), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 223 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2); |
| 224 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4); |
| 225 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4); |
| 226 | BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2); |
| 227 | } |
| 228 | |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 229 | BOOST_AUTO_TEST_CASE(PartitionRecover) |
| 230 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 231 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 232 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 233 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 234 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 235 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 236 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 237 | handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 238 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 239 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 240 | handler[3] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[3]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 241 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 242 | |
| 243 | handler[0]->updateSeqNo(1); |
| 244 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 245 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 246 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 247 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1); |
| 248 | BOOST_CHECK_EQUAL(handler[3]->map[handler[0]->logic.getSessionName()], 1); |
| 249 | |
| 250 | handler[2]->updateSeqNo(2); |
| 251 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 252 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 253 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 2); |
| 254 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 2); |
| 255 | BOOST_CHECK_EQUAL(handler[3]->map[handler[2]->logic.getSessionName()], 2); |
| 256 | |
| 257 | // Network Partition start |
| 258 | |
| 259 | handler[1]->updateSeqNo(3); |
| 260 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 261 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 262 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 3); |
| 263 | handler[2]->map[handler[1]->logic.getSessionName()] = 0; |
| 264 | handler[3]->map[handler[1]->logic.getSessionName()] = 0; |
| 265 | |
| 266 | handler[3]->updateSeqNo(4); |
| 267 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 268 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 269 | BOOST_CHECK_EQUAL(handler[2]->map[handler[3]->logic.getSessionName()], 4); |
| 270 | handler[0]->map[handler[3]->logic.getSessionName()] = 0; |
| 271 | handler[1]->map[handler[3]->logic.getSessionName()] = 0; |
| 272 | |
| 273 | // Network partition over |
| 274 | |
| 275 | handler[0]->updateSeqNo(5); |
| 276 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 277 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 278 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 5); |
| 279 | BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 5); |
| 280 | BOOST_CHECK_EQUAL(handler[3]->map[handler[0]->logic.getSessionName()], 5); |
| 281 | |
| 282 | handler[2]->updateSeqNo(6); |
| 283 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 284 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 285 | BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 6); |
| 286 | BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 6); |
| 287 | BOOST_CHECK_EQUAL(handler[3]->map[handler[2]->logic.getSessionName()], 6); |
| 288 | } |
| 289 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 290 | BOOST_AUTO_TEST_CASE(MultipleUserUnderOneLogic) |
| 291 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 292 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 293 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 294 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 295 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 296 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 297 | |
| 298 | handler[0]->logic.addUserNode(userPrefix[1]); |
| 299 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 300 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 301 | |
| 302 | handler[0]->updateSeqNo(1); |
| 303 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 304 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 305 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1); |
| 306 | |
| 307 | handler[0]->logic.updateSeqNo(2, userPrefix[1]); |
| 308 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 309 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 310 | BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName(userPrefix[1])], 2); |
| 311 | |
| 312 | handler[1]->updateSeqNo(4); |
| 313 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 314 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 315 | BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 4); |
| 316 | |
| 317 | handler[0]->logic.removeUserNode(userPrefix[0]); |
| 318 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 319 | advanceClocks(ndn::time::milliseconds(50), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 320 | BOOST_CHECK_EQUAL(handler[1]->logic.getSessionNames().size(), 2); |
| 321 | } |
| 322 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 323 | BOOST_AUTO_TEST_CASE(CancelOutstandingEvents) |
| 324 | { |
| 325 | auto h1 = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
| 326 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 327 | |
| 328 | auto h2 = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
| 329 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 330 | |
| 331 | h1->updateSeqNo(1); |
| 332 | |
| 333 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 334 | BOOST_CHECK_EQUAL(h2->map[h1->logic.getSessionName()], 1); |
| 335 | |
| 336 | h2->updateSeqNo(2); |
| 337 | |
| 338 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 339 | BOOST_CHECK_EQUAL(h1->map[h2->logic.getSessionName()], 2); |
| 340 | |
| 341 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 342 | auto h3 = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
| 343 | // Bringing this handler online later causes recovery interests to |
| 344 | // be sent -- h3 has no record of any digests |
| 345 | |
| 346 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 347 | BOOST_CHECK_EQUAL(h3->map[h1->logic.getSessionName()], 1); |
| 348 | BOOST_CHECK_EQUAL(h3->map[h2->logic.getSessionName()], 2); |
| 349 | |
| 350 | h3->updateSeqNo(4); |
| 351 | |
| 352 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 353 | BOOST_CHECK_EQUAL(h2->map[h3->logic.getSessionName()], 4); |
| 354 | BOOST_CHECK_EQUAL(h1->map[h3->logic.getSessionName()], 4); |
| 355 | |
| 356 | h1.reset(); |
| 357 | |
| 358 | BOOST_CHECK_NE(io.poll(), 0); // some cancel events handlers are expected |
| 359 | advanceClocks(ndn::time::minutes(1), 60); // should not crash |
| 360 | |
| 361 | h2.reset(); |
| 362 | h3.reset(); |
| 363 | |
| 364 | BOOST_CHECK_NE(io.poll(), 0); // some cancel events handlers are expected |
| 365 | fw.removeFaces(); |
| 366 | while (io.poll() != 0); // execute all other ready events that may have been scheduled |
| 367 | |
| 368 | steadyClock->advance(ndn::time::hours(1)); |
| 369 | systemClock->advance(ndn::time::hours(1)); |
| 370 | |
| 371 | BOOST_CHECK_EQUAL(io.poll(), 0); // no delayed handlers are expected |
| 372 | BOOST_CHECK_EQUAL(io.stopped(), true); // io_service expected to be stopped |
| 373 | } |
| 374 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 375 | BOOST_FIXTURE_TEST_CASE(TrimState, ndn::tests::IdentityManagementTimeFixture) |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 376 | { |
| 377 | Name syncPrefix("/ndn/broadcast/sync"); |
| 378 | Name userPrefix("/user"); |
| 379 | ndn::util::DummyClientFace face; |
| 380 | Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)); |
| 381 | |
| 382 | State state; |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 383 | for (size_t i = 0; i != 100; ++i) { |
| 384 | state.update(Name("/to/trim").appendNumber(i), 42); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 385 | } |
| 386 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 387 | State partial; |
| 388 | logic.trimState(partial, state, 1); |
| 389 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 99); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 390 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 391 | logic.trimState(partial, state, 100); |
| 392 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 393 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 394 | logic.trimState(partial, state, 101); |
| 395 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 396 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 397 | logic.trimState(partial, state, 42); |
| 398 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 58); |
| 399 | } |
| 400 | |
| 401 | BOOST_FIXTURE_TEST_CASE(VeryLargeState, ndn::tests::IdentityManagementTimeFixture) |
| 402 | { |
| 403 | addIdentity("/bla"); |
| 404 | Name syncPrefix("/ndn/broadcast/sync"); |
| 405 | Name userPrefix("/user"); |
| 406 | ndn::util::DummyClientFace face; |
| 407 | Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)); |
| 408 | |
| 409 | State state; |
| 410 | for (size_t i = 0; i < 50000 && bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().wire()), |
| 411 | state.wireEncode().size())->size() < ndn::MAX_NDN_PACKET_SIZE; |
| 412 | i += 10) { |
| 413 | Name prefix("/to/trim"); |
| 414 | prefix.appendNumber(i); |
| 415 | for (size_t j = 0; j != 20; ++j) { |
| 416 | prefix.appendNumber(ndn::random::generateWord32()); |
| 417 | } |
| 418 | state.update(prefix, ndn::random::generateWord32()); |
| 419 | } |
| 420 | BOOST_TEST_MESSAGE("Got state with " << state.getLeaves().size() << " leaves"); |
| 421 | |
| 422 | auto data = logic.encodeSyncReply(userPrefix, "/fake/prefix/of/interest", state); |
| 423 | BOOST_CHECK_LE(data.wireEncode().size(), ndn::MAX_NDN_PACKET_SIZE); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 424 | } |
| 425 | |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 426 | class MaxPacketCustomizationFixture |
| 427 | { |
| 428 | public: |
| 429 | MaxPacketCustomizationFixture() |
| 430 | { |
| 431 | if (getenv("CHRONOSYNC_MAX_PACKET_SIZE") != nullptr) { |
| 432 | oldSize = std::string(getenv("CHRONOSYNC_MAX_PACKET_SIZE")); |
| 433 | unsetenv("CHRONOSYNC_MAX_PACKET_SIZE"); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | ~MaxPacketCustomizationFixture() |
| 438 | { |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 439 | unsetenv("CHRONOSYNC_MAX_PACKET_SIZE"); |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 440 | if (oldSize) { |
| 441 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", oldSize->c_str(), 1); |
| 442 | } |
| 443 | } |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 444 | |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 445 | private: |
| 446 | ndn::optional<std::string> oldSize; |
| 447 | }; |
| 448 | |
| 449 | BOOST_FIXTURE_TEST_CASE(MaxPacketCustomization, MaxPacketCustomizationFixture) |
| 450 | { |
| 451 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE); |
| 452 | |
| 453 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1500", 1); |
| 454 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), 1500); |
| 455 | |
| 456 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", ndn::to_string(ndn::MAX_NDN_PACKET_SIZE * 100).c_str(), 1); |
| 457 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE); |
| 458 | |
| 459 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1", 1); |
| 460 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), 500); |
| 461 | } |
| 462 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 463 | BOOST_AUTO_TEST_SUITE_END() |
| 464 | |
| 465 | } // namespace test |
| 466 | } // namespace chronosync |