blob: ad1d2ed9d219d4d29007a937b9f7aeab08516a98 [file] [log] [blame]
Davide Pesaventofae9def2019-01-29 14:34:33 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento30c41ec2024-02-12 17:36:35 -05003 * Copyright (c) 2012-2024 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
Davide Pesavento30c41ec2024-02-12 17:36:35 -050024namespace chronosync::tests {
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080025
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080026class Handler
27{
28public:
Davide Pesavento8663ed12022-07-23 03:04:27 -040029 Handler(ndn::Face& face, const Name& syncPrefix, const Name& userPrefix)
30 : logic(face, syncPrefix, userPrefix, [] (auto&&...) {})
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080031 {
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080032 }
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 Ding8c095fd2014-11-19 17:38:32 -080052 Logic logic;
53 std::map<Name, SeqNo> map;
54};
55
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080056class MultiUserFixture
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080057{
58public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080059 MultiUserFixture()
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080060 : 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 Pesavento8663ed12022-07-23 03:04:27 -040068 face = std::make_shared<ndn::Face>(io);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080069 }
70
71 Name syncPrefix;
72 Name userPrefix[3];
73
Davide Pesavento1af79492023-09-22 15:45:21 -040074 boost::asio::io_context io;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080075 shared_ptr<ndn::Face> face;
76 ndn::Scheduler scheduler;
77 shared_ptr<Handler> handler;
78};
79
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080080BOOST_FIXTURE_TEST_SUITE(MultiUserTests, MultiUserFixture)
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080081
82BOOST_AUTO_TEST_CASE(ThreeUserNode)
83{
Davide Pesavento8663ed12022-07-23 03:04:27 -040084 handler = std::make_shared<Handler>(*face, syncPrefix, userPrefix[0]);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080085 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 Pesaventofae9def2019-01-29 14:34:33 -050098 BOOST_CHECK_THROW(handler->logic.getSeqNo(userPrefix[0]), Logic::Error);
99 BOOST_CHECK_THROW(handler->logic.getSessionName(userPrefix[0]), Logic::Error);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800100}
101
102BOOST_AUTO_TEST_SUITE_END()
103
Davide Pesavento30c41ec2024-02-12 17:36:35 -0500104} // namespace chronosync::tests