Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 4 | * |
| 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 Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 22 | #include "storage/sqlite-storage.hpp" |
| 23 | #include "storage/repo-storage.hpp" |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 24 | |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 25 | #include "command-fixture.hpp" |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 26 | #include "../repo-storage-fixture.hpp" |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 27 | #include "../dataset-fixtures.hpp" |
| 28 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 29 | #include <ndn-cxx/util/random.hpp> |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 30 | |
| 31 | #include <boost/mpl/vector.hpp> |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 32 | #include <boost/test/unit_test.hpp> |
| 33 | |
| 34 | namespace repo { |
| 35 | namespace tests { |
| 36 | |
| 37 | using ndn::time::milliseconds; |
| 38 | using ndn::time::seconds; |
| 39 | using ndn::EventId; |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 41 | // All the test cases in this test suite should be run at once. |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 42 | BOOST_AUTO_TEST_SUITE(TestBasicCommandInsertDelete) |
| 43 | |
| 44 | const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6}; |
| 45 | |
| 46 | template<class Dataset> |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 47 | class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 48 | { |
| 49 | public: |
| 50 | Fixture() |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 51 | : writeHandle(repoFace, *handle, keyChain, scheduler, validator) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 52 | , deleteHandle(repoFace, *handle, keyChain, scheduler, validator) |
| 53 | , insertFace(repoFace.getIoService()) |
| 54 | , deleteFace(repoFace.getIoService()) |
| 55 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 56 | 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 Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 65 | void |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 66 | 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 Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 81 | onInsertData(const Interest& interest, const Data& data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 82 | |
| 83 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 84 | onDeleteData(const Interest& interest, const Data& data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 85 | |
| 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 Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 99 | checkInsertOk(const Interest& interest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 100 | |
| 101 | void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 102 | checkDeleteOk(const Interest& interest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 103 | |
| 104 | public: |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 105 | WriteHandle writeHandle; |
| 106 | DeleteHandle deleteHandle; |
| 107 | Face insertFace; |
| 108 | Face deleteFace; |
| 109 | std::map<Name, EventId> insertEvents; |
| 110 | }; |
| 111 | |
| 112 | template<class T> void |
| 113 | Fixture<T>::onInsertInterest(const Interest& interest) |
| 114 | { |
| 115 | Data data(Name(interest.getName())); |
| 116 | data.setContent(content, sizeof(content)); |
| 117 | data.setFreshnessPeriod(milliseconds(0)); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 118 | keyChain.sign(data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 119 | insertFace.put(data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 120 | 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 Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 125 | // schedule an event 50ms later to check whether insert is Ok |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 126 | scheduler.scheduleEvent(milliseconds(500), |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 127 | bind(&Fixture<T>::checkInsertOk, this, interest)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 128 | |
| 129 | } |
| 130 | |
| 131 | |
| 132 | template<class T> void |
| 133 | Fixture<T>::onRegisterFailed(const std::string& reason) |
| 134 | { |
| 135 | BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason); |
| 136 | } |
| 137 | |
| 138 | template<class T> void |
| 139 | Fixture<T>::delayedInterest() |
| 140 | { |
| 141 | BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong"); |
| 142 | } |
| 143 | |
| 144 | template<class T> void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 145 | Fixture<T>::onInsertData(const Interest& interest, const Data& data) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 146 | { |
| 147 | RepoCommandResponse response; |
| 148 | response.wireDecode(data.getContent().blockFromValue()); |
| 149 | int statusCode = response.getStatusCode(); |
| 150 | BOOST_CHECK_EQUAL(statusCode, 100); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 151 | // std::cout<<"statuse code of insert name = "<<response.getName()<<std::endl; |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | template<class T> void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 155 | Fixture<T>::onDeleteData(const Interest& interest, const Data& data) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 156 | { |
| 157 | RepoCommandResponse response; |
| 158 | response.wireDecode(data.getContent().blockFromValue()); |
| 159 | int statusCode = response.getStatusCode(); |
| 160 | BOOST_CHECK_EQUAL(statusCode, 200); |
| 161 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 162 | //schedlute an event to check whether delete is Ok. |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 163 | scheduler.scheduleEvent(milliseconds(100), |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 164 | bind(&Fixture<T>::checkDeleteOk, this, interest)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | template<class T> void |
| 168 | Fixture<T>::onInsertTimeout(const Interest& interest) |
| 169 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 170 | BOOST_ERROR("Insert command timeout"); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | template<class T> void |
| 174 | Fixture<T>::onDeleteTimeout(const Interest& interest) |
| 175 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 176 | BOOST_ERROR("Delete command timeout"); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | template<class T> void |
| 180 | Fixture<T>::sendInsertInterest(const Interest& insertInterest) |
| 181 | { |
| 182 | insertFace.expressInterest(insertInterest, |
| 183 | bind(&Fixture<T>::onInsertData, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 184 | bind(&Fixture<T>::onInsertTimeout, this, _1), // Nack |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 185 | bind(&Fixture<T>::onInsertTimeout, this, _1)); |
| 186 | } |
| 187 | |
| 188 | template<class T> void |
| 189 | Fixture<T>::sendDeleteInterest(const Interest& deleteInterest) |
| 190 | { |
| 191 | deleteFace.expressInterest(deleteInterest, |
| 192 | bind(&Fixture<T>::onDeleteData, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 193 | bind(&Fixture<T>::onDeleteTimeout, this, _1), // Nack |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 194 | bind(&Fixture<T>::onDeleteTimeout, this, _1)); |
| 195 | } |
| 196 | |
| 197 | template<class T> void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 198 | Fixture<T>::checkInsertOk(const Interest& interest) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 199 | { |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 200 | BOOST_TEST_MESSAGE(interest); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 201 | 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 Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | template<class T> void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 212 | Fixture<T>::checkDeleteOk(const Interest& interest) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 213 | { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 214 | shared_ptr<Data> data = handle->readData(interest); |
| 215 | BOOST_CHECK_EQUAL(data, shared_ptr<Data>()); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 218 | template<class T> void |
| 219 | Fixture<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 Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 227 | .appendNumber(ndn::random::generateWord64())); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 228 | |
| 229 | insertCommandName.append(insertParameter.wireEncode()); |
| 230 | Interest insertInterest(insertCommandName); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 231 | keyChain.sign(insertInterest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 232 | //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 Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 244 | ndn::RegisterPrefixSuccessCallback(), |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 245 | bind(&Fixture<T>::onRegisterFailed, this, _2)); |
| 246 | timeCount++; |
| 247 | } |
| 248 | } |
| 249 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 250 | template<class T> void |
| 251 | Fixture<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 Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 258 | deleteParameter.setProcessId(ndn::random::generateWord64()); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 259 | deleteParameter.setName((*i)->getName()); |
| 260 | deleteCommandName.append(deleteParameter.wireEncode()); |
| 261 | Interest deleteInterest(deleteCommandName); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 262 | keyChain.sign(deleteInterest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 263 | scheduler.scheduleEvent(milliseconds(4000 + timeCount * 50), |
| 264 | bind(&Fixture<T>::sendDeleteInterest, this, deleteInterest)); |
| 265 | timeCount++; |
| 266 | } |
| 267 | } |
| 268 | |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 269 | typedef boost::mpl::vector< BasicDataset, |
| 270 | FetchByPrefixDataset, |
| 271 | BasicChildSelectorDataset, |
| 272 | ExtendedChildSelectorDataset, |
| 273 | SamePrefixDataset<10> > Datasets; |
| 274 | |
| 275 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertDelete, T, Datasets, Fixture<T>) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 276 | { |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 277 | // 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 Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 283 | this->repoFace.processEvents(seconds(30)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | BOOST_AUTO_TEST_SUITE_END() |
| 287 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 288 | } // namespace tests |
| 289 | } // namespace repo |