blob: ab3da0148e468d96ff97c2c28a51bf5bf23d518f [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 Pesavento9c0bd8d2022-03-14 16:48:12 -04003 * Copyright (c) 2014-2022, 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
Eric Newberryb16edda2017-12-23 17:54:47 -070027#include <boost/asio/io_service.hpp>
Shuo Chenca329182014-03-19 18:05:18 -070028#include <boost/test/unit_test.hpp>
29
weijia yuan82cf9142018-10-21 12:25:02 -070030#include <ndn-cxx/util/time.hpp>
31
Shuo Chenca329182014-03-19 18:05:18 -070032namespace repo {
33namespace tests {
34
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040035BOOST_AUTO_TEST_SUITE(TestBasicInterestRead)
weijia yuan3aa8d2b2018-03-06 15:35:57 -080036
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040037const uint8_t CONTENT[] = {3, 1, 4, 1, 5, 9, 2, 6};
Shuo Chenca329182014-03-19 18:05:18 -070038
39template<class Dataset>
Weiqi Shif0330d52014-07-09 10:54:27 -070040class BasicInterestReadFixture : public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070041{
42public:
43 BasicInterestReadFixture()
44 : scheduler(repoFace.getIoService())
weijia yuan82cf9142018-10-21 12:25:02 -070045 , readHandle(repoFace, *handle, 0)
Shuo Chenca329182014-03-19 18:05:18 -070046 , 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 Pesavento9c0bd8d2022-03-14 16:48:12 -040065 for (auto i = this->data.begin(); i != this->data.end(); ++i) {
66 // First insert a data into database
67 (*i)->setContent(CONTENT);
weijia yuan82cf9142018-10-21 12:25:02 -070068 (*i)->setFreshnessPeriod(36000_ms);
Shuo Chenca329182014-03-19 18:05:18 -070069 keyChain.sign(**i);
70 bool rc = handle->insertData(**i);
Shuo Chenca329182014-03-19 18:05:18 -070071 BOOST_CHECK_EQUAL(rc, true);
weijia yuan3aa8d2b2018-03-06 15:35:57 -080072
Shuo Chenca329182014-03-19 18:05:18 -070073 Interest readInterest((*i)->getName());
74 readInterest.setMustBeFresh(true);
Davide Pesavento8891c832019-03-20 23:20:35 -040075 scheduler.schedule(ndn::time::milliseconds(timeCount * 50),
76 std::bind(&BasicInterestReadFixture<Dataset>::sendInterest, this, readInterest));
Shuo Chenca329182014-03-19 18:05:18 -070077 timeCount++;
78 }
79 }
80
81 void
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040082 onReadData(const ndn::Interest&, const ndn::Data& data) const
Shuo Chenca329182014-03-19 18:05:18 -070083 {
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040084 BOOST_TEST(data.getContent().value_bytes() == CONTENT, boost::test_tools::per_element());
Shuo Chenca329182014-03-19 18:05:18 -070085 }
86
87 void
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040088 onReadTimeout(const ndn::Interest& interest) const
Shuo Chenca329182014-03-19 18:05:18 -070089 {
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040090 BOOST_ERROR("Read timeout " << interest.getName());
Shuo Chenca329182014-03-19 18:05:18 -070091 }
92
93 void
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040094 onReadNack(const ndn::Interest& interest, const ndn::lp::Nack& nack) const
weijia yuan82cf9142018-10-21 12:25:02 -070095 {
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040096 BOOST_ERROR("Read nack " << interest.getName() << " " << nack.getReason());
weijia yuan82cf9142018-10-21 12:25:02 -070097 }
98
99 void
Shuo Chenca329182014-03-19 18:05:18 -0700100 sendInterest(const ndn::Interest& interest)
101 {
102 readFace.expressInterest(interest,
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800103 std::bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
104 std::bind(&BasicInterestReadFixture::onReadNack, this, _1, _2),
105 std::bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
Shuo Chenca329182014-03-19 18:05:18 -0700106 }
107
108public:
109 ndn::Face repoFace;
110 ndn::KeyChain keyChain;
111 ndn::Scheduler scheduler;
112 ReadHandle readHandle;
113 ndn::Face readFace;
114};
115
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800116using Datasets = boost::mpl::vector<BasicDataset,
117 FetchByPrefixDataset,
118 SamePrefixDataset<10>>;
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700119
120BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700121{
Shuo Chenca329182014-03-19 18:05:18 -0700122 this->startListen();
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -0400123 this->scheduler.schedule(1_s, [this] { this->scheduleReadEvent(); });
Shuo Chenca329182014-03-19 18:05:18 -0700124
weijia yuan82cf9142018-10-21 12:25:02 -0700125 this->repoFace.processEvents(20_s);
Shuo Chenca329182014-03-19 18:05:18 -0700126}
127
128BOOST_AUTO_TEST_SUITE_END()
129
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800130} // namespace tests
131} // namespace repo