Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2012-2016 University of California, Los Angeles |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 4 | * |
| 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" |
| 21 | #include "../unit-test-time-fixture.hpp" |
| 22 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 23 | #include "boost-test.hpp" |
| 24 | |
| 25 | namespace chronosync { |
| 26 | namespace test { |
| 27 | |
| 28 | using std::string; |
| 29 | using std::vector; |
| 30 | using std::map; |
| 31 | using ndn::util::DummyClientFace; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 32 | |
| 33 | |
| 34 | /** |
| 35 | * @brief Emulate an app that use the Socket class |
| 36 | * |
| 37 | * The app has two types of data set: one is simply string while the other is integer array. |
| 38 | * For each type of data set, the app has a specific fetching strategy. |
| 39 | */ |
| 40 | class SocketTestApp : noncopyable |
| 41 | { |
| 42 | public: |
| 43 | SocketTestApp(const Name& syncPrefix, |
| 44 | const Name& userPrefix, |
| 45 | DummyClientFace& face, |
| 46 | bool isNum) |
| 47 | : sum(0) |
| 48 | , socket(syncPrefix, |
| 49 | userPrefix, |
| 50 | face, |
| 51 | isNum ? bind(&SocketTestApp::fetchNumbers, this, _1) : |
| 52 | bind(&SocketTestApp::fetchAll, this, _1)) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | set(const shared_ptr<const Data>& dataPacket) |
| 58 | { |
| 59 | // std::cerr << "set Data" << std::endl; |
| 60 | Name dataName(dataPacket->getName()); |
| 61 | string str2(reinterpret_cast<const char*>(dataPacket->getContent().value()), |
| 62 | dataPacket->getContent().value_size()); |
| 63 | data.insert(make_pair(dataName, str2)); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | set(Name name, const char* buf, int len) |
| 68 | { |
| 69 | string str2(buf, len); |
| 70 | data.insert(make_pair(name, str2)); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | setNum(const shared_ptr<const Data>& dataPacket) |
| 75 | { |
| 76 | // std::cerr << "setNum Data" << std::endl; |
| 77 | size_t n = dataPacket->getContent().value_size() / 4; |
| 78 | const uint32_t* numbers = reinterpret_cast<const uint32_t*>(dataPacket->getContent().value()); |
| 79 | for (size_t i = 0; i < n; i++) { |
| 80 | sum += numbers[i]; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | setNum(Name name, const uint8_t* buf, int len) |
| 86 | { |
| 87 | BOOST_ASSERT(len >= 4); |
| 88 | |
| 89 | int n = len / 4; |
| 90 | const uint32_t* numbers = reinterpret_cast<const uint32_t*>(buf); |
| 91 | for (int i = 0; i < n; i++) { |
| 92 | sum += numbers[i]; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | fetchAll(const vector<MissingDataInfo>& v) |
| 98 | { |
| 99 | // std::cerr << "fetchAll" << std::endl; |
Yingdi Yu | 372697f | 2015-02-09 14:42:39 -0800 | [diff] [blame] | 100 | for (size_t i = 0; i < v.size(); i++) { |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 101 | for(SeqNo s = v[i].low; s <= v[i].high; ++s) { |
| 102 | socket.fetchData(v[i].session, s, [this] (const shared_ptr<const Data>& dataPacket) { |
| 103 | this->set(dataPacket); |
| 104 | }); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | fetchNumbers(const vector<MissingDataInfo> &v) |
| 111 | { |
| 112 | // std::cerr << "fetchNumbers" << std::endl; |
Yingdi Yu | 372697f | 2015-02-09 14:42:39 -0800 | [diff] [blame] | 113 | for (size_t i = 0; i < v.size(); i++) { |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 114 | for(SeqNo s = v[i].low; s <= v[i].high; ++s) { |
| 115 | socket.fetchData(v[i].session, s, [this] (const shared_ptr<const Data>& dataPacket) { |
| 116 | this->setNum(dataPacket); |
| 117 | }); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | string |
| 123 | toString() |
| 124 | { |
| 125 | string str = "\n"; |
| 126 | for (map<Name, string>::iterator it = data.begin(); it != data.end(); ++it) { |
| 127 | str += "<"; |
| 128 | str += it->first.toUri(); |
| 129 | str += "|"; |
| 130 | str += it->second; |
| 131 | str += ">"; |
| 132 | str += "\n"; |
| 133 | } |
| 134 | |
| 135 | return str; |
| 136 | } |
| 137 | |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame^] | 138 | public: |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 139 | map<ndn::Name, string> data; |
| 140 | uint32_t sum; |
| 141 | Socket socket; |
| 142 | }; |
| 143 | |
| 144 | class SocketFixture : public ndn::tests::UnitTestTimeFixture |
| 145 | { |
| 146 | public: |
| 147 | SocketFixture() |
| 148 | : syncPrefix("/ndn/broadcast/sync") |
| 149 | { |
| 150 | syncPrefix.appendVersion(); |
| 151 | userPrefix[0] = Name("/user0"); |
| 152 | userPrefix[1] = Name("/user1"); |
| 153 | userPrefix[2] = Name("/user2"); |
| 154 | |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame^] | 155 | faces[0].reset(new DummyClientFace(io, {true, true})); |
| 156 | faces[1].reset(new DummyClientFace(io, {true, true})); |
| 157 | faces[2].reset(new DummyClientFace(io, {true, true})); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 158 | |
| 159 | for (int i = 0; i < 3; i++) { |
| 160 | readInterestOffset[i] = 0; |
| 161 | readDataOffset[i] = 0; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | passPacket() |
| 167 | { |
| 168 | for (int i = 0; i < 3; i++) |
| 169 | checkFace(i); |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | checkFace(int sender) |
| 174 | { |
| 175 | while (faces[sender]->sentInterests.size() > readInterestOffset[sender]) { |
| 176 | for (int i = 0; i < 3; i++) { |
| 177 | if (sender != i) |
| 178 | faces[i]->receive(faces[sender]->sentInterests[readInterestOffset[sender]]); |
| 179 | } |
| 180 | readInterestOffset[sender]++; |
| 181 | } |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame^] | 182 | while (faces[sender]->sentData.size() > readDataOffset[sender]) { |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 183 | for (int i = 0; i < 3; i++) { |
| 184 | if (sender != i) |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame^] | 185 | faces[i]->receive(faces[sender]->sentData[readDataOffset[sender]]); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 186 | } |
| 187 | readDataOffset[sender]++; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | createSocket(size_t idx, bool isNum) |
| 193 | { |
| 194 | app[idx] = make_shared<SocketTestApp>(syncPrefix, userPrefix[idx], ref(*faces[idx]), isNum); |
| 195 | sessionName[idx] = app[idx]->socket.getLogic().getSessionName(); |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | publishAppData(size_t idx, const string& data) |
| 200 | { |
| 201 | app[idx]->socket.publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), |
| 202 | ndn::time::milliseconds(1000)); |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | setAppData(size_t idx, SeqNo seqNo, const string& data) |
| 207 | { |
| 208 | Name dataName = sessionName[idx]; |
| 209 | dataName.appendNumber(seqNo); |
| 210 | app[idx]->set(dataName, data.c_str(), data.size()); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | publishAppNum(size_t idx, const uint8_t* buf, size_t size) |
| 215 | { |
| 216 | app[idx]->socket.publishData(buf, size, ndn::time::milliseconds(1000)); |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | setAppNum(size_t idx, SeqNo seqNo, const uint8_t* buf, size_t size) |
| 221 | { |
| 222 | Name dataName = sessionName[idx]; |
| 223 | dataName.appendNumber(seqNo); |
| 224 | app[idx]->setNum(dataName, buf, size); |
| 225 | } |
| 226 | |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame^] | 227 | public: |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 228 | Name syncPrefix; |
| 229 | Name userPrefix[3]; |
| 230 | Name sessionName[3]; |
| 231 | |
Junxiao Shi | 0c7f56a | 2016-07-14 15:27:14 +0000 | [diff] [blame^] | 232 | std::unique_ptr<DummyClientFace> faces[3]; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 233 | shared_ptr<SocketTestApp> app[3]; |
| 234 | |
| 235 | size_t readInterestOffset[3]; |
| 236 | size_t readDataOffset[3]; |
| 237 | }; |
| 238 | |
| 239 | |
| 240 | |
| 241 | BOOST_FIXTURE_TEST_SUITE(SocketTests, SocketFixture) |
| 242 | |
| 243 | BOOST_AUTO_TEST_CASE(BasicData) |
| 244 | { |
| 245 | createSocket(0, false); |
| 246 | advanceClocks(ndn::time::milliseconds(10), 5); |
| 247 | createSocket(1, false); |
| 248 | advanceClocks(ndn::time::milliseconds(10), 5); |
| 249 | createSocket(2, false); |
| 250 | advanceClocks(ndn::time::milliseconds(10), 5); |
| 251 | |
| 252 | string data0 = "Very funny Scotty, now beam down my clothes"; |
| 253 | publishAppData(0, data0); |
| 254 | |
| 255 | for (int i = 0; i < 50; i++) { |
| 256 | advanceClocks(ndn::time::milliseconds(2), 10); |
| 257 | passPacket(); |
| 258 | } |
| 259 | setAppData(0, 1, data0); |
| 260 | |
| 261 | advanceClocks(ndn::time::milliseconds(10), 1); |
| 262 | BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString()); |
| 263 | BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString()); |
| 264 | |
| 265 | string data1 = "Yes, give me that ketchup"; |
| 266 | string data2 = "Don't look conspicuous, it draws fire"; |
| 267 | publishAppData(0, data1); |
| 268 | advanceClocks(ndn::time::milliseconds(10), 1); |
| 269 | publishAppData(0, data2); |
| 270 | |
| 271 | for (int i = 0; i < 50; i++) { |
| 272 | advanceClocks(ndn::time::milliseconds(2), 10); |
| 273 | passPacket(); |
| 274 | } |
| 275 | setAppData(0, 2, data1); |
| 276 | advanceClocks(ndn::time::milliseconds(10), 1); |
| 277 | setAppData(0, 3, data2); |
| 278 | |
| 279 | advanceClocks(ndn::time::milliseconds(10), 1); |
| 280 | BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString()); |
| 281 | BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString()); |
| 282 | |
| 283 | string data3 = "You surf the Internet, I surf the real world"; |
| 284 | string data4 = "I got a fortune cookie once that said 'You like Chinese food'"; |
| 285 | string data5 = "Real men wear pink. Why? Because their wives make them"; |
| 286 | publishAppData(2, data3); |
| 287 | advanceClocks(ndn::time::milliseconds(10), 2); |
| 288 | publishAppData(1, data4); |
| 289 | advanceClocks(ndn::time::milliseconds(10), 1); |
| 290 | publishAppData(1, data5); |
| 291 | |
| 292 | for (int i = 0; i < 100; i++) { |
| 293 | advanceClocks(ndn::time::milliseconds(2), 10); |
| 294 | passPacket(); |
| 295 | } |
| 296 | setAppData(2, 1, data3); |
| 297 | advanceClocks(ndn::time::milliseconds(10), 1); |
| 298 | setAppData(1, 1, data4); |
| 299 | advanceClocks(ndn::time::milliseconds(10), 1); |
| 300 | setAppData(1, 2, data5); |
| 301 | |
| 302 | advanceClocks(ndn::time::milliseconds(10), 7); |
| 303 | BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString()); |
| 304 | BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString()); |
| 305 | |
| 306 | string data6 = "Shakespeare says: 'Prose before hos.'"; |
| 307 | string data7 = "Pick good people, talent never wears out"; |
| 308 | publishAppData(0, data6); |
| 309 | publishAppData(1, data7); |
| 310 | |
| 311 | for (int i = 0; i < 100; i++) { |
| 312 | advanceClocks(ndn::time::milliseconds(2), 10); |
| 313 | passPacket(); |
| 314 | } |
| 315 | setAppData(0, 4, data6); |
| 316 | setAppData(1, 3, data7); |
| 317 | |
| 318 | BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString()); |
| 319 | BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString()); |
| 320 | } |
| 321 | |
| 322 | BOOST_AUTO_TEST_CASE(BasicNumber) |
| 323 | { |
| 324 | createSocket(0, true); |
| 325 | advanceClocks(ndn::time::milliseconds(10), 5); |
| 326 | createSocket(1, true); |
| 327 | advanceClocks(ndn::time::milliseconds(10), 5); |
| 328 | |
| 329 | uint32_t num1[5] = {0, 1, 2, 3, 4}; |
| 330 | uint8_t* buf1 = reinterpret_cast<uint8_t*>(num1); |
| 331 | size_t size1 = sizeof(num1); |
| 332 | publishAppNum(0, buf1, size1); |
| 333 | advanceClocks(ndn::time::milliseconds(10), 5); |
| 334 | setAppNum(0, 0, buf1, size1); |
| 335 | |
| 336 | for (int i = 0; i < 100; i++) { |
| 337 | advanceClocks(ndn::time::milliseconds(2), 10); |
| 338 | passPacket(); |
| 339 | } |
| 340 | BOOST_CHECK_EQUAL(app[0]->sum, app[1]->sum); |
| 341 | BOOST_CHECK_EQUAL(app[1]->sum, 10); |
| 342 | |
| 343 | uint32_t num2[5] = {9, 7, 2, 1, 1}; |
| 344 | uint8_t* buf2 = reinterpret_cast<uint8_t*>(num2); |
| 345 | size_t size2 = sizeof(num2); |
| 346 | publishAppNum(1, buf2, size2); |
| 347 | setAppNum(1, 0, buf2, size2); |
| 348 | |
| 349 | for (int i = 0; i < 50; i++) { |
| 350 | advanceClocks(ndn::time::milliseconds(2), 10); |
| 351 | passPacket(); |
| 352 | } |
| 353 | BOOST_CHECK_EQUAL(app[0]->sum, app[1]->sum); |
| 354 | BOOST_CHECK_EQUAL(app[1]->sum, 30); |
| 355 | } |
| 356 | |
| 357 | BOOST_AUTO_TEST_SUITE_END() |
| 358 | |
| 359 | } // namespace test |
| 360 | } // namespace chronosync |