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 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 117 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 118 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 119 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 120 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 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 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 144 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 145 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 146 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 147 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 148 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 149 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 150 | handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 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 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 174 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 175 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 176 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 177 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
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 | |
| 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); |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 191 | handler[2] = make_shared<Handler>(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 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 206 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 207 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 208 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 209 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 210 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 211 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 212 | handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 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 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 233 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
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 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 236 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 237 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 238 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 239 | handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 240 | advanceClocks(ndn::time::milliseconds(10), 100); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 241 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 242 | handler[3] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[3]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 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 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 294 | handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 295 | advanceClocks(ndn::time::milliseconds(10), 100); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 296 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 297 | handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
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 | |
| 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 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 325 | BOOST_AUTO_TEST_CASE(CancelOutstandingEvents) |
| 326 | { |
| 327 | auto h1 = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]); |
| 328 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 329 | |
| 330 | auto h2 = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]); |
| 331 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 332 | |
| 333 | h1->updateSeqNo(1); |
| 334 | |
| 335 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 336 | BOOST_CHECK_EQUAL(h2->map[h1->logic.getSessionName()], 1); |
| 337 | |
| 338 | h2->updateSeqNo(2); |
| 339 | |
| 340 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 341 | BOOST_CHECK_EQUAL(h1->map[h2->logic.getSessionName()], 2); |
| 342 | |
| 343 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 344 | auto h3 = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]); |
| 345 | // Bringing this handler online later causes recovery interests to |
| 346 | // be sent -- h3 has no record of any digests |
| 347 | |
| 348 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 349 | BOOST_CHECK_EQUAL(h3->map[h1->logic.getSessionName()], 1); |
| 350 | BOOST_CHECK_EQUAL(h3->map[h2->logic.getSessionName()], 2); |
| 351 | |
| 352 | h3->updateSeqNo(4); |
| 353 | |
| 354 | advanceClocks(ndn::time::milliseconds(10), 100); |
| 355 | BOOST_CHECK_EQUAL(h2->map[h3->logic.getSessionName()], 4); |
| 356 | BOOST_CHECK_EQUAL(h1->map[h3->logic.getSessionName()], 4); |
| 357 | |
| 358 | h1.reset(); |
| 359 | |
| 360 | BOOST_CHECK_NE(io.poll(), 0); // some cancel events handlers are expected |
| 361 | advanceClocks(ndn::time::minutes(1), 60); // should not crash |
| 362 | |
| 363 | h2.reset(); |
| 364 | h3.reset(); |
| 365 | |
| 366 | BOOST_CHECK_NE(io.poll(), 0); // some cancel events handlers are expected |
| 367 | fw.removeFaces(); |
| 368 | while (io.poll() != 0); // execute all other ready events that may have been scheduled |
| 369 | |
| 370 | steadyClock->advance(ndn::time::hours(1)); |
| 371 | systemClock->advance(ndn::time::hours(1)); |
| 372 | |
| 373 | BOOST_CHECK_EQUAL(io.poll(), 0); // no delayed handlers are expected |
| 374 | BOOST_CHECK_EQUAL(io.stopped(), true); // io_service expected to be stopped |
| 375 | } |
| 376 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 377 | BOOST_FIXTURE_TEST_CASE(TrimState, ndn::tests::IdentityManagementTimeFixture) |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 378 | { |
| 379 | Name syncPrefix("/ndn/broadcast/sync"); |
| 380 | Name userPrefix("/user"); |
| 381 | ndn::util::DummyClientFace face; |
| 382 | Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)); |
| 383 | |
| 384 | State state; |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 385 | for (size_t i = 0; i != 100; ++i) { |
| 386 | state.update(Name("/to/trim").appendNumber(i), 42); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 387 | } |
| 388 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 389 | State partial; |
| 390 | logic.trimState(partial, state, 1); |
| 391 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 99); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 392 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 393 | logic.trimState(partial, state, 100); |
| 394 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 395 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 396 | logic.trimState(partial, state, 101); |
| 397 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 398 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 399 | logic.trimState(partial, state, 42); |
| 400 | BOOST_CHECK_EQUAL(partial.getLeaves().size(), 58); |
| 401 | } |
| 402 | |
| 403 | BOOST_FIXTURE_TEST_CASE(VeryLargeState, ndn::tests::IdentityManagementTimeFixture) |
| 404 | { |
| 405 | addIdentity("/bla"); |
| 406 | Name syncPrefix("/ndn/broadcast/sync"); |
| 407 | Name userPrefix("/user"); |
| 408 | ndn::util::DummyClientFace face; |
| 409 | Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)); |
| 410 | |
| 411 | State state; |
| 412 | for (size_t i = 0; i < 50000 && bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().wire()), |
| 413 | state.wireEncode().size())->size() < ndn::MAX_NDN_PACKET_SIZE; |
| 414 | i += 10) { |
| 415 | Name prefix("/to/trim"); |
| 416 | prefix.appendNumber(i); |
| 417 | for (size_t j = 0; j != 20; ++j) { |
| 418 | prefix.appendNumber(ndn::random::generateWord32()); |
| 419 | } |
| 420 | state.update(prefix, ndn::random::generateWord32()); |
| 421 | } |
| 422 | BOOST_TEST_MESSAGE("Got state with " << state.getLeaves().size() << " leaves"); |
| 423 | |
| 424 | auto data = logic.encodeSyncReply(userPrefix, "/fake/prefix/of/interest", state); |
| 425 | BOOST_CHECK_LE(data.wireEncode().size(), ndn::MAX_NDN_PACKET_SIZE); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 426 | } |
| 427 | |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 428 | class MaxPacketCustomizationFixture |
| 429 | { |
| 430 | public: |
| 431 | MaxPacketCustomizationFixture() |
| 432 | { |
| 433 | if (getenv("CHRONOSYNC_MAX_PACKET_SIZE") != nullptr) { |
| 434 | oldSize = std::string(getenv("CHRONOSYNC_MAX_PACKET_SIZE")); |
| 435 | unsetenv("CHRONOSYNC_MAX_PACKET_SIZE"); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | ~MaxPacketCustomizationFixture() |
| 440 | { |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 441 | unsetenv("CHRONOSYNC_MAX_PACKET_SIZE"); |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 442 | if (oldSize) { |
| 443 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", oldSize->c_str(), 1); |
| 444 | } |
| 445 | } |
| 446 | private: |
| 447 | ndn::optional<std::string> oldSize; |
| 448 | }; |
| 449 | |
| 450 | BOOST_FIXTURE_TEST_CASE(MaxPacketCustomization, MaxPacketCustomizationFixture) |
| 451 | { |
| 452 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE); |
| 453 | |
| 454 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1500", 1); |
| 455 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), 1500); |
| 456 | |
| 457 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", ndn::to_string(ndn::MAX_NDN_PACKET_SIZE * 100).c_str(), 1); |
| 458 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE); |
| 459 | |
| 460 | setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1", 1); |
| 461 | BOOST_CHECK_EQUAL(getMaxPacketLimit(), 500); |
| 462 | } |
| 463 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 464 | BOOST_AUTO_TEST_SUITE_END() |
| 465 | |
| 466 | } // namespace test |
| 467 | } // namespace chronosync |