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 | /* |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, 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 | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/util/time.hpp> |
| 31 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 32 | namespace repo { |
| 33 | namespace tests { |
| 34 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 35 | BOOST_AUTO_TEST_SUITE(TestBasicInterestRead) |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 36 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 37 | const uint8_t CONTENT[] = {3, 1, 4, 1, 5, 9, 2, 6}; |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 38 | |
| 39 | template<class Dataset> |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 40 | class BasicInterestReadFixture : public RepoStorageFixture, public Dataset |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | BasicInterestReadFixture() |
| 44 | : scheduler(repoFace.getIoService()) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 45 | , readHandle(repoFace, *handle, 0) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 46 | , readFace(repoFace.getIoService()) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | ~BasicInterestReadFixture() |
| 51 | { |
| 52 | repoFace.getIoService().stop(); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | startListen() |
| 57 | { |
| 58 | readHandle.listen("/"); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | scheduleReadEvent() |
| 63 | { |
| 64 | int timeCount = 1; |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 65 | for (auto i = this->data.begin(); i != this->data.end(); ++i) { |
| 66 | // First insert a data into database |
| 67 | (*i)->setContent(CONTENT); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 68 | (*i)->setFreshnessPeriod(36000_ms); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 69 | keyChain.sign(**i); |
| 70 | bool rc = handle->insertData(**i); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(rc, true); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 72 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 73 | Interest readInterest((*i)->getName()); |
| 74 | readInterest.setMustBeFresh(true); |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 75 | scheduler.schedule(ndn::time::milliseconds(timeCount * 50), |
| 76 | std::bind(&BasicInterestReadFixture<Dataset>::sendInterest, this, readInterest)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 77 | timeCount++; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 82 | onReadData(const ndn::Interest&, const ndn::Data& data) const |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 83 | { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 84 | BOOST_TEST(data.getContent().value_bytes() == CONTENT, boost::test_tools::per_element()); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 88 | onReadTimeout(const ndn::Interest& interest) const |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 89 | { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 90 | BOOST_ERROR("Read timeout " << interest.getName()); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 94 | onReadNack(const ndn::Interest& interest, const ndn::lp::Nack& nack) const |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 95 | { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 96 | BOOST_ERROR("Read nack " << interest.getName() << " " << nack.getReason()); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 100 | sendInterest(const ndn::Interest& interest) |
| 101 | { |
| 102 | readFace.expressInterest(interest, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 103 | std::bind(&BasicInterestReadFixture::onReadData, this, _1, _2), |
| 104 | std::bind(&BasicInterestReadFixture::onReadNack, this, _1, _2), |
| 105 | std::bind(&BasicInterestReadFixture::onReadTimeout, this, _1)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public: |
| 109 | ndn::Face repoFace; |
| 110 | ndn::KeyChain keyChain; |
| 111 | ndn::Scheduler scheduler; |
| 112 | ReadHandle readHandle; |
| 113 | ndn::Face readFace; |
| 114 | }; |
| 115 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 116 | using Datasets = boost::mpl::vector<BasicDataset, |
| 117 | FetchByPrefixDataset, |
| 118 | SamePrefixDataset<10>>; |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 119 | |
| 120 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 121 | { |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 122 | this->startListen(); |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 123 | this->scheduler.schedule(1_s, [this] { this->scheduleReadEvent(); }); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 124 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 125 | this->repoFace.processEvents(20_s); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_SUITE_END() |
| 129 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 130 | } // namespace tests |
| 131 | } // namespace repo |