blob: 83d00dadbe20f2ae1ae5563b9d18247c547878db [file] [log] [blame]
Davide Pesaventofae9def2019-01-29 14:34:33 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento1af79492023-09-22 15:45:21 -04003 * Copyright (c) 2012-2023 University of California, Los Angeles
Davide Pesaventofae9def2019-01-29 14:34:33 -05004 *
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 Ding8c095fd2014-11-19 17:38:32 -080020#include "logic.hpp"
21
Davide Pesaventofae9def2019-01-29 14:34:33 -050022#include "tests/boost-test.hpp"
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080023
24namespace chronosync {
25namespace test {
26
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080027class Handler
28{
29public:
Davide Pesavento8663ed12022-07-23 03:04:27 -040030 Handler(ndn::Face& face, const Name& syncPrefix, const Name& userPrefix)
31 : logic(face, syncPrefix, userPrefix, [] (auto&&...) {})
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080032 {
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080033 }
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 Ding8c095fd2014-11-19 17:38:32 -080053 Logic logic;
54 std::map<Name, SeqNo> map;
55};
56
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080057class MultiUserFixture
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080058{
59public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080060 MultiUserFixture()
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080061 : 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 Pesavento8663ed12022-07-23 03:04:27 -040069 face = std::make_shared<ndn::Face>(io);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080070 }
71
72 Name syncPrefix;
73 Name userPrefix[3];
74
Davide Pesavento1af79492023-09-22 15:45:21 -040075 boost::asio::io_context io;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080076 shared_ptr<ndn::Face> face;
77 ndn::Scheduler scheduler;
78 shared_ptr<Handler> handler;
79};
80
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080081BOOST_FIXTURE_TEST_SUITE(MultiUserTests, MultiUserFixture)
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080082
83BOOST_AUTO_TEST_CASE(ThreeUserNode)
84{
Davide Pesavento8663ed12022-07-23 03:04:27 -040085 handler = std::make_shared<Handler>(*face, syncPrefix, userPrefix[0]);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080086 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 Pesaventofae9def2019-01-29 14:34:33 -050099 BOOST_CHECK_THROW(handler->logic.getSeqNo(userPrefix[0]), Logic::Error);
100 BOOST_CHECK_THROW(handler->logic.getSessionName(userPrefix[0]), Logic::Error);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800101}
102
103BOOST_AUTO_TEST_SUITE_END()
104
105} // namespace test
106} // namespace chronosync