Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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> |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 30 | #include <boost/preprocessor/comparison/not_equal.hpp> |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 31 | #include <boost/test/unit_test.hpp> |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 32 | #include <fstream> |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 33 | |
| 34 | namespace repo { |
| 35 | namespace tests { |
| 36 | |
| 37 | using ndn::time::milliseconds; |
| 38 | using ndn::time::seconds; |
| 39 | using ndn::EventId; |
| 40 | namespace random=ndn::random; |
| 41 | |
| 42 | //All the test cases in this test suite should be run at once. |
| 43 | BOOST_AUTO_TEST_SUITE(TestBasicCommandInsertDelete) |
| 44 | |
| 45 | const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6}; |
| 46 | |
| 47 | template<class Dataset> |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 48 | class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 49 | { |
| 50 | public: |
| 51 | Fixture() |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 52 | : writeHandle(repoFace, *handle, keyChain, scheduler, validator) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 53 | , deleteHandle(repoFace, *handle, keyChain, scheduler, validator) |
| 54 | , insertFace(repoFace.getIoService()) |
| 55 | , deleteFace(repoFace.getIoService()) |
| 56 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame^] | 57 | Name cmdPrefix("/repo/command"); |
| 58 | repoFace.registerPrefix(cmdPrefix, nullptr, |
| 59 | [] (const Name& cmdPrefix, const std::string& reason) { |
| 60 | BOOST_FAIL("Command prefix registration error: " << reason); |
| 61 | }); |
| 62 | writeHandle.listen(cmdPrefix); |
| 63 | deleteHandle.listen(cmdPrefix); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 66 | void |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 67 | scheduleInsertEvent(); |
| 68 | |
| 69 | void |
| 70 | scheduleDeleteEvent(); |
| 71 | |
| 72 | void |
| 73 | onInsertInterest(const Interest& interest); |
| 74 | |
| 75 | void |
| 76 | onRegisterFailed(const std::string& reason); |
| 77 | |
| 78 | void |
| 79 | delayedInterest(); |
| 80 | |
| 81 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 82 | onInsertData(const Interest& interest, const Data& data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 83 | |
| 84 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 85 | onDeleteData(const Interest& interest, const Data& data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 86 | |
| 87 | void |
| 88 | onInsertTimeout(const Interest& interest); |
| 89 | |
| 90 | void |
| 91 | onDeleteTimeout(const Interest& interest); |
| 92 | |
| 93 | void |
| 94 | sendInsertInterest(const Interest& interest); |
| 95 | |
| 96 | void |
| 97 | sendDeleteInterest(const Interest& deleteInterest); |
| 98 | |
| 99 | void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 100 | checkInsertOk(const Interest& interest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 101 | |
| 102 | void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 103 | checkDeleteOk(const Interest& interest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 104 | |
| 105 | public: |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 106 | WriteHandle writeHandle; |
| 107 | DeleteHandle deleteHandle; |
| 108 | Face insertFace; |
| 109 | Face deleteFace; |
| 110 | std::map<Name, EventId> insertEvents; |
| 111 | }; |
| 112 | |
| 113 | template<class T> void |
| 114 | Fixture<T>::onInsertInterest(const Interest& interest) |
| 115 | { |
| 116 | Data data(Name(interest.getName())); |
| 117 | data.setContent(content, sizeof(content)); |
| 118 | data.setFreshnessPeriod(milliseconds(0)); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 119 | keyChain.sign(data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 120 | insertFace.put(data); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 121 | std::map<Name, EventId>::iterator event = insertEvents.find(interest.getName()); |
| 122 | if (event != insertEvents.end()) { |
| 123 | scheduler.cancelEvent(event->second); |
| 124 | insertEvents.erase(event); |
| 125 | } |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 126 | // schedule an event 50ms later to check whether insert is Ok |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 127 | scheduler.scheduleEvent(milliseconds(500), |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 128 | bind(&Fixture<T>::checkInsertOk, this, interest)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 129 | |
| 130 | } |
| 131 | |
| 132 | |
| 133 | template<class T> void |
| 134 | Fixture<T>::onRegisterFailed(const std::string& reason) |
| 135 | { |
| 136 | BOOST_ERROR("ERROR: Failed to register prefix in local hub's daemon" + reason); |
| 137 | } |
| 138 | |
| 139 | template<class T> void |
| 140 | Fixture<T>::delayedInterest() |
| 141 | { |
| 142 | BOOST_ERROR("Fetching interest does not come. It may be satisfied in CS or something is wrong"); |
| 143 | } |
| 144 | |
| 145 | template<class T> void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 146 | Fixture<T>::onInsertData(const Interest& interest, const Data& data) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 147 | { |
| 148 | RepoCommandResponse response; |
| 149 | response.wireDecode(data.getContent().blockFromValue()); |
| 150 | int statusCode = response.getStatusCode(); |
| 151 | BOOST_CHECK_EQUAL(statusCode, 100); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 152 | // std::cout<<"statuse code of insert name = "<<response.getName()<<std::endl; |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | template<class T> void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 156 | Fixture<T>::onDeleteData(const Interest& interest, const Data& data) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 157 | { |
| 158 | RepoCommandResponse response; |
| 159 | response.wireDecode(data.getContent().blockFromValue()); |
| 160 | int statusCode = response.getStatusCode(); |
| 161 | BOOST_CHECK_EQUAL(statusCode, 200); |
| 162 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 163 | //schedlute an event to check whether delete is Ok. |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 164 | scheduler.scheduleEvent(milliseconds(100), |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 165 | bind(&Fixture<T>::checkDeleteOk, this, interest)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | template<class T> void |
| 169 | Fixture<T>::onInsertTimeout(const Interest& interest) |
| 170 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame^] | 171 | BOOST_ERROR("Insert command timeout"); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | template<class T> void |
| 175 | Fixture<T>::onDeleteTimeout(const Interest& interest) |
| 176 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame^] | 177 | BOOST_ERROR("Delete command timeout"); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | template<class T> void |
| 181 | Fixture<T>::sendInsertInterest(const Interest& insertInterest) |
| 182 | { |
| 183 | insertFace.expressInterest(insertInterest, |
| 184 | bind(&Fixture<T>::onInsertData, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 185 | bind(&Fixture<T>::onInsertTimeout, this, _1), // Nack |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 186 | bind(&Fixture<T>::onInsertTimeout, this, _1)); |
| 187 | } |
| 188 | |
| 189 | template<class T> void |
| 190 | Fixture<T>::sendDeleteInterest(const Interest& deleteInterest) |
| 191 | { |
| 192 | deleteFace.expressInterest(deleteInterest, |
| 193 | bind(&Fixture<T>::onDeleteData, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 194 | bind(&Fixture<T>::onDeleteTimeout, this, _1), // Nack |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 195 | bind(&Fixture<T>::onDeleteTimeout, this, _1)); |
| 196 | } |
| 197 | |
| 198 | template<class T> void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 199 | Fixture<T>::checkInsertOk(const Interest& interest) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 200 | { |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 201 | BOOST_TEST_MESSAGE(interest); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 202 | shared_ptr<Data> data = handle->readData(interest); |
| 203 | if (data) { |
| 204 | int rc = memcmp(data->getContent().value(), content, sizeof(content)); |
| 205 | BOOST_CHECK_EQUAL(rc, 0); |
| 206 | } |
| 207 | else { |
| 208 | std::cerr<<"Check Insert Failed"<<std::endl; |
| 209 | } |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | template<class T> void |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 213 | Fixture<T>::checkDeleteOk(const Interest& interest) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 214 | { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 215 | shared_ptr<Data> data = handle->readData(interest); |
| 216 | BOOST_CHECK_EQUAL(data, shared_ptr<Data>()); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 219 | template<class T> void |
| 220 | Fixture<T>::scheduleInsertEvent() |
| 221 | { |
| 222 | int timeCount = 1; |
| 223 | for (typename T::DataContainer::iterator i = this->data.begin(); |
| 224 | i != this->data.end(); ++i) { |
| 225 | Name insertCommandName("/repo/command/insert"); |
| 226 | RepoCommandParameter insertParameter; |
| 227 | insertParameter.setName(Name((*i)->getName()) |
| 228 | .appendNumber(random::generateWord64())); |
| 229 | |
| 230 | insertCommandName.append(insertParameter.wireEncode()); |
| 231 | Interest insertInterest(insertCommandName); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 232 | keyChain.sign(insertInterest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 233 | //schedule a job to express insertInterest every 50ms |
| 234 | scheduler.scheduleEvent(milliseconds(timeCount * 50 + 1000), |
| 235 | bind(&Fixture<T>::sendInsertInterest, this, insertInterest)); |
| 236 | //schedule what to do when interest timeout |
| 237 | |
| 238 | EventId delayEventId = scheduler.scheduleEvent(milliseconds(5000 + timeCount * 50), |
| 239 | bind(&Fixture<T>::delayedInterest, this)); |
| 240 | insertEvents[insertParameter.getName()] = delayEventId; |
| 241 | |
| 242 | //The delayEvent will be canceled in onInsertInterest |
| 243 | insertFace.setInterestFilter(insertParameter.getName(), |
| 244 | bind(&Fixture<T>::onInsertInterest, this, _2), |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 245 | ndn::RegisterPrefixSuccessCallback(), |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 246 | bind(&Fixture<T>::onRegisterFailed, this, _2)); |
| 247 | timeCount++; |
| 248 | } |
| 249 | } |
| 250 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 251 | template<class T> void |
| 252 | Fixture<T>::scheduleDeleteEvent() |
| 253 | { |
| 254 | int timeCount = 1; |
| 255 | for (typename T::DataContainer::iterator i = this->data.begin(); |
| 256 | i != this->data.end(); ++i) { |
| 257 | Name deleteCommandName("/repo/command/delete"); |
| 258 | RepoCommandParameter deleteParameter; |
| 259 | static boost::random::mt19937_64 gen; |
| 260 | static boost::random::uniform_int_distribution<uint64_t> dist(0, 0xFFFFFFFFFFFFFFFFLL); |
| 261 | deleteParameter.setProcessId(dist(gen)); |
| 262 | deleteParameter.setName((*i)->getName()); |
| 263 | deleteCommandName.append(deleteParameter.wireEncode()); |
| 264 | Interest deleteInterest(deleteCommandName); |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 265 | keyChain.sign(deleteInterest); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 266 | scheduler.scheduleEvent(milliseconds(4000 + timeCount * 50), |
| 267 | bind(&Fixture<T>::sendDeleteInterest, this, deleteInterest)); |
| 268 | timeCount++; |
| 269 | } |
| 270 | } |
| 271 | |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 272 | typedef boost::mpl::vector< BasicDataset, |
| 273 | FetchByPrefixDataset, |
| 274 | BasicChildSelectorDataset, |
| 275 | ExtendedChildSelectorDataset, |
| 276 | SamePrefixDataset<10> > Datasets; |
| 277 | |
| 278 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertDelete, T, Datasets, Fixture<T>) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 279 | { |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 280 | // schedule events |
| 281 | this->scheduler.scheduleEvent(seconds(0), |
| 282 | bind(&Fixture<T>::scheduleInsertEvent, this)); |
| 283 | this->scheduler.scheduleEvent(seconds(10), |
| 284 | bind(&Fixture<T>::scheduleDeleteEvent, this)); |
| 285 | |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 286 | this->repoFace.processEvents(seconds(30)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | BOOST_AUTO_TEST_SUITE_END() |
| 290 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 291 | } // namespace tests |
| 292 | } // namespace repo |