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