blob: db8ffcf4f89615cad55f73aca4b54d0d31faa533 [file] [log] [blame]
Davide Pesaventofae9def2019-01-29 14:34:33 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2019 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
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:
30 Handler(ndn::Face& face,
31 const Name& syncPrefix,
32 const Name& userPrefix)
33 : logic(face,
34 syncPrefix,
35 userPrefix,
36 bind(&Handler::onUpdate, this, _1))
37 {
38 }
39
40 void
Davide Pesaventofae9def2019-01-29 14:34:33 -050041 onUpdate(const std::vector<MissingDataInfo>& v)
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080042 {
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080043 }
44
45 void
46 updateSeqNo(const SeqNo& seqNo)
47 {
48 logic.updateSeqNo(seqNo);
49 }
50
51 void
52 addUserNode(const Name& prefix)
53 {
54 logic.addUserNode(prefix);
55 }
56
57 void
58 removeUserNode(const Name& prefix)
59 {
60 logic.removeUserNode(prefix);
61 }
62
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080063 Logic logic;
64 std::map<Name, SeqNo> map;
65};
66
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080067class MultiUserFixture
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080068{
69public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080070 MultiUserFixture()
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080071 : syncPrefix("/ndn/broadcast/sync")
72 , scheduler(io)
73 {
74 syncPrefix.appendVersion();
75 userPrefix[0] = Name("/user0");
76 userPrefix[1] = Name("/user1");
77 userPrefix[2] = Name("/user2");
78
79 face = make_shared<ndn::Face>(ref(io));
80 }
81
82 Name syncPrefix;
83 Name userPrefix[3];
84
85 boost::asio::io_service io;
86 shared_ptr<ndn::Face> face;
87 ndn::Scheduler scheduler;
88 shared_ptr<Handler> handler;
89};
90
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080091BOOST_FIXTURE_TEST_SUITE(MultiUserTests, MultiUserFixture)
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080092
93BOOST_AUTO_TEST_CASE(ThreeUserNode)
94{
95 handler = make_shared<Handler>(ref(*face), syncPrefix, userPrefix[0]);
96 handler->addUserNode(userPrefix[1]);
97 handler->addUserNode(userPrefix[2]);
98 handler->removeUserNode(userPrefix[0]);
99
100 handler->logic.setDefaultUserPrefix(userPrefix[1]);
101 handler->updateSeqNo(1);
102 BOOST_CHECK_EQUAL(handler->logic.getSeqNo(userPrefix[1]), 1);
103
104 handler->logic.updateSeqNo(2, userPrefix[2]);
105 handler->logic.setDefaultUserPrefix(userPrefix[2]);
106
107 BOOST_CHECK_EQUAL(handler->logic.getSeqNo(), 2);
108
Davide Pesaventofae9def2019-01-29 14:34:33 -0500109 BOOST_CHECK_THROW(handler->logic.getSeqNo(userPrefix[0]), Logic::Error);
110 BOOST_CHECK_THROW(handler->logic.getSessionName(userPrefix[0]), Logic::Error);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800111}
112
113BOOST_AUTO_TEST_SUITE_END()
114
115} // namespace test
116} // namespace chronosync