blob: dafc00b577ec8d46b19a1385219193afb902a972 [file] [log] [blame]
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -06003 * Copyright (c) 2012-2018 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"
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050021#include "bzip2-helper.hpp"
Ashlesh Gawande08784d42017-09-06 23:40:21 -050022
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080023#include "boost-test.hpp"
Ashlesh Gawande08784d42017-09-06 23:40:21 -050024#include "../identity-management-fixture.hpp"
25
26#include "dummy-forwarder.hpp"
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080027
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050028#include <ndn-cxx/util/random.hpp>
29
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080030namespace chronosync {
31namespace test {
32
33using std::vector;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050034using ndn::chronosync::DummyForwarder;
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080035
36class Handler
37{
38public:
Ashlesh Gawande08784d42017-09-06 23:40:21 -050039 Handler(ndn::Face& face,
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080040 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 Shi0c7f56a2016-07-14 15:27:14 +000069public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080070 Logic logic;
71 std::map<Name, SeqNo> map;
72};
73
Ashlesh Gawande08784d42017-09-06 23:40:21 -050074class LogicFixture : public ndn::tests::IdentityManagementTimeFixture
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080075{
76public:
77 LogicFixture()
78 : syncPrefix("/ndn/broadcast/sync")
Ashlesh Gawande08784d42017-09-06 23:40:21 -050079 , fw(io, m_keyChain)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080080 {
81 syncPrefix.appendVersion();
82 userPrefix[0] = Name("/user0");
83 userPrefix[1] = Name("/user1");
84 userPrefix[2] = Name("/user2");
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080085 userPrefix[3] = Name("/user3");
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080086 }
87
Junxiao Shi0c7f56a2016-07-14 15:27:14 +000088public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080089 Name syncPrefix;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080090 Name userPrefix[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080091
Ashlesh Gawande08784d42017-09-06 23:40:21 -050092 DummyForwarder fw;
93 // std::unique_ptr<DummyClientFace> faces[4];
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080094 shared_ptr<Handler> handler[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080095
Ashlesh Gawande08784d42017-09-06 23:40:21 -050096 // size_t readInterestOffset[4];
97 // size_t readDataOffset[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080098};
99
100BOOST_FIXTURE_TEST_SUITE(LogicTests, LogicFixture)
101
102void
103onUpdate(const vector<MissingDataInfo>& v)
104{
105}
106
107BOOST_AUTO_TEST_CASE(Constructor)
108{
109 Name syncPrefix("/ndn/broadcast/sync");
110 Name userPrefix("/user");
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500111 ndn::util::DummyClientFace face(io, {true, true});
112 BOOST_REQUIRE_NO_THROW(Logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)));
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800113}
114
115BOOST_AUTO_TEST_CASE(TwoBasic)
116{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600117 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500118 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800119
Nick Gordon0b3beab2018-03-02 13:03:28 -0600120 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500121 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800122
123 handler[0]->updateSeqNo(1);
124
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500125 advanceClocks(ndn::time::milliseconds(10), 100);
126
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800127 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500128 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800129
130 handler[0]->updateSeqNo(2);
131
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500132 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800133 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 2);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500134 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800135
136 handler[1]->updateSeqNo(2);
137
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500138 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800139 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
140}
141
142BOOST_AUTO_TEST_CASE(ThreeBasic)
143{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600144 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500145 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800146
Nick Gordon0b3beab2018-03-02 13:03:28 -0600147 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500148 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800149
Nick Gordon0b3beab2018-03-02 13:03:28 -0600150 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500151 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800152
153 handler[0]->updateSeqNo(1);
154
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500155 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800156 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 Gawande08784d42017-09-06 23:40:21 -0500161 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800162 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 Gawande08784d42017-09-06 23:40:21 -0500167 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800168 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
172BOOST_AUTO_TEST_CASE(ResetRecover)
173{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600174 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500175 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800176
Nick Gordon0b3beab2018-03-02 13:03:28 -0600177 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500178 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800179
180 handler[0]->updateSeqNo(1);
181
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500182 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800183 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
184
185 handler[1]->updateSeqNo(2);
186
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500187 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800188 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
189
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500190 advanceClocks(ndn::time::milliseconds(10), 100);
Nick Gordon0b3beab2018-03-02 13:03:28 -0600191 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800192
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500193 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800194 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
195 BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800196
197 handler[2]->updateSeqNo(4);
198
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500199 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800200 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
204BOOST_AUTO_TEST_CASE(RecoverConflict)
205{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600206 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500207 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800208
Nick Gordon0b3beab2018-03-02 13:03:28 -0600209 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500210 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800211
Nick Gordon0b3beab2018-03-02 13:03:28 -0600212 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500213 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800214
215 handler[0]->updateSeqNo(1);
216
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500217 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800218 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 Gawande08784d42017-09-06 23:40:21 -0500224 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800225 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 Mishra4d3a2e02017-01-18 20:27:51 -0800231BOOST_AUTO_TEST_CASE(PartitionRecover)
232{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600233 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500234 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800235
Nick Gordon0b3beab2018-03-02 13:03:28 -0600236 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[1]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500237 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800238
Nick Gordon0b3beab2018-03-02 13:03:28 -0600239 handler[2] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500240 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800241
Nick Gordon0b3beab2018-03-02 13:03:28 -0600242 handler[3] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[3]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500243 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800244
245 handler[0]->updateSeqNo(1);
246
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500247 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800248 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 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[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 Gawande08784d42017-09-06 23:40:21 -0500263 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800264 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 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[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 Gawande08784d42017-09-06 23:40:21 -0500279 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800280 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 Gawande08784d42017-09-06 23:40:21 -0500286 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800287 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 Dingfb8c9e02015-01-30 14:04:55 -0800292BOOST_AUTO_TEST_CASE(MultipleUserUnderOneLogic)
293{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600294 handler[0] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[0]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500295 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800296
Nick Gordon0b3beab2018-03-02 13:03:28 -0600297 handler[1] = make_shared<Handler>(fw.addFace(), syncPrefix, userPrefix[2]);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500298 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800299
300 handler[0]->logic.addUserNode(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
304 handler[0]->updateSeqNo(1);
305
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500306 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800307 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
308
309 handler[0]->logic.updateSeqNo(2, userPrefix[1]);
310
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500311 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800312 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName(userPrefix[1])], 2);
313
314 handler[1]->updateSeqNo(4);
315
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500316 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800317 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 4);
318
319 handler[0]->logic.removeUserNode(userPrefix[0]);
320
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500321 advanceClocks(ndn::time::milliseconds(50), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800322 BOOST_CHECK_EQUAL(handler[1]->logic.getSessionNames().size(), 2);
323}
324
Nick Gordon0b3beab2018-03-02 13:03:28 -0600325BOOST_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 Afanasyev6ee98ff2018-02-13 19:12:28 -0500377BOOST_FIXTURE_TEST_CASE(TrimState, ndn::tests::IdentityManagementTimeFixture)
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600378{
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 Afanasyev6ee98ff2018-02-13 19:12:28 -0500385 for (size_t i = 0; i != 100; ++i) {
386 state.update(Name("/to/trim").appendNumber(i), 42);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600387 }
388
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500389 State partial;
390 logic.trimState(partial, state, 1);
391 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 99);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600392
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500393 logic.trimState(partial, state, 100);
394 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600395
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500396 logic.trimState(partial, state, 101);
397 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600398
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500399 logic.trimState(partial, state, 42);
400 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 58);
401}
402
403BOOST_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 Gawande4a9ecd52018-02-06 14:36:19 -0600426}
427
Alexander Afanasyev89036292018-02-13 17:19:50 -0500428class MaxPacketCustomizationFixture
429{
430public:
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 Afanasyev6ee98ff2018-02-13 19:12:28 -0500441 unsetenv("CHRONOSYNC_MAX_PACKET_SIZE");
Alexander Afanasyev89036292018-02-13 17:19:50 -0500442 if (oldSize) {
443 setenv("CHRONOSYNC_MAX_PACKET_SIZE", oldSize->c_str(), 1);
444 }
445 }
446private:
447 ndn::optional<std::string> oldSize;
448};
449
450BOOST_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 Dingfb8c9e02015-01-30 14:04:55 -0800464BOOST_AUTO_TEST_SUITE_END()
465
466} // namespace test
467} // namespace chronosync