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