Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame^] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame^] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame^] | 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. |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame^] | 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. |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame^] | 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/>. |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <boost/test/unit_test.hpp> |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 21 | #include <boost/test/output_test_stream.hpp> |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 22 | #include <map> |
| 23 | using boost::test_tools::output_test_stream; |
| 24 | |
| 25 | #include <boost/make_shared.hpp> |
| 26 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/security/validator-null.hpp> |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 28 | #include "sync-logic.h" |
| 29 | #include "sync-seq-no.h" |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 30 | |
| 31 | using namespace std; |
| 32 | using namespace boost; |
| 33 | using namespace Sync; |
| 34 | |
| 35 | struct Handler |
| 36 | { |
| 37 | string instance; |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 39 | Handler (const string &_instance) |
| 40 | : instance (_instance) |
| 41 | { |
| 42 | } |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 43 | |
Zhenkai Zhu | 43ae5c7 | 2012-05-31 23:18:45 -0700 | [diff] [blame] | 44 | void wrapper (const vector<MissingDataInfo> &v) { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 45 | int n = v.size(); |
| 46 | for (int i = 0; i < n; i++) { |
| 47 | onUpdate (v[i].prefix, v[i].high, v[i].low); |
| 48 | } |
| 49 | } |
| 50 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 51 | void onUpdate (const string &p/*prefix*/, const SeqNo &seq/*newSeq*/, const SeqNo &oldSeq/*oldSeq*/) |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 52 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 53 | m_map[p] = seq.getSeq (); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 55 | // cout << instance << "\t"; |
| 56 | // if (!oldSeq.isValid ()) |
| 57 | // cout << "Inserted: " << p << " (" << seq << ")" << endl; |
| 58 | // else |
| 59 | // cout << "Updated: " << p << " ( " << oldSeq << ".." << seq << ")" << endl; |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void onRemove (const string &p/*prefix*/) |
| 63 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 64 | // cout << instance << "\tRemoved: " << p << endl; |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 65 | m_map.erase (p); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 66 | } |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 67 | |
| 68 | map<string, uint32_t> m_map; |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 71 | class TestCore |
| 72 | { |
| 73 | public: |
| 74 | TestCore(ndn::shared_ptr<boost::asio::io_service> ioService) |
| 75 | : m_ioService(ioService) |
| 76 | { |
| 77 | m_l[0] = 0; |
| 78 | m_l[1] = 0; |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 79 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 80 | m_validator = ndn::make_shared<ndn::ValidatorNull>(); |
| 81 | } |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 82 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 83 | ~TestCore() |
| 84 | { |
| 85 | if(m_l[0] != 0) |
| 86 | delete m_l[0]; |
| 87 | |
| 88 | if(m_l[1] != 0) |
| 89 | delete m_l[1]; |
| 90 | } |
| 91 | |
| 92 | void |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 93 | finish(ndn::shared_ptr<boost::asio::io_service> ioService) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 94 | { |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 95 | ioService->stop(); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 96 | } |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 97 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 98 | void |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 99 | createSyncLogic(int index, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 100 | ndn::shared_ptr<Handler> h) |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 101 | { |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 102 | ndn::Name identity("/tmp-" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count())); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 103 | ndn::shared_ptr<ndn::IdentityCertificate> cert = m_keyChain.getCertificate(m_keyChain.createIdentity(identity)); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 104 | m_faces[index] = ndn::make_shared<ndn::Face>(m_ioService); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 105 | m_l[index] = new SyncLogic(ndn::Name("/bcast"), |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 106 | *cert, |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 107 | m_validator, m_faces[index], |
| 108 | bind (&Handler::wrapper, &*h, _1), |
| 109 | bind (&Handler::onRemove, &*h, _1)); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
| 113 | getOldDigestForOne() |
| 114 | { |
| 115 | m_oldDigest = m_l[0]->getRootDigest(); |
| 116 | } |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 117 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 118 | void |
| 119 | getNewDigestForOne() |
| 120 | { |
| 121 | m_newDigest = m_l[0]->getRootDigest(); |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | addLocalNamesForOne(ndn::Name name, uint64_t session, uint64_t seq) |
| 126 | { |
| 127 | m_l[0]->addLocalNames(name, session, seq); |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | removeForOne(ndn::Name name) |
| 132 | { |
| 133 | m_l[0]->remove(name); |
| 134 | } |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 135 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 136 | void |
| 137 | checkDigest() |
| 138 | { |
| 139 | BOOST_CHECK(m_oldDigest != m_newDigest); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | public: |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 144 | ndn::KeyChain m_keyChain; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 145 | ndn::shared_ptr<boost::asio::io_service> m_ioService; |
| 146 | SyncLogic* m_l[2]; |
| 147 | ndn::shared_ptr<ndn::Face> m_faces[2]; |
| 148 | ndn::shared_ptr<ndn::ValidatorNull> m_validator; |
| 149 | string m_oldDigest; |
| 150 | string m_newDigest; |
| 151 | }; |
| 152 | |
| 153 | void |
| 154 | checkMapSize(ndn::shared_ptr<Handler> h, int size) |
| 155 | { BOOST_CHECK_EQUAL (h->m_map.size (), size); } |
| 156 | |
| 157 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 158 | BOOST_AUTO_TEST_CASE (SyncLogicTest) |
| 159 | { |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 160 | ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>(); |
| 161 | ndn::Scheduler scheduler(*ioService); |
| 162 | TestCore testCore(ioService); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 163 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 164 | ndn::shared_ptr<Handler> h1 = ndn::make_shared<Handler>("1"); |
| 165 | ndn::shared_ptr<Handler> h2 = ndn::make_shared<Handler>("2"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 166 | |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 167 | scheduler.scheduleEvent(ndn::time::milliseconds(0), ndn::bind(&TestCore::createSyncLogic, &testCore, 0, h1)); |
| 168 | scheduler.scheduleEvent(ndn::time::milliseconds(100), ndn::bind(&TestCore::getOldDigestForOne, &testCore)); |
| 169 | scheduler.scheduleEvent(ndn::time::milliseconds(200), ndn::bind(&TestCore::addLocalNamesForOne, &testCore, "/one", 1, 2)); |
| 170 | scheduler.scheduleEvent(ndn::time::milliseconds(300), ndn::bind(&checkMapSize, h1, 0)); |
| 171 | scheduler.scheduleEvent(ndn::time::milliseconds(400), ndn::bind(&TestCore::createSyncLogic, &testCore, 1, h2)); |
| 172 | scheduler.scheduleEvent(ndn::time::milliseconds(500), ndn::bind(&checkMapSize, h1, 0)); |
| 173 | scheduler.scheduleEvent(ndn::time::milliseconds(600), ndn::bind(&checkMapSize, h2, 1)); |
| 174 | scheduler.scheduleEvent(ndn::time::milliseconds(700), ndn::bind(&TestCore::removeForOne, &testCore, "/one")); |
| 175 | scheduler.scheduleEvent(ndn::time::milliseconds(800), ndn::bind(&TestCore::getNewDigestForOne, &testCore)); |
| 176 | scheduler.scheduleEvent(ndn::time::milliseconds(900), ndn::bind(&TestCore::checkDigest, &testCore)); |
| 177 | scheduler.scheduleEvent(ndn::time::milliseconds(1000), ndn::bind(&TestCore::finish, &testCore, ioService)); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 178 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 179 | ioService->run(); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 180 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 181 | } |