blob: d73b0d1e69711fad941d9a73059b8d08340d1225 [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/*
weijia yuan82cf9142018-10-21 12:25:02 -07003 * Copyright (c) 2014-2018, 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 yuan3aa8d2b2018-03-06 15:35:57 -080030#include <ndn-cxx/util/logger.hpp>
weijia yuan82cf9142018-10-21 12:25:02 -070031#include <ndn-cxx/util/time.hpp>
32
Shuo Chenca329182014-03-19 18:05:18 -070033namespace repo {
34namespace tests {
35
weijia yuan3aa8d2b2018-03-06 15:35:57 -080036NDN_LOG_INIT(repo.tests.read);
37
Shuo Chenca329182014-03-19 18:05:18 -070038BOOST_AUTO_TEST_SUITE(TestBasicInerestRead)
39
40const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
41
42template<class Dataset>
Weiqi Shif0330d52014-07-09 10:54:27 -070043class BasicInterestReadFixture : public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070044{
45public:
46 BasicInterestReadFixture()
47 : scheduler(repoFace.getIoService())
weijia yuan82cf9142018-10-21 12:25:02 -070048 , readHandle(repoFace, *handle, 0)
Shuo Chenca329182014-03-19 18:05:18 -070049 , 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 yuan82cf9142018-10-21 12:25:02 -070072 (*i)->setFreshnessPeriod(36000_ms);
Shuo Chenca329182014-03-19 18:05:18 -070073 keyChain.sign(**i);
weijia yuan3aa8d2b2018-03-06 15:35:57 -080074 NDN_LOG_DEBUG(**i);
Shuo Chenca329182014-03-19 18:05:18 -070075 bool rc = handle->insertData(**i);
76
77 BOOST_CHECK_EQUAL(rc, true);
weijia yuan3aa8d2b2018-03-06 15:35:57 -080078 NDN_LOG_DEBUG("Inserted Data " << (**i).getName());
79
Shuo Chenca329182014-03-19 18:05:18 -070080 Interest readInterest((*i)->getName());
81 readInterest.setMustBeFresh(true);
82 scheduler.scheduleEvent(ndn::time::milliseconds(timeCount * 50),
weijia yuan3aa8d2b2018-03-06 15:35:57 -080083 std::bind(&BasicInterestReadFixture<Dataset>::sendInterest, this,
Shuo Chenca329182014-03-19 18:05:18 -070084 readInterest));
85 timeCount++;
86 }
87 }
88
89 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080090 onReadData(const ndn::Interest& interest, const ndn::Data& data)
Shuo Chenca329182014-03-19 18:05:18 -070091 {
92 int rc = memcmp(data.getContent().value(), content, sizeof(content));
93 BOOST_CHECK_EQUAL(rc, 0);
Shuo Chenca329182014-03-19 18:05:18 -070094 }
95
96 void
97 onReadTimeout(const ndn::Interest& interest)
98 {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080099 NDN_LOG_DEBUG("Timed out " << interest.getName());
100 BOOST_ERROR("Insert or read not successfull");
Shuo Chenca329182014-03-19 18:05:18 -0700101 }
102
103 void
weijia yuan82cf9142018-10-21 12:25:02 -0700104 onReadNack(const ndn::Interest& interest, const ndn::lp::Nack& nack)
105 {
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800106 NDN_LOG_DEBUG("Nacked " << interest.getName() << nack.getReason());
weijia yuan82cf9142018-10-21 12:25:02 -0700107 BOOST_ERROR("Read nacked");
108 }
109
110 void
Shuo Chenca329182014-03-19 18:05:18 -0700111 sendInterest(const ndn::Interest& interest)
112 {
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800113 NDN_LOG_DEBUG("Sending Interest " << interest.getName());
Shuo Chenca329182014-03-19 18:05:18 -0700114 readFace.expressInterest(interest,
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800115 std::bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
116 std::bind(&BasicInterestReadFixture::onReadNack, this, _1, _2),
117 std::bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
Shuo Chenca329182014-03-19 18:05:18 -0700118 }
119
120public:
121 ndn::Face repoFace;
122 ndn::KeyChain keyChain;
123 ndn::Scheduler scheduler;
124 ReadHandle readHandle;
125 ndn::Face readFace;
126};
127
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800128
129using Datasets = boost::mpl::vector<BasicDataset,
130 FetchByPrefixDataset,
131 SamePrefixDataset<10>>;
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700132
133BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700134{
Shuo Chenca329182014-03-19 18:05:18 -0700135 this->startListen();
weijia yuan82cf9142018-10-21 12:25:02 -0700136 this->scheduler.scheduleEvent(1_s,
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800137 std::bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this));
Shuo Chenca329182014-03-19 18:05:18 -0700138
weijia yuan82cf9142018-10-21 12:25:02 -0700139 this->repoFace.processEvents(20_s);
Shuo Chenca329182014-03-19 18:05:18 -0700140
141}
142
143BOOST_AUTO_TEST_SUITE_END()
144
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800145} // namespace tests
146} // namespace repo