blob: 81ace5b1855cb2702cfe6da3f52d4b9b81ed7769 [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 Pesavento8891c832019-03-20 23:20:35 -04003 * Copyright (c) 2014-2019, 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);
Davide Pesavento8891c832019-03-20 23:20:35 -040082 scheduler.schedule(ndn::time::milliseconds(timeCount * 50),
83 std::bind(&BasicInterestReadFixture<Dataset>::sendInterest, this, readInterest));
Shuo Chenca329182014-03-19 18:05:18 -070084 timeCount++;
85 }
86 }
87
88 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080089 onReadData(const ndn::Interest& interest, const ndn::Data& data)
Shuo Chenca329182014-03-19 18:05:18 -070090 {
91 int rc = memcmp(data.getContent().value(), content, sizeof(content));
92 BOOST_CHECK_EQUAL(rc, 0);
Shuo Chenca329182014-03-19 18:05:18 -070093 }
94
95 void
96 onReadTimeout(const ndn::Interest& interest)
97 {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080098 NDN_LOG_DEBUG("Timed out " << interest.getName());
99 BOOST_ERROR("Insert or read not successfull");
Shuo Chenca329182014-03-19 18:05:18 -0700100 }
101
102 void
weijia yuan82cf9142018-10-21 12:25:02 -0700103 onReadNack(const ndn::Interest& interest, const ndn::lp::Nack& nack)
104 {
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800105 NDN_LOG_DEBUG("Nacked " << interest.getName() << nack.getReason());
weijia yuan82cf9142018-10-21 12:25:02 -0700106 BOOST_ERROR("Read nacked");
107 }
108
109 void
Shuo Chenca329182014-03-19 18:05:18 -0700110 sendInterest(const ndn::Interest& interest)
111 {
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800112 NDN_LOG_DEBUG("Sending Interest " << interest.getName());
Shuo Chenca329182014-03-19 18:05:18 -0700113 readFace.expressInterest(interest,
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800114 std::bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
115 std::bind(&BasicInterestReadFixture::onReadNack, this, _1, _2),
116 std::bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
Shuo Chenca329182014-03-19 18:05:18 -0700117 }
118
119public:
120 ndn::Face repoFace;
121 ndn::KeyChain keyChain;
122 ndn::Scheduler scheduler;
123 ReadHandle readHandle;
124 ndn::Face readFace;
125};
126
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800127
128using Datasets = boost::mpl::vector<BasicDataset,
129 FetchByPrefixDataset,
130 SamePrefixDataset<10>>;
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700131
132BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700133{
Shuo Chenca329182014-03-19 18:05:18 -0700134 this->startListen();
Davide Pesavento8891c832019-03-20 23:20:35 -0400135 this->scheduler.schedule(1_s, std::bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this));
Shuo Chenca329182014-03-19 18:05:18 -0700136
weijia yuan82cf9142018-10-21 12:25:02 -0700137 this->repoFace.processEvents(20_s);
Shuo Chenca329182014-03-19 18:05:18 -0700138}
139
140BOOST_AUTO_TEST_SUITE_END()
141
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800142} // namespace tests
143} // namespace repo