blob: ff4b8665d27522fcafe750480e9f6f217d5dda9f [file] [log] [blame]
Shuo Chenca329182014-03-19 18:05:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento49f3a5f2017-09-23 01:36:33 -04002/*
Alexander Afanasyev42290b22017-03-09 12:58:29 -08003 * Copyright (c) 2014-2017, Regents of the University of California.
Shuo Chenca329182014-03-19 18:05:18 -07004 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng 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,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * repo-ng 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.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "handles/write-handle.hpp"
21#include "handles/delete-handle.hpp"
Weiqi Shif0330d52014-07-09 10:54:27 -070022#include "storage/sqlite-storage.hpp"
23#include "storage/repo-storage.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070024
Junxiao Shi047a6fb2017-06-08 16:16:05 +000025#include "command-fixture.hpp"
Weiqi Shif0330d52014-07-09 10:54:27 -070026#include "../repo-storage-fixture.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070027#include "../dataset-fixtures.hpp"
28
Shuo Chen028dcd32014-06-21 16:36:44 +080029#include <ndn-cxx/util/random.hpp>
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040030
31#include <boost/mpl/vector.hpp>
Shuo Chenca329182014-03-19 18:05:18 -070032#include <boost/test/unit_test.hpp>
33
34namespace repo {
35namespace tests {
36
37using ndn::time::milliseconds;
38using ndn::time::seconds;
39using ndn::EventId;
Shuo Chenca329182014-03-19 18:05:18 -070040
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040041// All the test cases in this test suite should be run at once.
Shuo Chenca329182014-03-19 18:05:18 -070042BOOST_AUTO_TEST_SUITE(TestBasicCommandInsertDelete)
43
44const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
45
46template<class Dataset>
Junxiao Shi047a6fb2017-06-08 16:16:05 +000047class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070048{
49public:
50 Fixture()
Junxiao Shi047a6fb2017-06-08 16:16:05 +000051 : writeHandle(repoFace, *handle, keyChain, scheduler, validator)
Shuo Chenca329182014-03-19 18:05:18 -070052 , deleteHandle(repoFace, *handle, keyChain, scheduler, validator)
53 , insertFace(repoFace.getIoService())
54 , deleteFace(repoFace.getIoService())
55 {
Junxiao Shi2b7b8312017-06-16 03:43:24 +000056 Name cmdPrefix("/repo/command");
57 repoFace.registerPrefix(cmdPrefix, nullptr,
58 [] (const Name& cmdPrefix, const std::string& reason) {
59 BOOST_FAIL("Command prefix registration error: " << reason);
60 });
61 writeHandle.listen(cmdPrefix);
62 deleteHandle.listen(cmdPrefix);
Shuo Chenca329182014-03-19 18:05:18 -070063 }
64
Shuo Chen028dcd32014-06-21 16:36:44 +080065 void
Shuo Chenca329182014-03-19 18:05:18 -070066 scheduleInsertEvent();
67
68 void
69 scheduleDeleteEvent();
70
71 void
72 onInsertInterest(const Interest& interest);
73
74 void
75 onRegisterFailed(const std::string& reason);
76
77 void
78 delayedInterest();
79
80 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080081 onInsertData(const Interest& interest, const Data& data);
Shuo Chenca329182014-03-19 18:05:18 -070082
83 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080084 onDeleteData(const Interest& interest, const Data& data);
Shuo Chenca329182014-03-19 18:05:18 -070085
86 void
87 onInsertTimeout(const Interest& interest);
88
89 void
90 onDeleteTimeout(const Interest& interest);
91
92 void
93 sendInsertInterest(const Interest& interest);
94
95 void
96 sendDeleteInterest(const Interest& deleteInterest);
97
98 void
Shuo Chen028dcd32014-06-21 16:36:44 +080099 checkInsertOk(const Interest& interest);
Shuo Chenca329182014-03-19 18:05:18 -0700100
101 void
Shuo Chen028dcd32014-06-21 16:36:44 +0800102 checkDeleteOk(const Interest& interest);
Shuo Chenca329182014-03-19 18:05:18 -0700103
104public:
Shuo Chenca329182014-03-19 18:05:18 -0700105 WriteHandle writeHandle;
106 DeleteHandle deleteHandle;
107 Face insertFace;
108 Face deleteFace;
109 std::map<Name, EventId> insertEvents;
110};
111
112template<class T> void
113Fixture<T>::onInsertInterest(const Interest& interest)
114{
115 Data data(Name(interest.getName()));
116 data.setContent(content, sizeof(content));
117 data.setFreshnessPeriod(milliseconds(0));
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000118 keyChain.sign(data);
Shuo Chenca329182014-03-19 18:05:18 -0700119 insertFace.put(data);
Shuo Chenca329182014-03-19 18:05:18 -0700120 std::map<Name, EventId>::iterator event = insertEvents.find(interest.getName());
121 if (event != insertEvents.end()) {
122 scheduler.cancelEvent(event->second);
123 insertEvents.erase(event);
124 }
Shuo Chen028dcd32014-06-21 16:36:44 +0800125 // schedule an event 50ms later to check whether insert is Ok
Weiqi Shif0330d52014-07-09 10:54:27 -0700126 scheduler.scheduleEvent(milliseconds(500),
Shuo Chen028dcd32014-06-21 16:36:44 +0800127 bind(&Fixture<T>::checkInsertOk, this, interest));
Shuo Chenca329182014-03-19 18:05:18 -0700128
129}
130
131
132template<class T> void
133Fixture<T>::onRegisterFailed(const std::string& reason)
134{
135 BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason);
136}
137
138template<class T> void
139Fixture<T>::delayedInterest()
140{
141 BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong");
142}
143
144template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800145Fixture<T>::onInsertData(const Interest& interest, const Data& data)
Shuo Chenca329182014-03-19 18:05:18 -0700146{
147 RepoCommandResponse response;
148 response.wireDecode(data.getContent().blockFromValue());
149 int statusCode = response.getStatusCode();
150 BOOST_CHECK_EQUAL(statusCode, 100);
Weiqi Shif0330d52014-07-09 10:54:27 -0700151 // std::cout<<"statuse code of insert name = "<<response.getName()<<std::endl;
Shuo Chenca329182014-03-19 18:05:18 -0700152}
153
154template<class T> void
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800155Fixture<T>::onDeleteData(const Interest& interest, const Data& data)
Shuo Chenca329182014-03-19 18:05:18 -0700156{
157 RepoCommandResponse response;
158 response.wireDecode(data.getContent().blockFromValue());
159 int statusCode = response.getStatusCode();
160 BOOST_CHECK_EQUAL(statusCode, 200);
161
Shuo Chen028dcd32014-06-21 16:36:44 +0800162 //schedlute an event to check whether delete is Ok.
Shuo Chenca329182014-03-19 18:05:18 -0700163 scheduler.scheduleEvent(milliseconds(100),
Shuo Chen028dcd32014-06-21 16:36:44 +0800164 bind(&Fixture<T>::checkDeleteOk, this, interest));
Shuo Chenca329182014-03-19 18:05:18 -0700165}
166
167template<class T> void
168Fixture<T>::onInsertTimeout(const Interest& interest)
169{
Junxiao Shi2b7b8312017-06-16 03:43:24 +0000170 BOOST_ERROR("Insert command timeout");
Shuo Chenca329182014-03-19 18:05:18 -0700171}
172
173template<class T> void
174Fixture<T>::onDeleteTimeout(const Interest& interest)
175{
Junxiao Shi2b7b8312017-06-16 03:43:24 +0000176 BOOST_ERROR("Delete command timeout");
Shuo Chenca329182014-03-19 18:05:18 -0700177}
178
179template<class T> void
180Fixture<T>::sendInsertInterest(const Interest& insertInterest)
181{
182 insertFace.expressInterest(insertInterest,
183 bind(&Fixture<T>::onInsertData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800184 bind(&Fixture<T>::onInsertTimeout, this, _1), // Nack
Shuo Chenca329182014-03-19 18:05:18 -0700185 bind(&Fixture<T>::onInsertTimeout, this, _1));
186}
187
188template<class T> void
189Fixture<T>::sendDeleteInterest(const Interest& deleteInterest)
190{
191 deleteFace.expressInterest(deleteInterest,
192 bind(&Fixture<T>::onDeleteData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800193 bind(&Fixture<T>::onDeleteTimeout, this, _1), // Nack
Shuo Chenca329182014-03-19 18:05:18 -0700194 bind(&Fixture<T>::onDeleteTimeout, this, _1));
195}
196
197template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800198Fixture<T>::checkInsertOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700199{
Shuo Chenca329182014-03-19 18:05:18 -0700200 BOOST_TEST_MESSAGE(interest);
Weiqi Shif0330d52014-07-09 10:54:27 -0700201 shared_ptr<Data> data = handle->readData(interest);
202 if (data) {
203 int rc = memcmp(data->getContent().value(), content, sizeof(content));
204 BOOST_CHECK_EQUAL(rc, 0);
205 }
206 else {
207 std::cerr<<"Check Insert Failed"<<std::endl;
208 }
Shuo Chenca329182014-03-19 18:05:18 -0700209}
210
211template<class T> void
Shuo Chen028dcd32014-06-21 16:36:44 +0800212Fixture<T>::checkDeleteOk(const Interest& interest)
Shuo Chenca329182014-03-19 18:05:18 -0700213{
Weiqi Shif0330d52014-07-09 10:54:27 -0700214 shared_ptr<Data> data = handle->readData(interest);
215 BOOST_CHECK_EQUAL(data, shared_ptr<Data>());
Shuo Chenca329182014-03-19 18:05:18 -0700216}
217
Shuo Chenca329182014-03-19 18:05:18 -0700218template<class T> void
219Fixture<T>::scheduleInsertEvent()
220{
221 int timeCount = 1;
222 for (typename T::DataContainer::iterator i = this->data.begin();
223 i != this->data.end(); ++i) {
224 Name insertCommandName("/repo/command/insert");
225 RepoCommandParameter insertParameter;
226 insertParameter.setName(Name((*i)->getName())
Davide Pesavento49f3a5f2017-09-23 01:36:33 -0400227 .appendNumber(ndn::random::generateWord64()));
Shuo Chenca329182014-03-19 18:05:18 -0700228
229 insertCommandName.append(insertParameter.wireEncode());
230 Interest insertInterest(insertCommandName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000231 keyChain.sign(insertInterest);
Shuo Chenca329182014-03-19 18:05:18 -0700232 //schedule a job to express insertInterest every 50ms
233 scheduler.scheduleEvent(milliseconds(timeCount * 50 + 1000),
234 bind(&Fixture<T>::sendInsertInterest, this, insertInterest));
235 //schedule what to do when interest timeout
236
237 EventId delayEventId = scheduler.scheduleEvent(milliseconds(5000 + timeCount * 50),
238 bind(&Fixture<T>::delayedInterest, this));
239 insertEvents[insertParameter.getName()] = delayEventId;
240
241 //The delayEvent will be canceled in onInsertInterest
242 insertFace.setInterestFilter(insertParameter.getName(),
243 bind(&Fixture<T>::onInsertInterest, this, _2),
Wentao Shang91fb4f22014-05-20 10:55:22 -0700244 ndn::RegisterPrefixSuccessCallback(),
Shuo Chenca329182014-03-19 18:05:18 -0700245 bind(&Fixture<T>::onRegisterFailed, this, _2));
246 timeCount++;
247 }
248}
249
Shuo Chenca329182014-03-19 18:05:18 -0700250template<class T> void
251Fixture<T>::scheduleDeleteEvent()
252{
253 int timeCount = 1;
254 for (typename T::DataContainer::iterator i = this->data.begin();
255 i != this->data.end(); ++i) {
256 Name deleteCommandName("/repo/command/delete");
257 RepoCommandParameter deleteParameter;
Davide Pesavento49f3a5f2017-09-23 01:36:33 -0400258 deleteParameter.setProcessId(ndn::random::generateWord64());
Shuo Chenca329182014-03-19 18:05:18 -0700259 deleteParameter.setName((*i)->getName());
260 deleteCommandName.append(deleteParameter.wireEncode());
261 Interest deleteInterest(deleteCommandName);
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000262 keyChain.sign(deleteInterest);
Shuo Chenca329182014-03-19 18:05:18 -0700263 scheduler.scheduleEvent(milliseconds(4000 + timeCount * 50),
264 bind(&Fixture<T>::sendDeleteInterest, this, deleteInterest));
265 timeCount++;
266 }
267}
268
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700269typedef boost::mpl::vector< BasicDataset,
270 FetchByPrefixDataset,
271 BasicChildSelectorDataset,
272 ExtendedChildSelectorDataset,
273 SamePrefixDataset<10> > Datasets;
274
275BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertDelete, T, Datasets, Fixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700276{
Shuo Chenca329182014-03-19 18:05:18 -0700277 // schedule events
278 this->scheduler.scheduleEvent(seconds(0),
279 bind(&Fixture<T>::scheduleInsertEvent, this));
280 this->scheduler.scheduleEvent(seconds(10),
281 bind(&Fixture<T>::scheduleDeleteEvent, this));
282
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000283 this->repoFace.processEvents(seconds(30));
Shuo Chenca329182014-03-19 18:05:18 -0700284}
285
286BOOST_AUTO_TEST_SUITE_END()
287
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800288} // namespace tests
289} // namespace repo