blob: c19887164e96f4c5f763714827b9e0594fdcab7e [file] [log] [blame]
Shuo Chenca329182014-03-19 18:05:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
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 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
27#include <ndn-cxx/util/random.hpp>
28
29#include <boost/test/unit_test.hpp>
30
31namespace repo {
32namespace tests {
33
34//All the test cases in this test suite should be run at once.
35BOOST_AUTO_TEST_SUITE(TestBasicInerestRead)
36
37const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
38
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())
45 , readHandle(repoFace, *handle, keyChain, scheduler)
46 , 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;
65 for (typename Dataset::DataContainer::iterator i = this->data.begin();
66 i != this->data.end(); ++i) {
67 //First insert a data into database;
68 (*i)->setContent(content, sizeof(content));
69 (*i)->setFreshnessPeriod(ndn::time::milliseconds(36000));
70 keyChain.sign(**i);
71 bool rc = handle->insertData(**i);
72
73 BOOST_CHECK_EQUAL(rc, true);
Shuo Chenca329182014-03-19 18:05:18 -070074 Interest readInterest((*i)->getName());
75 readInterest.setMustBeFresh(true);
76 scheduler.scheduleEvent(ndn::time::milliseconds(timeCount * 50),
77 ndn::bind(&BasicInterestReadFixture<Dataset>::sendInterest, this,
78 readInterest));
79 timeCount++;
80 }
81 }
82
83 void
84 stopFaceProcess()
85 {
86 repoFace.getIoService().stop();
87 }
88
89 void
90 onReadData(const ndn::Interest& interest, ndn::Data& data)
91 {
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 {
99 BOOST_ERROR("Insert not successfull or Read data does not successfull");
100 }
101
102 void
103 sendInterest(const ndn::Interest& interest)
104 {
105 readFace.expressInterest(interest,
106 bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
107 bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
108 }
109
110public:
111 ndn::Face repoFace;
112 ndn::KeyChain keyChain;
113 ndn::Scheduler scheduler;
114 ReadHandle readHandle;
115 ndn::Face readFace;
116};
117
118
119BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, DatasetFixtures, BasicInterestReadFixture<T>)
120{
121 // Insert dataset
Weiqi Shif0330d52014-07-09 10:54:27 -0700122 // for (typename T::DataContainer::iterator i = this->data.begin();
123 // i != this->data.end(); ++i) {
124 // BOOST_CHECK_EQUAL(this->handle.insertData(**i), true);
125 // }
Shuo Chenca329182014-03-19 18:05:18 -0700126
Weiqi Shif0330d52014-07-09 10:54:27 -0700127// BOOST_CHECK_EQUAL(this->handle.size(), this->data.size());
Shuo Chenca329182014-03-19 18:05:18 -0700128
129 this->startListen();
130 this->scheduler.scheduleEvent(ndn::time::seconds(0),
131 ndn::bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this));
132
133 // schedule an event to terminate IO
Weiqi Shif0330d52014-07-09 10:54:27 -0700134 this->scheduler.scheduleEvent(ndn::time::seconds(20),
Shuo Chenca329182014-03-19 18:05:18 -0700135 ndn::bind(&BasicInterestReadFixture<T>::stopFaceProcess, this));
136 this->repoFace.getIoService().run();
137
138}
139
140BOOST_AUTO_TEST_SUITE_END()
141
142} //namespace tests
143} //namespace repo