Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [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. |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [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. |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [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. |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [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/>. |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 18 | */ |
| 19 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 20 | /* |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 21 | #include <boost/test/unit_test.hpp> |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 22 | #include <boost/test/output_test_stream.hpp> |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 23 | #include <map> |
| 24 | using boost::test_tools::output_test_stream; |
| 25 | |
| 26 | #include <boost/make_shared.hpp> |
| 27 | |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 28 | #include "sync-ccnx-wrapper.h" |
| 29 | #include "sync-app-data-fetch.h" |
| 30 | #include "sync-app-data-publish.h" |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace Sync; |
| 33 | using namespace std; |
| 34 | using namespace boost; |
| 35 | |
Zhenkai Zhu | de59ea1 | 2012-03-09 14:41:49 -0800 | [diff] [blame] | 36 | class TestStructApp { |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 37 | public: |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 38 | map<string, string> data; |
| 39 | void set(string str1, string str2) { |
| 40 | data.insert(make_pair(str1, str2)); |
| 41 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 43 | void erase(string str1, string str2) { |
| 44 | data.erase(str1); |
| 45 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 47 | string toString(){ |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 48 | map<string, string>::iterator it = data.begin(); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 49 | string str = ""; |
| 50 | for (; it != data.end(); ++it){ |
| 51 | str += "<"; |
| 52 | str += it->first; |
| 53 | str += "|"; |
| 54 | str += it->second; |
| 55 | str += ">"; |
| 56 | } |
| 57 | return str; |
| 58 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 59 | |
| 60 | }; |
| 61 | |
| 62 | BOOST_AUTO_TEST_CASE (AppDataPublishAndFetchTest) |
| 63 | { |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 64 | TestStructApp foo; |
| 65 | TestStructApp bar; |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 66 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 67 | string interest = "/april/fool"; |
Zhenkai Zhu | 1ee93c9 | 2012-03-12 20:32:57 -0700 | [diff] [blame] | 68 | string seq[5] = {"0", "1", "2", "3", "4" }; |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 69 | string str[5] = {"panda", "express", "tastes", "so", "good"}; |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 71 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 72 | foo.set(interest + "/" + "0/" + seq[i], str[i]); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 73 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 75 | boost::function<void (string, string)> setFunc = |
| 76 | bind(&TestStructApp::set, &bar, _1, _2); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 78 | shared_ptr<CcnxWrapper> handle(new CcnxWrapper()); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 80 | AppDataFetch fetcher(handle, setFunc); |
| 81 | AppDataPublish publisher(handle); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 83 | for (int i = 1; i <= 5; i++) { |
| 84 | publisher.publishData(interest, 0, str[i - 1], 5); |
| 85 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 86 | |
Zhenkai Zhu | 1ee93c9 | 2012-03-12 20:32:57 -0700 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(publisher.getNextSeq(interest, 0), 5); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(publisher.getRecentData(interest, 0), str[4]); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 89 | |
Zhenkai Zhu | 1ee93c9 | 2012-03-12 20:32:57 -0700 | [diff] [blame] | 90 | fetcher.onUpdate (interest, SeqNo (0,4), SeqNo (0,-1)); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 91 | // give time for ccnd to react |
| 92 | sleep(1); |
| 93 | BOOST_CHECK_EQUAL(foo.toString(), bar.toString()); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 94 | |
| 95 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 96 | boost::function<void (string, string)> eraseFunc = |
| 97 | bind(&TestStructApp::erase, &bar, _1, _2); |
| 98 | fetcher.setDataCallback(eraseFunc); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 99 | |
Zhenkai Zhu | 1ee93c9 | 2012-03-12 20:32:57 -0700 | [diff] [blame] | 100 | fetcher.onUpdate (interest, SeqNo (0,4), SeqNo (0,-1)); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 101 | // give time for ccnd to react |
| 102 | sleep(1); |
| 103 | TestStructApp poo; |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(poo.toString(), bar.toString()); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 106 | |
| 107 | } |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 108 | */ |