blob: 1a1257a2df30b639a8d1371541c817771d49688c [file] [log] [blame]
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento48dbab62023-08-12 16:06:52 -04003 * Copyright (c) 2012-2023 University of California, Los Angeles
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -08004 *
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 Pesavento07684bc2021-02-07 20:09:28 -050021#include "detail/bzip2-helper.hpp"
Ashlesh Gawande08784d42017-09-06 23:40:21 -050022
Davide Pesaventofae9def2019-01-29 14:34:33 -050023#include "tests/boost-test.hpp"
24#include "tests/identity-management-fixture.hpp"
25#include "tests/unit-tests/dummy-forwarder.hpp"
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080026
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050027#include <ndn-cxx/util/random.hpp>
28
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080029namespace chronosync {
30namespace test {
31
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080032class Handler
33{
34public:
Davide Pesavento5f5101a2022-03-11 20:05:03 -050035 Handler(ndn::Face& face, const Name& syncPrefix, const Name& userPrefix)
36 : logic(face, syncPrefix, userPrefix,
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080037 bind(&Handler::onUpdate, this, _1))
38 {
39 }
40
41 void
Davide Pesaventofae9def2019-01-29 14:34:33 -050042 onUpdate(const std::vector<MissingDataInfo>& v)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080043 {
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 Shi0c7f56a2016-07-14 15:27:14 +000061public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080062 Logic logic;
63 std::map<Name, SeqNo> map;
64};
65
Ashlesh Gawande08784d42017-09-06 23:40:21 -050066class LogicFixture : public ndn::tests::IdentityManagementTimeFixture
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080067{
68public:
69 LogicFixture()
70 : syncPrefix("/ndn/broadcast/sync")
Ashlesh Gawande08784d42017-09-06 23:40:21 -050071 , fw(io, m_keyChain)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080072 {
73 syncPrefix.appendVersion();
74 userPrefix[0] = Name("/user0");
75 userPrefix[1] = Name("/user1");
76 userPrefix[2] = Name("/user2");
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080077 userPrefix[3] = Name("/user3");
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080078 }
79
Junxiao Shi0c7f56a2016-07-14 15:27:14 +000080public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080081 Name syncPrefix;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080082 Name userPrefix[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080083
Davide Pesavento5f5101a2022-03-11 20:05:03 -050084 ndn::chronosync::DummyForwarder fw;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050085 // std::unique_ptr<DummyClientFace> faces[4];
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080086 shared_ptr<Handler> handler[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080087
Ashlesh Gawande08784d42017-09-06 23:40:21 -050088 // size_t readInterestOffset[4];
89 // size_t readDataOffset[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080090};
91
92BOOST_FIXTURE_TEST_SUITE(LogicTests, LogicFixture)
93
Davide Pesaventofae9def2019-01-29 14:34:33 -050094static void
Davide Pesavento5f5101a2022-03-11 20:05:03 -050095onUpdate(const std::vector<MissingDataInfo>&)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080096{
97}
98
99BOOST_AUTO_TEST_CASE(Constructor)
100{
101 Name syncPrefix("/ndn/broadcast/sync");
102 Name userPrefix("/user");
Davide Pesavento48dbab62023-08-12 16:06:52 -0400103 ndn::DummyClientFace face(io, {true, true});
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500104 BOOST_REQUIRE_NO_THROW(Logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)));
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800105}
106
107BOOST_AUTO_TEST_CASE(TwoBasic)
108{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600109 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500110 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800111
Nick Gordon0b3beab2018-03-02 13:03:28 -0600112 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500113 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800114
115 handler[0]->updateSeqNo(1);
116
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500117 advanceClocks(ndn::time::milliseconds(10), 100);
118
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800119 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500120 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800121
122 handler[0]->updateSeqNo(2);
123
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500124 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800125 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 2);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500126 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800127
128 handler[1]->updateSeqNo(2);
129
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500130 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800131 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
132}
133
134BOOST_AUTO_TEST_CASE(ThreeBasic)
135{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600136 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500137 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800138
Nick Gordon0b3beab2018-03-02 13:03:28 -0600139 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500140 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800141
Nick Gordon0b3beab2018-03-02 13:03:28 -0600142 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500143 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800144
145 handler[0]->updateSeqNo(1);
146
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500147 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800148 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 Gawande08784d42017-09-06 23:40:21 -0500153 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800154 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 Gawande08784d42017-09-06 23:40:21 -0500159 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800160 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
164BOOST_AUTO_TEST_CASE(ResetRecover)
165{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600166 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500167 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800168
Nick Gordon0b3beab2018-03-02 13:03:28 -0600169 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500170 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800171
172 handler[0]->updateSeqNo(1);
173
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500174 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800175 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
176
177 handler[1]->updateSeqNo(2);
178
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500179 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800180 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
181
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500182 advanceClocks(ndn::time::milliseconds(10), 100);
Nick Gordon0b3beab2018-03-02 13:03:28 -0600183 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800184
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500185 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800186 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
187 BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800188
189 handler[2]->updateSeqNo(4);
190
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500191 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800192 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
196BOOST_AUTO_TEST_CASE(RecoverConflict)
197{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600198 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500199 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800200
Nick Gordon0b3beab2018-03-02 13:03:28 -0600201 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500202 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800203
Nick Gordon0b3beab2018-03-02 13:03:28 -0600204 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500205 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800206
207 handler[0]->updateSeqNo(1);
208
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500209 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800210 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 Pesavento0d837e32019-03-16 22:31:14 -0400216 advanceClocks(ndn::time::milliseconds(50), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800217 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 Mishra4d3a2e02017-01-18 20:27:51 -0800223BOOST_AUTO_TEST_CASE(PartitionRecover)
224{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600225 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500226 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800227
Nick Gordon0b3beab2018-03-02 13:03:28 -0600228 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500229 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800230
Nick Gordon0b3beab2018-03-02 13:03:28 -0600231 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500232 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800233
Nick Gordon0b3beab2018-03-02 13:03:28 -0600234 handler[3] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[3]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500235 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800236
237 handler[0]->updateSeqNo(1);
238
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500239 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800240 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 Gawande08784d42017-09-06 23:40:21 -0500246 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800247 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 Gawande08784d42017-09-06 23:40:21 -0500255 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800256 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 Gawande08784d42017-09-06 23:40:21 -0500262 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800263 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 Gawande08784d42017-09-06 23:40:21 -0500271 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800272 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 Gawande08784d42017-09-06 23:40:21 -0500278 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800279 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 Dingfb8c9e02015-01-30 14:04:55 -0800284BOOST_AUTO_TEST_CASE(MultipleUserUnderOneLogic)
285{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600286 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500287 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800288
Nick Gordon0b3beab2018-03-02 13:03:28 -0600289 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500290 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800291
292 handler[0]->logic.addUserNode(userPrefix[1]);
293
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500294 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800295
296 handler[0]->updateSeqNo(1);
297
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500298 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800299 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
300
301 handler[0]->logic.updateSeqNo(2, userPrefix[1]);
302
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500303 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800304 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName(userPrefix[1])], 2);
305
306 handler[1]->updateSeqNo(4);
307
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500308 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800309 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 4);
310
311 handler[0]->logic.removeUserNode(userPrefix[0]);
312
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500313 advanceClocks(ndn::time::milliseconds(50), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800314 BOOST_CHECK_EQUAL(handler[1]->logic.getSessionNames().size(), 2);
315}
316
Nick Gordon0b3beab2018-03-02 13:03:28 -0600317BOOST_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 Afanasyev6ee98ff2018-02-13 19:12:28 -0500369BOOST_FIXTURE_TEST_CASE(TrimState, ndn::tests::IdentityManagementTimeFixture)
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600370{
371 Name syncPrefix("/ndn/broadcast/sync");
372 Name userPrefix("/user");
Davide Pesavento48dbab62023-08-12 16:06:52 -0400373 ndn::DummyClientFace face;
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600374 Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1));
375
376 State state;
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500377 for (size_t i = 0; i != 100; ++i) {
378 state.update(Name("/to/trim").appendNumber(i), 42);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600379 }
380
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500381 State partial;
382 logic.trimState(partial, state, 1);
383 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 99);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600384
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500385 logic.trimState(partial, state, 100);
386 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600387
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500388 logic.trimState(partial, state, 101);
389 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600390
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500391 logic.trimState(partial, state, 42);
392 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 58);
393}
394
395BOOST_FIXTURE_TEST_CASE(VeryLargeState, ndn::tests::IdentityManagementTimeFixture)
396{
397 addIdentity("/bla");
398 Name syncPrefix("/ndn/broadcast/sync");
399 Name userPrefix("/user");
Davide Pesavento48dbab62023-08-12 16:06:52 -0400400 ndn::DummyClientFace face;
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500401 Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1));
402
403 State state;
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500404 for (size_t i = 0; i < 50000 && bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().data()),
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500405 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 Gawande4a9ecd52018-02-06 14:36:19 -0600418}
419
Alexander Afanasyev89036292018-02-13 17:19:50 -0500420class MaxPacketCustomizationFixture
421{
422public:
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 Afanasyev6ee98ff2018-02-13 19:12:28 -0500433 unsetenv("CHRONOSYNC_MAX_PACKET_SIZE");
Alexander Afanasyev89036292018-02-13 17:19:50 -0500434 if (oldSize) {
Davide Pesavento8663ed12022-07-23 03:04:27 -0400435 setenv("CHRONOSYNC_MAX_PACKET_SIZE", oldSize->data(), 1);
Alexander Afanasyev89036292018-02-13 17:19:50 -0500436 }
437 }
Davide Pesaventofae9def2019-01-29 14:34:33 -0500438
Alexander Afanasyev89036292018-02-13 17:19:50 -0500439private:
Davide Pesavento8663ed12022-07-23 03:04:27 -0400440 std::optional<std::string> oldSize;
Alexander Afanasyev89036292018-02-13 17:19:50 -0500441};
442
443BOOST_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 Pesavento8663ed12022-07-23 03:04:27 -0400450 setenv("CHRONOSYNC_MAX_PACKET_SIZE", std::to_string(ndn::MAX_NDN_PACKET_SIZE * 100).data(), 1);
Alexander Afanasyev89036292018-02-13 17:19:50 -0500451 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 Dingfb8c9e02015-01-30 14:04:55 -0800457BOOST_AUTO_TEST_SUITE_END()
458
459} // namespace test
460} // namespace chronosync