blob: c2b6a17d222ebe4bfeab51b4bb87d760354ce39d [file] [log] [blame]
Shuo Chenca329182014-03-19 18:05:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento49f3a5f2017-09-23 01:36:33 -04002/*
Davide Pesaventod8521aa2023-09-17 14:21:27 -04003 * Copyright (c) 2014-2023, Regents of the University of California.
Shuo Chenca329182014-03-19 18:05:18 -07004 *
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 Shif0330d52014-07-09 10:54:27 -070021#include "storage/sqlite-storage.hpp"
22#include "storage/repo-storage.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070023
Weiqi Shif0330d52014-07-09 10:54:27 -070024#include "../repo-storage-fixture.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070025#include "../dataset-fixtures.hpp"
26
Davide Pesaventod8521aa2023-09-17 14:21:27 -040027#include <boost/asio/io_context.hpp>
Shuo Chenca329182014-03-19 18:05:18 -070028#include <boost/test/unit_test.hpp>
29
Davide Pesavento11904062022-04-14 22:33:28 -040030namespace repo::tests {
Shuo Chenca329182014-03-19 18:05:18 -070031
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040032BOOST_AUTO_TEST_SUITE(TestBasicInterestRead)
weijia yuan3aa8d2b2018-03-06 15:35:57 -080033
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040034const uint8_t CONTENT[] = {3, 1, 4, 1, 5, 9, 2, 6};
Shuo Chenca329182014-03-19 18:05:18 -070035
36template<class Dataset>
Weiqi Shif0330d52014-07-09 10:54:27 -070037class BasicInterestReadFixture : public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070038{
39public:
40 BasicInterestReadFixture()
Davide Pesavento164ae3b2023-11-11 22:00:23 -050041 : scheduler(repoFace.getIoContext())
weijia yuan82cf9142018-10-21 12:25:02 -070042 , readHandle(repoFace, *handle, 0)
Davide Pesavento164ae3b2023-11-11 22:00:23 -050043 , readFace(repoFace.getIoContext())
Shuo Chenca329182014-03-19 18:05:18 -070044 {
45 }
46
47 ~BasicInterestReadFixture()
48 {
Davide Pesavento164ae3b2023-11-11 22:00:23 -050049 repoFace.getIoContext().stop();
Shuo Chenca329182014-03-19 18:05:18 -070050 }
51
52 void
53 startListen()
54 {
55 readHandle.listen("/");
56 }
57
58 void
59 scheduleReadEvent()
60 {
61 int timeCount = 1;
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040062 for (auto i = this->data.begin(); i != this->data.end(); ++i) {
63 // First insert a data into database
64 (*i)->setContent(CONTENT);
weijia yuan82cf9142018-10-21 12:25:02 -070065 (*i)->setFreshnessPeriod(36000_ms);
Shuo Chenca329182014-03-19 18:05:18 -070066 keyChain.sign(**i);
67 bool rc = handle->insertData(**i);
Shuo Chenca329182014-03-19 18:05:18 -070068 BOOST_CHECK_EQUAL(rc, true);
weijia yuan3aa8d2b2018-03-06 15:35:57 -080069
Shuo Chenca329182014-03-19 18:05:18 -070070 Interest readInterest((*i)->getName());
71 readInterest.setMustBeFresh(true);
Davide Pesavento8891c832019-03-20 23:20:35 -040072 scheduler.schedule(ndn::time::milliseconds(timeCount * 50),
73 std::bind(&BasicInterestReadFixture<Dataset>::sendInterest, this, readInterest));
Shuo Chenca329182014-03-19 18:05:18 -070074 timeCount++;
75 }
76 }
77
78 void
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040079 onReadData(const ndn::Interest&, const ndn::Data& data) const
Shuo Chenca329182014-03-19 18:05:18 -070080 {
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040081 BOOST_TEST(data.getContent().value_bytes() == CONTENT, boost::test_tools::per_element());
Shuo Chenca329182014-03-19 18:05:18 -070082 }
83
84 void
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040085 onReadTimeout(const ndn::Interest& interest) const
Shuo Chenca329182014-03-19 18:05:18 -070086 {
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040087 BOOST_ERROR("Read timeout " << interest.getName());
Shuo Chenca329182014-03-19 18:05:18 -070088 }
89
90 void
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040091 onReadNack(const ndn::Interest& interest, const ndn::lp::Nack& nack) const
weijia yuan82cf9142018-10-21 12:25:02 -070092 {
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040093 BOOST_ERROR("Read nack " << interest.getName() << " " << nack.getReason());
weijia yuan82cf9142018-10-21 12:25:02 -070094 }
95
96 void
Shuo Chenca329182014-03-19 18:05:18 -070097 sendInterest(const ndn::Interest& interest)
98 {
99 readFace.expressInterest(interest,
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800100 std::bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
101 std::bind(&BasicInterestReadFixture::onReadNack, this, _1, _2),
102 std::bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
Shuo Chenca329182014-03-19 18:05:18 -0700103 }
104
105public:
106 ndn::Face repoFace;
107 ndn::KeyChain keyChain;
108 ndn::Scheduler scheduler;
109 ReadHandle readHandle;
110 ndn::Face readFace;
111};
112
Davide Pesaventob5e57652023-09-17 15:18:08 -0400113using Datasets = boost::mp11::mp_list<BasicDataset, FetchByPrefixDataset, SamePrefixDataset<10>>;
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700114
115BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700116{
Shuo Chenca329182014-03-19 18:05:18 -0700117 this->startListen();
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -0400118 this->scheduler.schedule(1_s, [this] { this->scheduleReadEvent(); });
Shuo Chenca329182014-03-19 18:05:18 -0700119
weijia yuan82cf9142018-10-21 12:25:02 -0700120 this->repoFace.processEvents(20_s);
Shuo Chenca329182014-03-19 18:05:18 -0700121}
122
123BOOST_AUTO_TEST_SUITE_END()
124
Davide Pesavento11904062022-04-14 22:33:28 -0400125} // namespace repo::tests