Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2012-2023 University of California, Los Angeles |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [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 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 20 | #include "logic.hpp" |
| 21 | |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 22 | #include "tests/boost-test.hpp" |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 23 | |
| 24 | namespace chronosync { |
| 25 | namespace test { |
| 26 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 27 | class Handler |
| 28 | { |
| 29 | public: |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 30 | Handler(ndn::Face& face, const Name& syncPrefix, const Name& userPrefix) |
| 31 | : logic(face, syncPrefix, userPrefix, [] (auto&&...) {}) |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 32 | { |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void |
| 36 | updateSeqNo(const SeqNo& seqNo) |
| 37 | { |
| 38 | logic.updateSeqNo(seqNo); |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | addUserNode(const Name& prefix) |
| 43 | { |
| 44 | logic.addUserNode(prefix); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | removeUserNode(const Name& prefix) |
| 49 | { |
| 50 | logic.removeUserNode(prefix); |
| 51 | } |
| 52 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 53 | Logic logic; |
| 54 | std::map<Name, SeqNo> map; |
| 55 | }; |
| 56 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 57 | class MultiUserFixture |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 58 | { |
| 59 | public: |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 60 | MultiUserFixture() |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 61 | : syncPrefix("/ndn/broadcast/sync") |
| 62 | , scheduler(io) |
| 63 | { |
| 64 | syncPrefix.appendVersion(); |
| 65 | userPrefix[0] = Name("/user0"); |
| 66 | userPrefix[1] = Name("/user1"); |
| 67 | userPrefix[2] = Name("/user2"); |
| 68 | |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 69 | face = std::make_shared<ndn::Face>(io); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | Name syncPrefix; |
| 73 | Name userPrefix[3]; |
| 74 | |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 75 | boost::asio::io_context io; |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 76 | shared_ptr<ndn::Face> face; |
| 77 | ndn::Scheduler scheduler; |
| 78 | shared_ptr<Handler> handler; |
| 79 | }; |
| 80 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 81 | BOOST_FIXTURE_TEST_SUITE(MultiUserTests, MultiUserFixture) |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 82 | |
| 83 | BOOST_AUTO_TEST_CASE(ThreeUserNode) |
| 84 | { |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 85 | handler = std::make_shared<Handler>(*face, syncPrefix, userPrefix[0]); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 86 | handler->addUserNode(userPrefix[1]); |
| 87 | handler->addUserNode(userPrefix[2]); |
| 88 | handler->removeUserNode(userPrefix[0]); |
| 89 | |
| 90 | handler->logic.setDefaultUserPrefix(userPrefix[1]); |
| 91 | handler->updateSeqNo(1); |
| 92 | BOOST_CHECK_EQUAL(handler->logic.getSeqNo(userPrefix[1]), 1); |
| 93 | |
| 94 | handler->logic.updateSeqNo(2, userPrefix[2]); |
| 95 | handler->logic.setDefaultUserPrefix(userPrefix[2]); |
| 96 | |
| 97 | BOOST_CHECK_EQUAL(handler->logic.getSeqNo(), 2); |
| 98 | |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 99 | BOOST_CHECK_THROW(handler->logic.getSeqNo(userPrefix[0]), Logic::Error); |
| 100 | BOOST_CHECK_THROW(handler->logic.getSessionName(userPrefix[0]), Logic::Error); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_SUITE_END() |
| 104 | |
| 105 | } // namespace test |
| 106 | } // namespace chronosync |