Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #include <boost/test/unit_test.hpp> |
| 24 | #include <boost/test/output_test_stream.hpp> |
| 25 | #include <map> |
| 26 | using boost::test_tools::output_test_stream; |
| 27 | |
| 28 | #include <boost/make_shared.hpp> |
| 29 | |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 30 | #include "sync-ccnx-wrapper.h" |
| 31 | #include "sync-logic.h" |
| 32 | #include "sync-seq-no.h" |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 33 | |
| 34 | using namespace std; |
| 35 | using namespace boost; |
| 36 | using namespace Sync; |
| 37 | |
| 38 | struct Handler |
| 39 | { |
| 40 | string instance; |
| 41 | |
| 42 | Handler (const string &_instance) |
| 43 | : instance (_instance) |
| 44 | { |
| 45 | } |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 46 | |
Zhenkai Zhu | 43ae5c7 | 2012-05-31 23:18:45 -0700 | [diff] [blame] | 47 | void wrapper (const vector<MissingDataInfo> &v) { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 48 | int n = v.size(); |
| 49 | for (int i = 0; i < n; i++) { |
| 50 | onUpdate (v[i].prefix, v[i].high, v[i].low); |
| 51 | } |
| 52 | } |
| 53 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 54 | 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] | 55 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 56 | m_map[p] = seq.getSeq (); |
| 57 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 58 | // cout << instance << "\t"; |
| 59 | // if (!oldSeq.isValid ()) |
| 60 | // cout << "Inserted: " << p << " (" << seq << ")" << endl; |
| 61 | // else |
| 62 | // cout << "Updated: " << p << " ( " << oldSeq << ".." << seq << ")" << endl; |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void onRemove (const string &p/*prefix*/) |
| 66 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 67 | // cout << instance << "\tRemoved: " << p << endl; |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 68 | m_map.erase (p); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 69 | } |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 70 | |
| 71 | map<string, uint32_t> m_map; |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | BOOST_AUTO_TEST_CASE (SyncLogicTest) |
| 75 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 76 | Handler h1 ("1"); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 77 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 78 | SyncLogic l1 ("/bcast", bind (&Handler::wrapper, &h1, _1), bind (&Handler::onRemove, &h1, _1)); |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame^] | 79 | |
| 80 | std::string oldDigest = l1.getRootDigest(); |
| 81 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 82 | l1.addLocalNames ("/one", 1, 2); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 83 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL (h1.m_map.size (), 0); |
| 85 | sleep (1); |
| 86 | BOOST_CHECK_EQUAL (h1.m_map.size (), 0); |
| 87 | |
| 88 | Handler h2 ("2"); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 89 | SyncLogic l2 ("/bcast", bind (&Handler::wrapper, &h2, _1), bind (&Handler::onRemove, &h2, _1)); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 90 | |
| 91 | sleep (1); |
| 92 | BOOST_CHECK_EQUAL (h1.m_map.size (), 0); |
| 93 | BOOST_CHECK_EQUAL (h2.m_map.size (), 1); |
| 94 | |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame^] | 95 | l1.remove ("/one"); |
| 96 | sleep(1); |
| 97 | std::string newDigest = l1.getRootDigest(); |
| 98 | BOOST_CHECK(oldDigest != newDigest); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 100 | } |