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 | /* |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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/read-handle.hpp" |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 21 | #include "storage/sqlite-storage.hpp" |
| 22 | #include "storage/repo-storage.hpp" |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 23 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 24 | #include "../repo-storage-fixture.hpp" |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 25 | #include "../dataset-fixtures.hpp" |
| 26 | |
Eric Newberry | b16edda | 2017-12-23 17:54:47 -0700 | [diff] [blame] | 27 | #include <boost/asio/io_service.hpp> |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 28 | #include <boost/test/unit_test.hpp> |
| 29 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 30 | #include <ndn-cxx/util/logger.hpp> |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 31 | #include <ndn-cxx/util/time.hpp> |
| 32 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 33 | namespace repo { |
| 34 | namespace tests { |
| 35 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 36 | NDN_LOG_INIT(repo.tests.read); |
| 37 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(TestBasicInerestRead) |
| 39 | |
| 40 | const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6}; |
| 41 | |
| 42 | template<class Dataset> |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 43 | class BasicInterestReadFixture : public RepoStorageFixture, public Dataset |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 44 | { |
| 45 | public: |
| 46 | BasicInterestReadFixture() |
| 47 | : scheduler(repoFace.getIoService()) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 48 | , readHandle(repoFace, *handle, 0) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 49 | , readFace(repoFace.getIoService()) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | ~BasicInterestReadFixture() |
| 54 | { |
| 55 | repoFace.getIoService().stop(); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | startListen() |
| 60 | { |
| 61 | readHandle.listen("/"); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | scheduleReadEvent() |
| 66 | { |
| 67 | int timeCount = 1; |
| 68 | for (typename Dataset::DataContainer::iterator i = this->data.begin(); |
| 69 | i != this->data.end(); ++i) { |
| 70 | //First insert a data into database; |
| 71 | (*i)->setContent(content, sizeof(content)); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 72 | (*i)->setFreshnessPeriod(36000_ms); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 73 | keyChain.sign(**i); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 74 | NDN_LOG_DEBUG(**i); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 75 | bool rc = handle->insertData(**i); |
| 76 | |
| 77 | BOOST_CHECK_EQUAL(rc, true); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 78 | NDN_LOG_DEBUG("Inserted Data " << (**i).getName()); |
| 79 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 80 | Interest readInterest((*i)->getName()); |
| 81 | readInterest.setMustBeFresh(true); |
| 82 | scheduler.scheduleEvent(ndn::time::milliseconds(timeCount * 50), |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 83 | std::bind(&BasicInterestReadFixture<Dataset>::sendInterest, this, |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 84 | readInterest)); |
| 85 | timeCount++; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 90 | onReadData(const ndn::Interest& interest, const ndn::Data& data) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 91 | { |
| 92 | int rc = memcmp(data.getContent().value(), content, sizeof(content)); |
| 93 | BOOST_CHECK_EQUAL(rc, 0); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
| 97 | onReadTimeout(const ndn::Interest& interest) |
| 98 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 99 | NDN_LOG_DEBUG("Timed out " << interest.getName()); |
| 100 | BOOST_ERROR("Insert or read not successfull"); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 104 | onReadNack(const ndn::Interest& interest, const ndn::lp::Nack& nack) |
| 105 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 106 | NDN_LOG_DEBUG("Nacked " << interest.getName() << nack.getReason()); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 107 | BOOST_ERROR("Read nacked"); |
| 108 | } |
| 109 | |
| 110 | void |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 111 | sendInterest(const ndn::Interest& interest) |
| 112 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 113 | NDN_LOG_DEBUG("Sending Interest " << interest.getName()); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 114 | readFace.expressInterest(interest, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 115 | std::bind(&BasicInterestReadFixture::onReadData, this, _1, _2), |
| 116 | std::bind(&BasicInterestReadFixture::onReadNack, this, _1, _2), |
| 117 | std::bind(&BasicInterestReadFixture::onReadTimeout, this, _1)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public: |
| 121 | ndn::Face repoFace; |
| 122 | ndn::KeyChain keyChain; |
| 123 | ndn::Scheduler scheduler; |
| 124 | ReadHandle readHandle; |
| 125 | ndn::Face readFace; |
| 126 | }; |
| 127 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 128 | |
| 129 | using Datasets = boost::mpl::vector<BasicDataset, |
| 130 | FetchByPrefixDataset, |
| 131 | SamePrefixDataset<10>>; |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 132 | |
| 133 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 134 | { |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 135 | this->startListen(); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 136 | this->scheduler.scheduleEvent(1_s, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 137 | std::bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 138 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 139 | this->repoFace.processEvents(20_s); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 140 | |
| 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_SUITE_END() |
| 144 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 145 | } // namespace tests |
| 146 | } // namespace repo |