blob: 583717c5db6e66e25020ad70304c3e6777e2757c [file] [log] [blame]
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * 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 Zhubc7bfce2012-03-09 14:37:52 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * 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 Zhubc7bfce2012-03-09 14:37:52 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * 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 Zhubc7bfce2012-03-09 14:37:52 -080018 */
19
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070020/*
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080021#include <boost/test/unit_test.hpp>
Alexander Afanasyev8722d872014-07-02 13:00:29 -070022#include <boost/test/output_test_stream.hpp>
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080023#include <map>
24using boost::test_tools::output_test_stream;
25
26#include <boost/make_shared.hpp>
27
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070028#include "sync-ccnx-wrapper.h"
29#include "sync-app-data-fetch.h"
30#include "sync-app-data-publish.h"
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080031
32using namespace Sync;
33using namespace std;
34using namespace boost;
35
Zhenkai Zhude59ea12012-03-09 14:41:49 -080036class TestStructApp {
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080037public:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070038 map<string, string> data;
39 void set(string str1, string str2) {
40 data.insert(make_pair(str1, str2));
41 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080042
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070043 void erase(string str1, string str2) {
44 data.erase(str1);
45 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080046
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070047 string toString(){
Alexander Afanasyev8722d872014-07-02 13:00:29 -070048 map<string, string>::iterator it = data.begin();
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070049 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 Zhubc7bfce2012-03-09 14:37:52 -080059
60};
61
62BOOST_AUTO_TEST_CASE (AppDataPublishAndFetchTest)
63{
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070064 TestStructApp foo;
65 TestStructApp bar;
Alexander Afanasyev8722d872014-07-02 13:00:29 -070066
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070067 string interest = "/april/fool";
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070068 string seq[5] = {"0", "1", "2", "3", "4" };
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070069 string str[5] = {"panda", "express", "tastes", "so", "good"};
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080070
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070071 for (int i = 0; i < 5; i++) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070072 foo.set(interest + "/" + "0/" + seq[i], str[i]);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070073 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080074
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070075 boost::function<void (string, string)> setFunc =
76 bind(&TestStructApp::set, &bar, _1, _2);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080077
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070078 shared_ptr<CcnxWrapper> handle(new CcnxWrapper());
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080079
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070080 AppDataFetch fetcher(handle, setFunc);
81 AppDataPublish publisher(handle);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080082
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070083 for (int i = 1; i <= 5; i++) {
84 publisher.publishData(interest, 0, str[i - 1], 5);
85 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080086
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070087 BOOST_CHECK_EQUAL(publisher.getNextSeq(interest, 0), 5);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070088 BOOST_CHECK_EQUAL(publisher.getRecentData(interest, 0), str[4]);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080089
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070090 fetcher.onUpdate (interest, SeqNo (0,4), SeqNo (0,-1));
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070091 // give time for ccnd to react
92 sleep(1);
93 BOOST_CHECK_EQUAL(foo.toString(), bar.toString());
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080094
95
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070096 boost::function<void (string, string)> eraseFunc =
97 bind(&TestStructApp::erase, &bar, _1, _2);
98 fetcher.setDataCallback(eraseFunc);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080099
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -0700100 fetcher.onUpdate (interest, SeqNo (0,4), SeqNo (0,-1));
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700101 // give time for ccnd to react
102 sleep(1);
103 TestStructApp poo;
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800104
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700105 BOOST_CHECK_EQUAL(poo.toString(), bar.toString());
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800106
107}
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700108*/