blob: f35a42f7b7b95f1312d20f9644298cc6e31df533 [file] [log] [blame]
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shie1801312017-07-22 19:16:49 +00003 * Copyright (c) 2014-2017, Regents of the University of California.
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -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/>.
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070018 */
19
Alexander Afanasyev39d98072014-05-04 12:46:29 -070020#include "handles/tcp-bulk-insert-handle.hpp"
Weiqi Shif0330d52014-07-09 10:54:27 -070021#include "storage/sqlite-storage.hpp"
22#include "../repo-storage-fixture.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070023#include "../dataset-fixtures.hpp"
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070024
25#include <boost/test/unit_test.hpp>
26
27namespace repo {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(TcpBulkInsertHandle)
31
32class TcpClient
33{
34public:
35 TcpClient()
36 : socket(ioService)
37 {
38 }
39
Junxiao Shie1801312017-07-22 19:16:49 +000040 virtual
41 ~TcpClient() = default;
42
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070043 void
44 start(const std::string& host, const std::string& port)
45 {
46 using namespace boost::asio;
47
48 ip::tcp::resolver resolver(ioService);
49 ip::tcp::resolver::query query(host, port);
50
51 ip::tcp::resolver::iterator endpoint = resolver.resolve(query);
52 ip::tcp::resolver::iterator end;
53
54 if (endpoint == end)
55 BOOST_FAIL("Cannot resolve [" + host + ":" + port + "]");
56
57 ip::tcp::endpoint serverEndpoint = *endpoint;
58
59 socket.async_connect(serverEndpoint,
60 bind(&TcpClient::onSuccessfullConnect, this, _1));
61 }
62
63 virtual void
64 onSuccessfullConnect(const boost::system::error_code& error)
65 {
66 if (error)
67 {
68 BOOST_FAIL("TCP connection aborted");
69 return;
70 }
71 }
72
73public:
74 boost::asio::io_service ioService;
75 boost::asio::ip::tcp::socket socket;
76};
77
78template<class Dataset>
79class TcpBulkInsertFixture : public TcpClient,
Weiqi Shif0330d52014-07-09 10:54:27 -070080 public RepoStorageFixture,
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070081 public Dataset
82{
83public:
84 TcpBulkInsertFixture()
85 : scheduler(ioService)
86 , bulkInserter(ioService, *handle)
87 {
88 guardEvent = scheduler.scheduleEvent(ndn::time::seconds(2),
89 bind(&TcpBulkInsertFixture::fail, this, "Test timed out"));
90 }
91
92 virtual void
93 onSuccessfullConnect(const boost::system::error_code& error)
94 {
95 TcpClient::onSuccessfullConnect(error);
96
97 // This value may need to be adjusted if some dataset exceeds 100k
98 socket.set_option(boost::asio::socket_base::send_buffer_size(100000));
99
100 // Initially I wrote the following to use scatter-gather approach (using
101 // std::vector<const_buffer> and a single socket.async_send operation). Unfortunately, as
102 // described in http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/overview/implementation.html,
103 // scatter-gather is limited to at most `min(64,IOV_MAX)` buffers to be transmitted
104 // in a single operation
105 for (typename Dataset::DataContainer::iterator i = this->data.begin();
106 i != this->data.end(); ++i) {
107
108 socket.async_send(boost::asio::buffer((*i)->wireEncode().wire(), (*i)->wireEncode().size()),
109 bind(&TcpBulkInsertFixture::onSendFinished, this, _1, false));
110 }
111
112 socket.async_send(boost::asio::buffer(static_cast<const uint8_t*>(0), 0),
113 bind(&TcpBulkInsertFixture::onSendFinished, this, _1, true));
114 }
115
116 void
117 onSendFinished(const boost::system::error_code& error, bool isFinal)
118 {
119 if (error) {
120 BOOST_FAIL("TCP connection aborted");
121 return;
122 }
123
124 if (isFinal) {
125 scheduler.cancelEvent(guardEvent);
126
127 // In case there are some outstanding handlers
128 // ioService.post(bind(&TcpBulkInsertFixture::stop, this));
129 scheduler.scheduleEvent(ndn::time::seconds(1),
130 bind(&TcpBulkInsertFixture::stop, this));
131 }
132 }
133
134 void
135 fail(const std::string& info)
136 {
137 ioService.stop();
138 BOOST_FAIL(info);
139 }
140
141 void
142 stop()
143 {
144 // Terminate test
145 socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
146 socket.close();
147
148 bulkInserter.stop();
149 // may be ioService.stop() as well
150 }
151
152public:
153 Scheduler scheduler;
154 ndn::EventId guardEvent;
155 repo::TcpBulkInsertHandle bulkInserter;
156};
157
158
Alexander Afanasyev595b2bd2014-12-14 12:55:36 -0800159BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, CommonDatasets, TcpBulkInsertFixture<T>)
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700160{
161 BOOST_TEST_MESSAGE(T::getName());
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700162
163 // start bulk inserter
164 this->bulkInserter.listen("localhost", "17376");
165
166 // start test
167 this->start("localhost", "17376");
168
169 // actually run the test
170 this->ioService.run();
171
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700172 // Read (all items should exist)
173 for (typename T::InterestContainer::iterator i = this->interests.begin();
174 i != this->interests.end(); ++i) {
Weiqi Shif0330d52014-07-09 10:54:27 -0700175 BOOST_CHECK_EQUAL(*this->handle->readData(i->first), *i->second);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700176 }
177}
178
179
180BOOST_AUTO_TEST_SUITE_END()
181
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700182} // namespace tests
Junxiao Shie1801312017-07-22 19:16:49 +0000183} // namespace repo