blob: 3fd0d2600d8ec42e7a35dd4a9d4da1b72c761d06 [file] [log] [blame]
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -05003 * Copyright (c) 2012-2017 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 "socket.hpp"
Ashlesh Gawande08784d42017-09-06 23:40:21 -050021
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080022#include "boost-test.hpp"
Ashlesh Gawande08784d42017-09-06 23:40:21 -050023#include "../unit-test-time-fixture.hpp"
24
25#include <ndn-cxx/util/dummy-client-face.hpp>
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080026
27namespace chronosync {
28namespace test {
29
30using std::string;
31using std::vector;
32using std::map;
33using ndn::util::DummyClientFace;
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080034
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080035/**
36 * @brief Emulate an app that use the Socket class
37 *
38 * The app has two types of data set: one is simply string while the other is integer array.
39 * For each type of data set, the app has a specific fetching strategy.
40 */
41class SocketTestApp : noncopyable
42{
43public:
44 SocketTestApp(const Name& syncPrefix,
45 const Name& userPrefix,
46 DummyClientFace& face,
47 bool isNum)
48 : sum(0)
49 , socket(syncPrefix,
50 userPrefix,
51 face,
52 isNum ? bind(&SocketTestApp::fetchNumbers, this, _1) :
53 bind(&SocketTestApp::fetchAll, this, _1))
54 {
55 }
56
57 void
Ashlesh Gawande08784d42017-09-06 23:40:21 -050058 set(const Data& dataPacket)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080059 {
60 // std::cerr << "set Data" << std::endl;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050061 Name dataName(dataPacket.getName());
62 string str2(reinterpret_cast<const char*>(dataPacket.getContent().value()),
63 dataPacket.getContent().value_size());
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080064 data.insert(make_pair(dataName, str2));
65 }
66
67 void
68 set(Name name, const char* buf, int len)
69 {
70 string str2(buf, len);
71 data.insert(make_pair(name, str2));
72 }
73
74 void
Ashlesh Gawande08784d42017-09-06 23:40:21 -050075 setNum(const Data& dataPacket)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080076 {
77 // std::cerr << "setNum Data" << std::endl;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050078 size_t n = dataPacket.getContent().value_size() / 4;
79 const uint32_t* numbers = reinterpret_cast<const uint32_t*>(dataPacket.getContent().value());
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080080 for (size_t i = 0; i < n; i++) {
81 sum += numbers[i];
82 }
83 }
84
85 void
86 setNum(Name name, const uint8_t* buf, int len)
87 {
88 BOOST_ASSERT(len >= 4);
89
90 int n = len / 4;
91 const uint32_t* numbers = reinterpret_cast<const uint32_t*>(buf);
92 for (int i = 0; i < n; i++) {
93 sum += numbers[i];
94 }
95 }
96
97 void
98 fetchAll(const vector<MissingDataInfo>& v)
99 {
100 // std::cerr << "fetchAll" << std::endl;
Yingdi Yu372697f2015-02-09 14:42:39 -0800101 for (size_t i = 0; i < v.size(); i++) {
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800102 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500103 socket.fetchData(v[i].session, s, [this] (const Data& dataPacket) {
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800104 this->set(dataPacket);
105 });
106 }
107 }
108 }
109
110 void
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500111 fetchNumbers(const vector<MissingDataInfo>& v)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800112 {
113 // std::cerr << "fetchNumbers" << std::endl;
Yingdi Yu372697f2015-02-09 14:42:39 -0800114 for (size_t i = 0; i < v.size(); i++) {
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800115 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500116 socket.fetchData(v[i].session, s, [this] (const Data& dataPacket) {
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800117 this->setNum(dataPacket);
118 });
119 }
120 }
121 }
122
123 string
124 toString()
125 {
126 string str = "\n";
127 for (map<Name, string>::iterator it = data.begin(); it != data.end(); ++it) {
128 str += "<";
129 str += it->first.toUri();
130 str += "|";
131 str += it->second;
132 str += ">";
133 str += "\n";
134 }
135
136 return str;
137 }
138
Junxiao Shi0c7f56a2016-07-14 15:27:14 +0000139public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800140 map<ndn::Name, string> data;
141 uint32_t sum;
142 Socket socket;
143};
144
145class SocketFixture : public ndn::tests::UnitTestTimeFixture
146{
147public:
148 SocketFixture()
149 : syncPrefix("/ndn/broadcast/sync")
150 {
151 syncPrefix.appendVersion();
152 userPrefix[0] = Name("/user0");
153 userPrefix[1] = Name("/user1");
154 userPrefix[2] = Name("/user2");
155
Junxiao Shi0c7f56a2016-07-14 15:27:14 +0000156 faces[0].reset(new DummyClientFace(io, {true, true}));
157 faces[1].reset(new DummyClientFace(io, {true, true}));
158 faces[2].reset(new DummyClientFace(io, {true, true}));
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800159
160 for (int i = 0; i < 3; i++) {
161 readInterestOffset[i] = 0;
162 readDataOffset[i] = 0;
163 }
164 }
165
166 void
167 passPacket()
168 {
169 for (int i = 0; i < 3; i++)
170 checkFace(i);
171 }
172
173 void
174 checkFace(int sender)
175 {
176 while (faces[sender]->sentInterests.size() > readInterestOffset[sender]) {
177 for (int i = 0; i < 3; i++) {
178 if (sender != i)
179 faces[i]->receive(faces[sender]->sentInterests[readInterestOffset[sender]]);
180 }
181 readInterestOffset[sender]++;
182 }
Junxiao Shi0c7f56a2016-07-14 15:27:14 +0000183 while (faces[sender]->sentData.size() > readDataOffset[sender]) {
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800184 for (int i = 0; i < 3; i++) {
185 if (sender != i)
Junxiao Shi0c7f56a2016-07-14 15:27:14 +0000186 faces[i]->receive(faces[sender]->sentData[readDataOffset[sender]]);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800187 }
188 readDataOffset[sender]++;
189 }
190 }
191
192 void
193 createSocket(size_t idx, bool isNum)
194 {
195 app[idx] = make_shared<SocketTestApp>(syncPrefix, userPrefix[idx], ref(*faces[idx]), isNum);
196 sessionName[idx] = app[idx]->socket.getLogic().getSessionName();
197 }
198
199 void
200 publishAppData(size_t idx, const string& data)
201 {
202 app[idx]->socket.publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(),
203 ndn::time::milliseconds(1000));
204 }
205
206 void
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500207 publishAppData(size_t idx, const string& data, SeqNo seqNo)
208 {
209 app[idx]->socket.publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(),
210 ndn::time::milliseconds(1000), seqNo);
211 }
212
213 void
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800214 setAppData(size_t idx, SeqNo seqNo, const string& data)
215 {
216 Name dataName = sessionName[idx];
217 dataName.appendNumber(seqNo);
218 app[idx]->set(dataName, data.c_str(), data.size());
219 }
220
221 void
222 publishAppNum(size_t idx, const uint8_t* buf, size_t size)
223 {
224 app[idx]->socket.publishData(buf, size, ndn::time::milliseconds(1000));
225 }
226
227 void
228 setAppNum(size_t idx, SeqNo seqNo, const uint8_t* buf, size_t size)
229 {
230 Name dataName = sessionName[idx];
231 dataName.appendNumber(seqNo);
232 app[idx]->setNum(dataName, buf, size);
233 }
234
Junxiao Shi0c7f56a2016-07-14 15:27:14 +0000235public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800236 Name syncPrefix;
237 Name userPrefix[3];
238 Name sessionName[3];
239
Junxiao Shi0c7f56a2016-07-14 15:27:14 +0000240 std::unique_ptr<DummyClientFace> faces[3];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800241 shared_ptr<SocketTestApp> app[3];
242
243 size_t readInterestOffset[3];
244 size_t readDataOffset[3];
245};
246
247
248
249BOOST_FIXTURE_TEST_SUITE(SocketTests, SocketFixture)
250
251BOOST_AUTO_TEST_CASE(BasicData)
252{
253 createSocket(0, false);
254 advanceClocks(ndn::time::milliseconds(10), 5);
255 createSocket(1, false);
256 advanceClocks(ndn::time::milliseconds(10), 5);
257 createSocket(2, false);
258 advanceClocks(ndn::time::milliseconds(10), 5);
259
260 string data0 = "Very funny Scotty, now beam down my clothes";
261 publishAppData(0, data0);
262
263 for (int i = 0; i < 50; i++) {
264 advanceClocks(ndn::time::milliseconds(2), 10);
265 passPacket();
266 }
267 setAppData(0, 1, data0);
268
269 advanceClocks(ndn::time::milliseconds(10), 1);
270 BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
271 BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
272
273 string data1 = "Yes, give me that ketchup";
274 string data2 = "Don't look conspicuous, it draws fire";
275 publishAppData(0, data1);
276 advanceClocks(ndn::time::milliseconds(10), 1);
277 publishAppData(0, data2);
278
279 for (int i = 0; i < 50; i++) {
280 advanceClocks(ndn::time::milliseconds(2), 10);
281 passPacket();
282 }
283 setAppData(0, 2, data1);
284 advanceClocks(ndn::time::milliseconds(10), 1);
285 setAppData(0, 3, data2);
286
287 advanceClocks(ndn::time::milliseconds(10), 1);
288 BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
289 BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
290
291 string data3 = "You surf the Internet, I surf the real world";
292 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
293 string data5 = "Real men wear pink. Why? Because their wives make them";
294 publishAppData(2, data3);
295 advanceClocks(ndn::time::milliseconds(10), 2);
296 publishAppData(1, data4);
297 advanceClocks(ndn::time::milliseconds(10), 1);
298 publishAppData(1, data5);
299
300 for (int i = 0; i < 100; i++) {
301 advanceClocks(ndn::time::milliseconds(2), 10);
302 passPacket();
303 }
304 setAppData(2, 1, data3);
305 advanceClocks(ndn::time::milliseconds(10), 1);
306 setAppData(1, 1, data4);
307 advanceClocks(ndn::time::milliseconds(10), 1);
308 setAppData(1, 2, data5);
309
310 advanceClocks(ndn::time::milliseconds(10), 7);
311 BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
312 BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
313
314 string data6 = "Shakespeare says: 'Prose before hos.'";
315 string data7 = "Pick good people, talent never wears out";
316 publishAppData(0, data6);
317 publishAppData(1, data7);
318
319 for (int i = 0; i < 100; i++) {
320 advanceClocks(ndn::time::milliseconds(2), 10);
321 passPacket();
322 }
323 setAppData(0, 4, data6);
324 setAppData(1, 3, data7);
325
326 BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
327 BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
328}
329
330BOOST_AUTO_TEST_CASE(BasicNumber)
331{
332 createSocket(0, true);
333 advanceClocks(ndn::time::milliseconds(10), 5);
334 createSocket(1, true);
335 advanceClocks(ndn::time::milliseconds(10), 5);
336
337 uint32_t num1[5] = {0, 1, 2, 3, 4};
338 uint8_t* buf1 = reinterpret_cast<uint8_t*>(num1);
339 size_t size1 = sizeof(num1);
340 publishAppNum(0, buf1, size1);
341 advanceClocks(ndn::time::milliseconds(10), 5);
342 setAppNum(0, 0, buf1, size1);
343
344 for (int i = 0; i < 100; i++) {
345 advanceClocks(ndn::time::milliseconds(2), 10);
346 passPacket();
347 }
348 BOOST_CHECK_EQUAL(app[0]->sum, app[1]->sum);
349 BOOST_CHECK_EQUAL(app[1]->sum, 10);
350
351 uint32_t num2[5] = {9, 7, 2, 1, 1};
352 uint8_t* buf2 = reinterpret_cast<uint8_t*>(num2);
353 size_t size2 = sizeof(num2);
354 publishAppNum(1, buf2, size2);
355 setAppNum(1, 0, buf2, size2);
356
357 for (int i = 0; i < 50; i++) {
358 advanceClocks(ndn::time::milliseconds(2), 10);
359 passPacket();
360 }
361 BOOST_CHECK_EQUAL(app[0]->sum, app[1]->sum);
362 BOOST_CHECK_EQUAL(app[1]->sum, 30);
363}
364
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500365BOOST_AUTO_TEST_CASE(BasicDataWithAppSeq)
366{
367 createSocket(0, false);
368 advanceClocks(ndn::time::milliseconds(10), 5);
369 createSocket(1, false);
370 advanceClocks(ndn::time::milliseconds(10), 5);
371 createSocket(2, false);
372 advanceClocks(ndn::time::milliseconds(10), 5);
373
374 string data0 = "Very funny Scotty, now beam down my clothes";
375 publishAppData(0, data0, 100);
376
377 for (int i = 0; i < 50; i++) {
378 advanceClocks(ndn::time::milliseconds(2), 10);
379 passPacket();
380 }
381 setAppData(0, 100, data0);
382
383 advanceClocks(ndn::time::milliseconds(10), 1);
384 BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
385 BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
386
387 string data1 = "Yes, give me that ketchup";
388 string data2 = "Don't look conspicuous, it draws fire";
389 publishAppData(0, data1, 200);
390 advanceClocks(ndn::time::milliseconds(10), 1);
391 publishAppData(0, data2, 300);
392
393 for (int i = 0; i < 50; i++) {
394 advanceClocks(ndn::time::milliseconds(2), 10);
395 passPacket();
396 }
397 setAppData(0, 200, data1);
398 advanceClocks(ndn::time::milliseconds(10), 1);
399 setAppData(0, 300, data2);
400
401 advanceClocks(ndn::time::milliseconds(10), 1);
402 BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
403 BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
404
405 string data3 = "You surf the Internet, I surf the real world";
406 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
407 string data5 = "Real men wear pink. Why? Because their wives make them";
408 publishAppData(2, data3, 100);
409 advanceClocks(ndn::time::milliseconds(10), 2);
410 publishAppData(1, data4, 100);
411 advanceClocks(ndn::time::milliseconds(10), 1);
412 publishAppData(1, data5, 200);
413
414 for (int i = 0; i < 100; i++) {
415 advanceClocks(ndn::time::milliseconds(2), 10);
416 passPacket();
417 }
418 setAppData(2, 100, data3);
419 advanceClocks(ndn::time::milliseconds(10), 1);
420 setAppData(1, 100, data4);
421 advanceClocks(ndn::time::milliseconds(10), 1);
422 setAppData(1, 200, data5);
423
424 advanceClocks(ndn::time::milliseconds(10), 7);
425 BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
426 BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
427
428 string data6 = "Shakespeare says: 'Prose before hos.'";
429 string data7 = "Pick good people, talent never wears out";
430 publishAppData(0, data6, 500);
431 publishAppData(1, data7, 300);
432
433 for (int i = 0; i < 100; i++) {
434 advanceClocks(ndn::time::milliseconds(2), 10);
435 passPacket();
436 }
437 setAppData(0, 500, data6);
438 setAppData(1, 300, data7);
439
440 BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
441 BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
442}
443
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800444BOOST_AUTO_TEST_SUITE_END()
445
446} // namespace test
447} // namespace chronosync