blob: 665a9d1b9ab012e7760fd2a8dcbf66da27d5eae8 [file] [log] [blame]
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento30c41ec2024-02-12 17:36:35 -05003 * Copyright (c) 2012-2024 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
Davide Pesavento30c41ec2024-02-12 17:36:35 -050029namespace chronosync::tests {
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080030
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080031class Handler
32{
33public:
Davide Pesavento5f5101a2022-03-11 20:05:03 -050034 Handler(ndn::Face& face, const Name& syncPrefix, const Name& userPrefix)
35 : logic(face, syncPrefix, userPrefix,
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080036 bind(&Handler::onUpdate, this, _1))
37 {
38 }
39
40 void
Davide Pesaventofae9def2019-01-29 14:34:33 -050041 onUpdate(const std::vector<MissingDataInfo>& v)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080042 {
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 Shi0c7f56a2016-07-14 15:27:14 +000060public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080061 Logic logic;
62 std::map<Name, SeqNo> map;
63};
64
Ashlesh Gawande08784d42017-09-06 23:40:21 -050065class LogicFixture : public ndn::tests::IdentityManagementTimeFixture
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080066{
67public:
68 LogicFixture()
69 : syncPrefix("/ndn/broadcast/sync")
Ashlesh Gawande08784d42017-09-06 23:40:21 -050070 , fw(io, m_keyChain)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080071 {
72 syncPrefix.appendVersion();
73 userPrefix[0] = Name("/user0");
74 userPrefix[1] = Name("/user1");
75 userPrefix[2] = Name("/user2");
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080076 userPrefix[3] = Name("/user3");
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080077 }
78
Junxiao Shi0c7f56a2016-07-14 15:27:14 +000079public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080080 Name syncPrefix;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080081 Name userPrefix[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080082
Davide Pesavento30c41ec2024-02-12 17:36:35 -050083 DummyForwarder fw;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050084 // std::unique_ptr<DummyClientFace> faces[4];
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080085 shared_ptr<Handler> handler[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080086
Ashlesh Gawande08784d42017-09-06 23:40:21 -050087 // size_t readInterestOffset[4];
88 // size_t readDataOffset[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080089};
90
91BOOST_FIXTURE_TEST_SUITE(LogicTests, LogicFixture)
92
Davide Pesaventofae9def2019-01-29 14:34:33 -050093static void
Davide Pesavento5f5101a2022-03-11 20:05:03 -050094onUpdate(const std::vector<MissingDataInfo>&)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080095{
96}
97
98BOOST_AUTO_TEST_CASE(Constructor)
99{
100 Name syncPrefix("/ndn/broadcast/sync");
101 Name userPrefix("/user");
Davide Pesavento48dbab62023-08-12 16:06:52 -0400102 ndn::DummyClientFace face(io, {true, true});
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500103 BOOST_REQUIRE_NO_THROW(Logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)));
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800104}
105
106BOOST_AUTO_TEST_CASE(TwoBasic)
107{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600108 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500109 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800110
Nick Gordon0b3beab2018-03-02 13:03:28 -0600111 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500112 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800113
114 handler[0]->updateSeqNo(1);
115
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500116 advanceClocks(ndn::time::milliseconds(10), 100);
117
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800118 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500119 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800120
121 handler[0]->updateSeqNo(2);
122
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500123 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800124 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 2);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500125 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800126
127 handler[1]->updateSeqNo(2);
128
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500129 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800130 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
131}
132
133BOOST_AUTO_TEST_CASE(ThreeBasic)
134{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600135 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500136 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800137
Nick Gordon0b3beab2018-03-02 13:03:28 -0600138 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500139 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800140
Nick Gordon0b3beab2018-03-02 13:03:28 -0600141 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500142 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800143
144 handler[0]->updateSeqNo(1);
145
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500146 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800147 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 Gawande08784d42017-09-06 23:40:21 -0500152 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800153 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 Gawande08784d42017-09-06 23:40:21 -0500158 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800159 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
163BOOST_AUTO_TEST_CASE(ResetRecover)
164{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600165 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500166 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800167
Nick Gordon0b3beab2018-03-02 13:03:28 -0600168 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500169 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800170
171 handler[0]->updateSeqNo(1);
172
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500173 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800174 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
175
176 handler[1]->updateSeqNo(2);
177
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500178 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800179 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
180
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500181 advanceClocks(ndn::time::milliseconds(10), 100);
Nick Gordon0b3beab2018-03-02 13:03:28 -0600182 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800183
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500184 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800185 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
186 BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800187
188 handler[2]->updateSeqNo(4);
189
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500190 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800191 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
195BOOST_AUTO_TEST_CASE(RecoverConflict)
196{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600197 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500198 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800199
Nick Gordon0b3beab2018-03-02 13:03:28 -0600200 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500201 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800202
Nick Gordon0b3beab2018-03-02 13:03:28 -0600203 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500204 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800205
206 handler[0]->updateSeqNo(1);
207
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500208 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800209 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 Pesavento0d837e32019-03-16 22:31:14 -0400215 advanceClocks(ndn::time::milliseconds(50), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800216 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 Mishra4d3a2e02017-01-18 20:27:51 -0800222BOOST_AUTO_TEST_CASE(PartitionRecover)
223{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600224 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500225 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800226
Nick Gordon0b3beab2018-03-02 13:03:28 -0600227 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500228 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800229
Nick Gordon0b3beab2018-03-02 13:03:28 -0600230 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500231 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800232
Nick Gordon0b3beab2018-03-02 13:03:28 -0600233 handler[3] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[3]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500234 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800235
236 handler[0]->updateSeqNo(1);
237
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500238 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800239 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 Gawande08784d42017-09-06 23:40:21 -0500245 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800246 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 Gawande08784d42017-09-06 23:40:21 -0500254 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800255 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 Gawande08784d42017-09-06 23:40:21 -0500261 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800262 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 Gawande08784d42017-09-06 23:40:21 -0500270 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800271 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 Gawande08784d42017-09-06 23:40:21 -0500277 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800278 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 Dingfb8c9e02015-01-30 14:04:55 -0800283BOOST_AUTO_TEST_CASE(MultipleUserUnderOneLogic)
284{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600285 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500286 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800287
Nick Gordon0b3beab2018-03-02 13:03:28 -0600288 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500289 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800290
291 handler[0]->logic.addUserNode(userPrefix[1]);
292
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500293 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800294
295 handler[0]->updateSeqNo(1);
296
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500297 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800298 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
299
300 handler[0]->logic.updateSeqNo(2, userPrefix[1]);
301
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500302 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800303 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName(userPrefix[1])], 2);
304
305 handler[1]->updateSeqNo(4);
306
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500307 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800308 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 4);
309
310 handler[0]->logic.removeUserNode(userPrefix[0]);
311
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500312 advanceClocks(ndn::time::milliseconds(50), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800313 BOOST_CHECK_EQUAL(handler[1]->logic.getSessionNames().size(), 2);
314}
315
Nick Gordon0b3beab2018-03-02 13:03:28 -0600316BOOST_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 Pesavento1af79492023-09-22 15:45:21 -0400365 BOOST_CHECK_EQUAL(io.stopped(), true); // io_context expected to be stopped
Nick Gordon0b3beab2018-03-02 13:03:28 -0600366}
367
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500368BOOST_FIXTURE_TEST_CASE(TrimState, ndn::tests::IdentityManagementTimeFixture)
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600369{
370 Name syncPrefix("/ndn/broadcast/sync");
371 Name userPrefix("/user");
Davide Pesavento48dbab62023-08-12 16:06:52 -0400372 ndn::DummyClientFace face;
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600373 Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1));
374
375 State state;
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500376 for (size_t i = 0; i != 100; ++i) {
377 state.update(Name("/to/trim").appendNumber(i), 42);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600378 }
379
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500380 State partial;
381 logic.trimState(partial, state, 1);
382 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 99);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600383
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500384 logic.trimState(partial, state, 100);
385 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600386
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500387 logic.trimState(partial, state, 101);
388 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600389
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500390 logic.trimState(partial, state, 42);
391 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 58);
392}
393
394BOOST_FIXTURE_TEST_CASE(VeryLargeState, ndn::tests::IdentityManagementTimeFixture)
395{
396 addIdentity("/bla");
397 Name syncPrefix("/ndn/broadcast/sync");
398 Name userPrefix("/user");
Davide Pesavento48dbab62023-08-12 16:06:52 -0400399 ndn::DummyClientFace face;
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500400 Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1));
401
402 State state;
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500403 for (size_t i = 0; i < 50000 && bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().data()),
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500404 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 }
413 BOOST_TEST_MESSAGE("Got state with " << state.getLeaves().size() << " leaves");
414
415 auto data = logic.encodeSyncReply(userPrefix, "/fake/prefix/of/interest", state);
416 BOOST_CHECK_LE(data.wireEncode().size(), ndn::MAX_NDN_PACKET_SIZE);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600417}
418
Alexander Afanasyev89036292018-02-13 17:19:50 -0500419class MaxPacketCustomizationFixture
420{
421public:
422 MaxPacketCustomizationFixture()
423 {
424 if (getenv("CHRONOSYNC_MAX_PACKET_SIZE") != nullptr) {
425 oldSize = std::string(getenv("CHRONOSYNC_MAX_PACKET_SIZE"));
426 unsetenv("CHRONOSYNC_MAX_PACKET_SIZE");
427 }
428 }
429
430 ~MaxPacketCustomizationFixture()
431 {
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500432 unsetenv("CHRONOSYNC_MAX_PACKET_SIZE");
Alexander Afanasyev89036292018-02-13 17:19:50 -0500433 if (oldSize) {
Davide Pesavento8663ed12022-07-23 03:04:27 -0400434 setenv("CHRONOSYNC_MAX_PACKET_SIZE", oldSize->data(), 1);
Alexander Afanasyev89036292018-02-13 17:19:50 -0500435 }
436 }
Davide Pesaventofae9def2019-01-29 14:34:33 -0500437
Alexander Afanasyev89036292018-02-13 17:19:50 -0500438private:
Davide Pesavento8663ed12022-07-23 03:04:27 -0400439 std::optional<std::string> oldSize;
Alexander Afanasyev89036292018-02-13 17:19:50 -0500440};
441
442BOOST_FIXTURE_TEST_CASE(MaxPacketCustomization, MaxPacketCustomizationFixture)
443{
444 BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE);
445
446 setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1500", 1);
447 BOOST_CHECK_EQUAL(getMaxPacketLimit(), 1500);
448
Davide Pesavento8663ed12022-07-23 03:04:27 -0400449 setenv("CHRONOSYNC_MAX_PACKET_SIZE", std::to_string(ndn::MAX_NDN_PACKET_SIZE * 100).data(), 1);
Alexander Afanasyev89036292018-02-13 17:19:50 -0500450 BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE);
451
452 setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1", 1);
453 BOOST_CHECK_EQUAL(getMaxPacketLimit(), 500);
454}
455
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800456BOOST_AUTO_TEST_SUITE_END()
457
Davide Pesavento30c41ec2024-02-12 17:36:35 -0500458} // namespace chronosync::tests