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