blob: d2444e7aab0ea2d2072ba68f76a79a1ddc08728c [file] [log] [blame]
Yingdi Yu280bb962014-01-30 09:52:43 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -07002/*
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 Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070020 * 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>
26using boost::test_tools::output_test_stream;
27
28#include <boost/make_shared.hpp>
29
Yingdi Yu280bb962014-01-30 09:52:43 -080030#include <ndn-cpp-dev/security/validator-null.hpp>
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070031#include "sync-logic.h"
32#include "sync-seq-no.h"
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070033
34using namespace std;
35using namespace boost;
36using namespace Sync;
37
38struct Handler
39{
40 string instance;
41
42 Handler (const string &_instance)
43 : instance (_instance)
44 {
45 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070046
Zhenkai Zhu43ae5c72012-05-31 23:18:45 -070047 void wrapper (const vector<MissingDataInfo> &v) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070048 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 Afanasyev1b449c42012-03-13 20:24:07 -070054 void onUpdate (const string &p/*prefix*/, const SeqNo &seq/*newSeq*/, const SeqNo &oldSeq/*oldSeq*/)
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070055 {
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070056 m_map[p] = seq.getSeq ();
57
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070058 // cout << instance << "\t";
59 // if (!oldSeq.isValid ())
60 // cout << "Inserted: " << p << " (" << seq << ")" << endl;
61 // else
62 // cout << "Updated: " << p << " ( " << oldSeq << ".." << seq << ")" << endl;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070063 }
64
65 void onRemove (const string &p/*prefix*/)
66 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070067 // cout << instance << "\tRemoved: " << p << endl;
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070068 m_map.erase (p);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070069 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070070
71 map<string, uint32_t> m_map;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070072};
73
Yingdi Yu280bb962014-01-30 09:52:43 -080074class TestCore
75{
76public:
77 TestCore(ndn::shared_ptr<boost::asio::io_service> ioService)
78 : m_ioService(ioService)
79 {
80 m_l[0] = 0;
81 m_l[1] = 0;
82
83 m_validator = ndn::make_shared<ndn::ValidatorNull>();
84 }
85
86 ~TestCore()
87 {
88 if(m_l[0] != 0)
89 delete m_l[0];
90
91 if(m_l[1] != 0)
92 delete m_l[1];
93 }
94
95 void
96 finish()
97 {
98 }
99
100 void
101 createSyncLogic(int index,
102 ndn::shared_ptr<Handler> h)
103 {
104 m_faces[index] = ndn::make_shared<ndn::Face>(m_ioService);
105 m_l[index] = new SyncLogic(ndn::Name("/bcast"),
106 m_validator, m_faces[index],
107 bind (&Handler::wrapper, &*h, _1),
108 bind (&Handler::onRemove, &*h, _1));
109 }
110
111 void
112 getOldDigestForOne()
113 {
114 m_oldDigest = m_l[0]->getRootDigest();
115 }
116
117 void
118 getNewDigestForOne()
119 {
120 m_newDigest = m_l[0]->getRootDigest();
121 }
122
123 void
124 addLocalNamesForOne(ndn::Name name, uint64_t session, uint64_t seq)
125 {
126 m_l[0]->addLocalNames(name, session, seq);
127 }
128
129 void
130 removeForOne(ndn::Name name)
131 {
132 m_l[0]->remove(name);
133 }
134
135 void
136 checkDigest()
137 {
138 BOOST_CHECK(m_oldDigest != m_newDigest);
139 }
140
141
142public:
143 ndn::shared_ptr<boost::asio::io_service> m_ioService;
144 SyncLogic* m_l[2];
145 ndn::shared_ptr<ndn::Face> m_faces[2];
146 ndn::shared_ptr<ndn::ValidatorNull> m_validator;
147 string m_oldDigest;
148 string m_newDigest;
149};
150
151void
152checkMapSize(ndn::shared_ptr<Handler> h, int size)
153{ BOOST_CHECK_EQUAL (h->m_map.size (), size); }
154
155
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700156BOOST_AUTO_TEST_CASE (SyncLogicTest)
157{
Yingdi Yu280bb962014-01-30 09:52:43 -0800158 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
159 ndn::Scheduler scheduler(*ioService);
160 TestCore testCore(ioService);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700161
Yingdi Yu280bb962014-01-30 09:52:43 -0800162 ndn::shared_ptr<Handler> h1 = ndn::make_shared<Handler>("1");
163 ndn::shared_ptr<Handler> h2 = ndn::make_shared<Handler>("2");
Yingdi Yu43e71612013-10-30 22:19:31 -0700164
Yingdi Yu280bb962014-01-30 09:52:43 -0800165 scheduler.scheduleEvent(ndn::time::seconds(0), ndn::bind(&TestCore::createSyncLogic, &testCore, 0, h1));
166 scheduler.scheduleEvent(ndn::time::seconds(0.1), ndn::bind(&TestCore::getOldDigestForOne, &testCore));
167 scheduler.scheduleEvent(ndn::time::seconds(0.2), ndn::bind(&TestCore::addLocalNamesForOne, &testCore, "/one", 1, 2));
168 scheduler.scheduleEvent(ndn::time::seconds(0.3), ndn::bind(&checkMapSize, h1, 0));
169 scheduler.scheduleEvent(ndn::time::seconds(0.4), ndn::bind(&TestCore::createSyncLogic, &testCore, 1, h2));
170 scheduler.scheduleEvent(ndn::time::seconds(0.5), ndn::bind(&checkMapSize, h1, 0));
171 scheduler.scheduleEvent(ndn::time::seconds(0.6), ndn::bind(&checkMapSize, h2, 1));
172 scheduler.scheduleEvent(ndn::time::seconds(0.7), ndn::bind(&TestCore::removeForOne, &testCore, "/one"));
173 scheduler.scheduleEvent(ndn::time::seconds(0.8), ndn::bind(&TestCore::getNewDigestForOne, &testCore));
174 scheduler.scheduleEvent(ndn::time::seconds(0.9), ndn::bind(&TestCore::checkDigest, &testCore));
175 scheduler.scheduleEvent(ndn::time::seconds(1.0), ndn::bind(&TestCore::finish, &testCore));
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700176
Yingdi Yu280bb962014-01-30 09:52:43 -0800177 ioService->run();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700178
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700179}