Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame^] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
| 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 "logic.hpp" |
| 21 | |
| 22 | #include "boost-test.hpp" |
| 23 | |
| 24 | namespace chronosync { |
| 25 | namespace test { |
| 26 | |
| 27 | using std::vector; |
| 28 | |
| 29 | class Handler |
| 30 | { |
| 31 | public: |
| 32 | Handler(ndn::Face& face, |
| 33 | const Name& syncPrefix, |
| 34 | const Name& userPrefix) |
| 35 | : logic(face, |
| 36 | syncPrefix, |
| 37 | userPrefix, |
| 38 | bind(&Handler::onUpdate, this, _1)) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | onUpdate(const vector<MissingDataInfo>& v) |
| 44 | { |
| 45 | for (size_t i = 0; i < v.size(); i++) { |
| 46 | update(v[i].session, v[i].high, v[i].low); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | update(const Name& p, const SeqNo& high, const SeqNo& low) |
| 52 | { |
| 53 | map[p] = high; |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | updateSeqNo(const SeqNo& seqNo) |
| 58 | { |
| 59 | logic.updateSeqNo(seqNo); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | check(const Name& sessionName, const SeqNo& seqNo) |
| 64 | { |
| 65 | BOOST_CHECK_EQUAL(map[sessionName], seqNo); |
| 66 | } |
| 67 | |
| 68 | Logic logic; |
| 69 | std::map<Name, SeqNo> map; |
| 70 | }; |
| 71 | |
| 72 | class LogicFixture |
| 73 | { |
| 74 | public: |
| 75 | LogicFixture() |
| 76 | : syncPrefix("/ndn/broadcast/sync") |
| 77 | , scheduler(io) |
| 78 | { |
| 79 | syncPrefix.appendVersion(); |
| 80 | userPrefix[0] = Name("/user0"); |
| 81 | userPrefix[1] = Name("/user1"); |
| 82 | userPrefix[2] = Name("/user2"); |
| 83 | |
| 84 | faces[0] = make_shared<ndn::Face>(ref(io)); |
| 85 | faces[1] = make_shared<ndn::Face>(ref(io)); |
| 86 | faces[2] = make_shared<ndn::Face>(ref(io)); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | createHandler(size_t idx) |
| 91 | { |
| 92 | handler[idx] = make_shared<Handler>(ref(*faces[idx]), syncPrefix, userPrefix[idx]); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | updateSeqNo(size_t idx, const SeqNo& seqNo) |
| 97 | { |
| 98 | handler[idx]->updateSeqNo(seqNo); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | checkSeqNo(size_t sIdx, size_t dIdx, const SeqNo& seqNo) |
| 103 | { |
| 104 | handler[sIdx]->check(handler[dIdx]->logic.getSessionName(), seqNo); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | terminate() |
| 109 | { |
| 110 | io.stop(); |
| 111 | } |
| 112 | |
| 113 | Name syncPrefix; |
| 114 | Name userPrefix[3]; |
| 115 | |
| 116 | boost::asio::io_service io; |
| 117 | shared_ptr<ndn::Face> faces[3]; |
| 118 | ndn::Scheduler scheduler; |
| 119 | shared_ptr<Handler> handler[3]; |
| 120 | }; |
| 121 | |
| 122 | BOOST_FIXTURE_TEST_SUITE(LogicTests, LogicFixture) |
| 123 | |
| 124 | void |
| 125 | onUpdate(const vector<MissingDataInfo>& v) |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(Constructor) |
| 130 | { |
| 131 | Name syncPrefix("/ndn/broadcast/sync"); |
| 132 | Name userPrefix("/user"); |
| 133 | ndn::Face face; |
| 134 | BOOST_REQUIRE_NO_THROW(Logic(face, syncPrefix, userPrefix, |
| 135 | bind(onUpdate, _1))); |
| 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(TwoBasic) |
| 139 | { |
| 140 | scheduler.scheduleEvent(ndn::time::milliseconds(100), |
| 141 | bind(&LogicFixture::createHandler, this, 0)); |
| 142 | |
| 143 | scheduler.scheduleEvent(ndn::time::milliseconds(200), |
| 144 | bind(&LogicFixture::createHandler, this, 1)); |
| 145 | |
| 146 | scheduler.scheduleEvent(ndn::time::milliseconds(300), |
| 147 | bind(&LogicFixture::updateSeqNo, this, 0, 1)); |
| 148 | |
| 149 | scheduler.scheduleEvent(ndn::time::milliseconds(1000), |
| 150 | bind(&LogicFixture::checkSeqNo, this, 1, 0, 1)); |
| 151 | |
| 152 | scheduler.scheduleEvent(ndn::time::milliseconds(1100), |
| 153 | bind(&LogicFixture::updateSeqNo, this, 0, 2)); |
| 154 | |
| 155 | scheduler.scheduleEvent(ndn::time::milliseconds(1800), |
| 156 | bind(&LogicFixture::checkSeqNo, this, 1, 0, 2)); |
| 157 | |
| 158 | scheduler.scheduleEvent(ndn::time::milliseconds(1900), |
| 159 | bind(&LogicFixture::updateSeqNo, this, 1, 2)); |
| 160 | |
| 161 | scheduler.scheduleEvent(ndn::time::milliseconds(2600), |
| 162 | bind(&LogicFixture::checkSeqNo, this, 0, 1, 2)); |
| 163 | |
| 164 | scheduler.scheduleEvent(ndn::time::milliseconds(2800), |
| 165 | bind(&LogicFixture::terminate, this)); |
| 166 | |
| 167 | io.run(); |
| 168 | } |
| 169 | |
| 170 | BOOST_AUTO_TEST_CASE(ThreeBasic) |
| 171 | { |
| 172 | scheduler.scheduleEvent(ndn::time::milliseconds(100), |
| 173 | bind(&LogicFixture::createHandler, this, 0)); |
| 174 | |
| 175 | scheduler.scheduleEvent(ndn::time::milliseconds(200), |
| 176 | bind(&LogicFixture::createHandler, this, 1)); |
| 177 | |
| 178 | scheduler.scheduleEvent(ndn::time::milliseconds(300), |
| 179 | bind(&LogicFixture::createHandler, this, 2)); |
| 180 | |
| 181 | scheduler.scheduleEvent(ndn::time::milliseconds(500), |
| 182 | bind(&LogicFixture::updateSeqNo, this, 0, 1)); |
| 183 | |
| 184 | scheduler.scheduleEvent(ndn::time::milliseconds(1400), |
| 185 | bind(&LogicFixture::checkSeqNo, this, 1, 0, 1)); |
| 186 | |
| 187 | scheduler.scheduleEvent(ndn::time::milliseconds(1450), |
| 188 | bind(&LogicFixture::checkSeqNo, this, 2, 0, 1)); |
| 189 | |
| 190 | scheduler.scheduleEvent(ndn::time::milliseconds(1500), |
| 191 | bind(&LogicFixture::updateSeqNo, this, 1, 2)); |
| 192 | |
| 193 | scheduler.scheduleEvent(ndn::time::milliseconds(2400), |
| 194 | bind(&LogicFixture::checkSeqNo, this, 0, 1, 2)); |
| 195 | |
| 196 | scheduler.scheduleEvent(ndn::time::milliseconds(2450), |
| 197 | bind(&LogicFixture::checkSeqNo, this, 2, 1, 2)); |
| 198 | |
| 199 | scheduler.scheduleEvent(ndn::time::milliseconds(2500), |
| 200 | bind(&LogicFixture::updateSeqNo, this, 2, 4)); |
| 201 | |
| 202 | scheduler.scheduleEvent(ndn::time::milliseconds(4400), |
| 203 | bind(&LogicFixture::checkSeqNo, this, 0, 2, 4)); |
| 204 | |
| 205 | scheduler.scheduleEvent(ndn::time::milliseconds(4450), |
| 206 | bind(&LogicFixture::checkSeqNo, this, 1, 2, 4)); |
| 207 | |
| 208 | scheduler.scheduleEvent(ndn::time::milliseconds(4500), |
| 209 | bind(&LogicFixture::terminate, this)); |
| 210 | |
| 211 | io.run(); |
| 212 | } |
| 213 | |
| 214 | BOOST_AUTO_TEST_CASE(ResetRecover) |
| 215 | { |
| 216 | scheduler.scheduleEvent(ndn::time::milliseconds(100), |
| 217 | bind(&LogicFixture::createHandler, this, 0)); |
| 218 | |
| 219 | scheduler.scheduleEvent(ndn::time::milliseconds(200), |
| 220 | bind(&LogicFixture::createHandler, this, 1)); |
| 221 | |
| 222 | scheduler.scheduleEvent(ndn::time::milliseconds(500), |
| 223 | bind(&LogicFixture::updateSeqNo, this, 0, 1)); |
| 224 | |
| 225 | scheduler.scheduleEvent(ndn::time::milliseconds(1400), |
| 226 | bind(&LogicFixture::checkSeqNo, this, 1, 0, 1)); |
| 227 | |
| 228 | scheduler.scheduleEvent(ndn::time::milliseconds(1500), |
| 229 | bind(&LogicFixture::updateSeqNo, this, 1, 2)); |
| 230 | |
| 231 | scheduler.scheduleEvent(ndn::time::milliseconds(2400), |
| 232 | bind(&LogicFixture::checkSeqNo, this, 0, 1, 2)); |
| 233 | |
| 234 | scheduler.scheduleEvent(ndn::time::milliseconds(2500), |
| 235 | bind(&LogicFixture::createHandler, this, 2)); |
| 236 | |
| 237 | scheduler.scheduleEvent(ndn::time::milliseconds(3000), |
| 238 | bind(&LogicFixture::checkSeqNo, this, 1, 0, 1)); |
| 239 | |
| 240 | scheduler.scheduleEvent(ndn::time::milliseconds(3050), |
| 241 | bind(&LogicFixture::checkSeqNo, this, 0, 1, 2)); |
| 242 | |
| 243 | scheduler.scheduleEvent(ndn::time::milliseconds(3100), |
| 244 | bind(&LogicFixture::updateSeqNo, this, 2, 4)); |
| 245 | |
| 246 | scheduler.scheduleEvent(ndn::time::milliseconds(4000), |
| 247 | bind(&LogicFixture::checkSeqNo, this, 1, 2, 4)); |
| 248 | |
| 249 | scheduler.scheduleEvent(ndn::time::milliseconds(4050), |
| 250 | bind(&LogicFixture::checkSeqNo, this, 0, 2, 4)); |
| 251 | |
| 252 | |
| 253 | scheduler.scheduleEvent(ndn::time::milliseconds(4500), |
| 254 | bind(&LogicFixture::terminate, this)); |
| 255 | |
| 256 | io.run(); |
| 257 | } |
| 258 | |
| 259 | BOOST_AUTO_TEST_CASE(RecoverConflict) |
| 260 | { |
| 261 | scheduler.scheduleEvent(ndn::time::milliseconds(0), |
| 262 | bind(&LogicFixture::createHandler, this, 0)); |
| 263 | |
| 264 | scheduler.scheduleEvent(ndn::time::milliseconds(50), |
| 265 | bind(&LogicFixture::createHandler, this, 1)); |
| 266 | |
| 267 | scheduler.scheduleEvent(ndn::time::milliseconds(100), |
| 268 | bind(&LogicFixture::createHandler, this, 2)); |
| 269 | |
| 270 | scheduler.scheduleEvent(ndn::time::milliseconds(500), |
| 271 | bind(&LogicFixture::updateSeqNo, this, 0, 1)); |
| 272 | |
| 273 | scheduler.scheduleEvent(ndn::time::milliseconds(1400), |
| 274 | bind(&LogicFixture::checkSeqNo, this, 1, 0, 1)); |
| 275 | |
| 276 | scheduler.scheduleEvent(ndn::time::milliseconds(1400), |
| 277 | bind(&LogicFixture::checkSeqNo, this, 2, 0, 1)); |
| 278 | |
| 279 | scheduler.scheduleEvent(ndn::time::milliseconds(1500), |
| 280 | bind(&LogicFixture::updateSeqNo, this, 1, 2)); |
| 281 | |
| 282 | scheduler.scheduleEvent(ndn::time::milliseconds(1500), |
| 283 | bind(&LogicFixture::updateSeqNo, this, 2, 4)); |
| 284 | |
| 285 | scheduler.scheduleEvent(ndn::time::milliseconds(2400), |
| 286 | bind(&LogicFixture::checkSeqNo, this, 0, 1, 2)); |
| 287 | |
| 288 | scheduler.scheduleEvent(ndn::time::milliseconds(2450), |
| 289 | bind(&LogicFixture::checkSeqNo, this, 0, 2, 4)); |
| 290 | |
| 291 | scheduler.scheduleEvent(ndn::time::milliseconds(2500), |
| 292 | bind(&LogicFixture::checkSeqNo, this, 1, 2, 4)); |
| 293 | |
| 294 | scheduler.scheduleEvent(ndn::time::milliseconds(2550), |
| 295 | bind(&LogicFixture::checkSeqNo, this, 2, 1, 2)); |
| 296 | |
| 297 | scheduler.scheduleEvent(ndn::time::milliseconds(4500), |
| 298 | bind(&LogicFixture::terminate, this)); |
| 299 | |
| 300 | io.run(); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | BOOST_AUTO_TEST_SUITE_END() |
| 305 | |
| 306 | } // namespace test |
| 307 | } // namespace chronosync |