blob: 8cb0baf497d08e44384a7852458344db77151dfe [file] [log] [blame]
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento07684bc2021-02-07 20:09:28 -05003 * Copyright (c) 2012-2021 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
Ashlesh Gawande08784d42017-09-06 23:40:21 -050032using ndn::chronosync::DummyForwarder;
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080033
34class Handler
35{
36public:
Ashlesh Gawande08784d42017-09-06 23:40:21 -050037 Handler(ndn::Face& face,
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080038 const Name& syncPrefix,
39 const Name& userPrefix)
40 : logic(face,
41 syncPrefix,
42 userPrefix,
43 bind(&Handler::onUpdate, this, _1))
44 {
45 }
46
47 void
Davide Pesaventofae9def2019-01-29 14:34:33 -050048 onUpdate(const std::vector<MissingDataInfo>& v)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080049 {
50 for (size_t i = 0; i < v.size(); i++) {
51 update(v[i].session, v[i].high, v[i].low);
52 }
53 }
54
55 void
56 update(const Name& p, const SeqNo& high, const SeqNo& low)
57 {
58 map[p] = high;
59 }
60
61 void
62 updateSeqNo(const SeqNo& seqNo)
63 {
64 logic.updateSeqNo(seqNo);
65 }
66
Junxiao Shi0c7f56a2016-07-14 15:27:14 +000067public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080068 Logic logic;
69 std::map<Name, SeqNo> map;
70};
71
Ashlesh Gawande08784d42017-09-06 23:40:21 -050072class LogicFixture : public ndn::tests::IdentityManagementTimeFixture
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080073{
74public:
75 LogicFixture()
76 : syncPrefix("/ndn/broadcast/sync")
Ashlesh Gawande08784d42017-09-06 23:40:21 -050077 , fw(io, m_keyChain)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080078 {
79 syncPrefix.appendVersion();
80 userPrefix[0] = Name("/user0");
81 userPrefix[1] = Name("/user1");
82 userPrefix[2] = Name("/user2");
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080083 userPrefix[3] = Name("/user3");
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080084 }
85
Junxiao Shi0c7f56a2016-07-14 15:27:14 +000086public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080087 Name syncPrefix;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080088 Name userPrefix[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080089
Ashlesh Gawande08784d42017-09-06 23:40:21 -050090 DummyForwarder fw;
91 // std::unique_ptr<DummyClientFace> faces[4];
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080092 shared_ptr<Handler> handler[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080093
Ashlesh Gawande08784d42017-09-06 23:40:21 -050094 // size_t readInterestOffset[4];
95 // size_t readDataOffset[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080096};
97
98BOOST_FIXTURE_TEST_SUITE(LogicTests, LogicFixture)
99
Davide Pesaventofae9def2019-01-29 14:34:33 -0500100static void
101onUpdate(const std::vector<MissingDataInfo>& v)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800102{
103}
104
105BOOST_AUTO_TEST_CASE(Constructor)
106{
107 Name syncPrefix("/ndn/broadcast/sync");
108 Name userPrefix("/user");
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500109 ndn::util::DummyClientFace face(io, {true, true});
110 BOOST_REQUIRE_NO_THROW(Logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)));
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800111}
112
113BOOST_AUTO_TEST_CASE(TwoBasic)
114{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600115 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500116 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800117
Nick Gordon0b3beab2018-03-02 13:03:28 -0600118 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[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(1);
122
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500123 advanceClocks(ndn::time::milliseconds(10), 100);
124
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800125 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500126 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800127
128 handler[0]->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[1]->map[handler[0]->logic.getSessionName()], 2);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500132 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800133
134 handler[1]->updateSeqNo(2);
135
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500136 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800137 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
138}
139
140BOOST_AUTO_TEST_CASE(ThreeBasic)
141{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600142 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500143 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800144
Nick Gordon0b3beab2018-03-02 13:03:28 -0600145 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500146 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800147
Nick Gordon0b3beab2018-03-02 13:03:28 -0600148 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500149 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800150
151 handler[0]->updateSeqNo(1);
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[1]->map[handler[0]->logic.getSessionName()], 1);
155 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
156
157 handler[1]->updateSeqNo(2);
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[1]->logic.getSessionName()], 2);
161 BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2);
162
163 handler[2]->updateSeqNo(4);
164
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500165 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800166 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4);
167 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4);
168}
169
170BOOST_AUTO_TEST_CASE(ResetRecover)
171{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600172 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500173 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800174
Nick Gordon0b3beab2018-03-02 13:03:28 -0600175 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500176 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800177
178 handler[0]->updateSeqNo(1);
179
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500180 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800181 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
182
183 handler[1]->updateSeqNo(2);
184
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500185 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800186 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
187
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500188 advanceClocks(ndn::time::milliseconds(10), 100);
Nick Gordon0b3beab2018-03-02 13:03:28 -0600189 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800190
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500191 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800192 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
193 BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800194
195 handler[2]->updateSeqNo(4);
196
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500197 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800198 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4);
199 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4);
200}
201
202BOOST_AUTO_TEST_CASE(RecoverConflict)
203{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600204 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500205 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800206
Nick Gordon0b3beab2018-03-02 13:03:28 -0600207 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500208 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800209
Nick Gordon0b3beab2018-03-02 13:03:28 -0600210 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500211 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800212
213 handler[0]->updateSeqNo(1);
214
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500215 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800216 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
217 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
218
219 handler[1]->updateSeqNo(2);
220 handler[2]->updateSeqNo(4);
221
Davide Pesavento0d837e32019-03-16 22:31:14 -0400222 advanceClocks(ndn::time::milliseconds(50), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800223 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
224 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4);
225 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4);
226 BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2);
227}
228
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800229BOOST_AUTO_TEST_CASE(PartitionRecover)
230{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600231 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
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[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500235 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800236
Nick Gordon0b3beab2018-03-02 13:03:28 -0600237 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500238 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800239
Nick Gordon0b3beab2018-03-02 13:03:28 -0600240 handler[3] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[3]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500241 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800242
243 handler[0]->updateSeqNo(1);
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[1]->map[handler[0]->logic.getSessionName()], 1);
247 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
248 BOOST_CHECK_EQUAL(handler[3]->map[handler[0]->logic.getSessionName()], 1);
249
250 handler[2]->updateSeqNo(2);
251
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500252 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800253 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 2);
254 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 2);
255 BOOST_CHECK_EQUAL(handler[3]->map[handler[2]->logic.getSessionName()], 2);
256
257 // Network Partition start
258
259 handler[1]->updateSeqNo(3);
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[0]->map[handler[1]->logic.getSessionName()], 3);
263 handler[2]->map[handler[1]->logic.getSessionName()] = 0;
264 handler[3]->map[handler[1]->logic.getSessionName()] = 0;
265
266 handler[3]->updateSeqNo(4);
267
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500268 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800269 BOOST_CHECK_EQUAL(handler[2]->map[handler[3]->logic.getSessionName()], 4);
270 handler[0]->map[handler[3]->logic.getSessionName()] = 0;
271 handler[1]->map[handler[3]->logic.getSessionName()] = 0;
272
273 // Network partition over
274
275 handler[0]->updateSeqNo(5);
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[1]->map[handler[0]->logic.getSessionName()], 5);
279 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 5);
280 BOOST_CHECK_EQUAL(handler[3]->map[handler[0]->logic.getSessionName()], 5);
281
282 handler[2]->updateSeqNo(6);
283
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500284 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800285 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 6);
286 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 6);
287 BOOST_CHECK_EQUAL(handler[3]->map[handler[2]->logic.getSessionName()], 6);
288}
289
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800290BOOST_AUTO_TEST_CASE(MultipleUserUnderOneLogic)
291{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600292 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500293 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800294
Nick Gordon0b3beab2018-03-02 13:03:28 -0600295 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500296 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800297
298 handler[0]->logic.addUserNode(userPrefix[1]);
299
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500300 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800301
302 handler[0]->updateSeqNo(1);
303
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500304 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800305 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
306
307 handler[0]->logic.updateSeqNo(2, userPrefix[1]);
308
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500309 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800310 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName(userPrefix[1])], 2);
311
312 handler[1]->updateSeqNo(4);
313
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500314 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800315 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 4);
316
317 handler[0]->logic.removeUserNode(userPrefix[0]);
318
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500319 advanceClocks(ndn::time::milliseconds(50), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800320 BOOST_CHECK_EQUAL(handler[1]->logic.getSessionNames().size(), 2);
321}
322
Nick Gordon0b3beab2018-03-02 13:03:28 -0600323BOOST_AUTO_TEST_CASE(CancelOutstandingEvents)
324{
325 auto h1 = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
326 advanceClocks(ndn::time::milliseconds(10), 100);
327
328 auto h2 = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
329 advanceClocks(ndn::time::milliseconds(10), 100);
330
331 h1->updateSeqNo(1);
332
333 advanceClocks(ndn::time::milliseconds(10), 100);
334 BOOST_CHECK_EQUAL(h2->map[h1->logic.getSessionName()], 1);
335
336 h2->updateSeqNo(2);
337
338 advanceClocks(ndn::time::milliseconds(10), 100);
339 BOOST_CHECK_EQUAL(h1->map[h2->logic.getSessionName()], 2);
340
341 advanceClocks(ndn::time::milliseconds(10), 100);
342 auto h3 = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
343 // Bringing this handler online later causes recovery interests to
344 // be sent -- h3 has no record of any digests
345
346 advanceClocks(ndn::time::milliseconds(10), 100);
347 BOOST_CHECK_EQUAL(h3->map[h1->logic.getSessionName()], 1);
348 BOOST_CHECK_EQUAL(h3->map[h2->logic.getSessionName()], 2);
349
350 h3->updateSeqNo(4);
351
352 advanceClocks(ndn::time::milliseconds(10), 100);
353 BOOST_CHECK_EQUAL(h2->map[h3->logic.getSessionName()], 4);
354 BOOST_CHECK_EQUAL(h1->map[h3->logic.getSessionName()], 4);
355
356 h1.reset();
357
358 BOOST_CHECK_NE(io.poll(), 0); // some cancel events handlers are expected
359 advanceClocks(ndn::time::minutes(1), 60); // should not crash
360
361 h2.reset();
362 h3.reset();
363
364 BOOST_CHECK_NE(io.poll(), 0); // some cancel events handlers are expected
365 fw.removeFaces();
366 while (io.poll() != 0); // execute all other ready events that may have been scheduled
367
368 steadyClock->advance(ndn::time::hours(1));
369 systemClock->advance(ndn::time::hours(1));
370
371 BOOST_CHECK_EQUAL(io.poll(), 0); // no delayed handlers are expected
372 BOOST_CHECK_EQUAL(io.stopped(), true); // io_service expected to be stopped
373}
374
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500375BOOST_FIXTURE_TEST_CASE(TrimState, ndn::tests::IdentityManagementTimeFixture)
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600376{
377 Name syncPrefix("/ndn/broadcast/sync");
378 Name userPrefix("/user");
379 ndn::util::DummyClientFace face;
380 Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1));
381
382 State state;
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500383 for (size_t i = 0; i != 100; ++i) {
384 state.update(Name("/to/trim").appendNumber(i), 42);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600385 }
386
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500387 State partial;
388 logic.trimState(partial, state, 1);
389 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 99);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600390
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500391 logic.trimState(partial, state, 100);
392 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600393
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500394 logic.trimState(partial, state, 101);
395 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600396
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500397 logic.trimState(partial, state, 42);
398 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 58);
399}
400
401BOOST_FIXTURE_TEST_CASE(VeryLargeState, ndn::tests::IdentityManagementTimeFixture)
402{
403 addIdentity("/bla");
404 Name syncPrefix("/ndn/broadcast/sync");
405 Name userPrefix("/user");
406 ndn::util::DummyClientFace face;
407 Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1));
408
409 State state;
410 for (size_t i = 0; i < 50000 && bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().wire()),
411 state.wireEncode().size())->size() < ndn::MAX_NDN_PACKET_SIZE;
412 i += 10) {
413 Name prefix("/to/trim");
414 prefix.appendNumber(i);
415 for (size_t j = 0; j != 20; ++j) {
416 prefix.appendNumber(ndn::random::generateWord32());
417 }
418 state.update(prefix, ndn::random::generateWord32());
419 }
420 BOOST_TEST_MESSAGE("Got state with " << state.getLeaves().size() << " leaves");
421
422 auto data = logic.encodeSyncReply(userPrefix, "/fake/prefix/of/interest", state);
423 BOOST_CHECK_LE(data.wireEncode().size(), ndn::MAX_NDN_PACKET_SIZE);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600424}
425
Alexander Afanasyev89036292018-02-13 17:19:50 -0500426class MaxPacketCustomizationFixture
427{
428public:
429 MaxPacketCustomizationFixture()
430 {
431 if (getenv("CHRONOSYNC_MAX_PACKET_SIZE") != nullptr) {
432 oldSize = std::string(getenv("CHRONOSYNC_MAX_PACKET_SIZE"));
433 unsetenv("CHRONOSYNC_MAX_PACKET_SIZE");
434 }
435 }
436
437 ~MaxPacketCustomizationFixture()
438 {
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500439 unsetenv("CHRONOSYNC_MAX_PACKET_SIZE");
Alexander Afanasyev89036292018-02-13 17:19:50 -0500440 if (oldSize) {
441 setenv("CHRONOSYNC_MAX_PACKET_SIZE", oldSize->c_str(), 1);
442 }
443 }
Davide Pesaventofae9def2019-01-29 14:34:33 -0500444
Alexander Afanasyev89036292018-02-13 17:19:50 -0500445private:
446 ndn::optional<std::string> oldSize;
447};
448
449BOOST_FIXTURE_TEST_CASE(MaxPacketCustomization, MaxPacketCustomizationFixture)
450{
451 BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE);
452
453 setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1500", 1);
454 BOOST_CHECK_EQUAL(getMaxPacketLimit(), 1500);
455
456 setenv("CHRONOSYNC_MAX_PACKET_SIZE", ndn::to_string(ndn::MAX_NDN_PACKET_SIZE * 100).c_str(), 1);
457 BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE);
458
459 setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1", 1);
460 BOOST_CHECK_EQUAL(getMaxPacketLimit(), 500);
461}
462
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800463BOOST_AUTO_TEST_SUITE_END()
464
465} // namespace test
466} // namespace chronosync