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