blob: 9b7b796dc3eef6a15b523d75116a8ac9d49331fa [file] [log] [blame]
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento9a8d7d82019-03-15 20:14:25 -04002/*
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -04003 * Copyright (c) 2014-2022, 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
Davide Pesavento11904062022-04-14 22:33:28 -040027namespace repo::tests {
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070028
29BOOST_AUTO_TEST_SUITE(TcpBulkInsertHandle)
30
31class TcpClient
32{
33public:
34 TcpClient()
35 : socket(ioService)
36 {
37 }
38
Junxiao Shie1801312017-07-22 19:16:49 +000039 virtual
40 ~TcpClient() = default;
41
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070042 void
43 start(const std::string& host, const std::string& port)
44 {
45 using namespace boost::asio;
46
47 ip::tcp::resolver resolver(ioService);
48 ip::tcp::resolver::query query(host, port);
49
50 ip::tcp::resolver::iterator endpoint = resolver.resolve(query);
51 ip::tcp::resolver::iterator end;
52
53 if (endpoint == end)
54 BOOST_FAIL("Cannot resolve [" + host + ":" + port + "]");
55
56 ip::tcp::endpoint serverEndpoint = *endpoint;
57
58 socket.async_connect(serverEndpoint,
weijia yuan3aa8d2b2018-03-06 15:35:57 -080059 std::bind(&TcpClient::onSuccessfullConnect, this, _1));
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070060 }
61
62 virtual void
63 onSuccessfullConnect(const boost::system::error_code& error)
64 {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080065 if (error) {
66 BOOST_FAIL("TCP connection aborted");
67 return;
68 }
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070069 }
70
71public:
72 boost::asio::io_service ioService;
73 boost::asio::ip::tcp::socket socket;
74};
75
76template<class Dataset>
77class TcpBulkInsertFixture : public TcpClient,
Weiqi Shif0330d52014-07-09 10:54:27 -070078 public RepoStorageFixture,
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070079 public Dataset
80{
81public:
82 TcpBulkInsertFixture()
83 : scheduler(ioService)
84 , bulkInserter(ioService, *handle)
85 {
Davide Pesavento8891c832019-03-20 23:20:35 -040086 guardEvent = scheduler.schedule(2_s, std::bind(&TcpBulkInsertFixture::fail, this, "Test timed out"));
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070087 }
88
89 virtual void
90 onSuccessfullConnect(const boost::system::error_code& error)
91 {
92 TcpClient::onSuccessfullConnect(error);
93
94 // This value may need to be adjusted if some dataset exceeds 100k
95 socket.set_option(boost::asio::socket_base::send_buffer_size(100000));
96
97 // Initially I wrote the following to use scatter-gather approach (using
98 // std::vector<const_buffer> and a single socket.async_send operation). Unfortunately, as
99 // described in http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/overview/implementation.html,
100 // scatter-gather is limited to at most `min(64,IOV_MAX)` buffers to be transmitted
101 // in a single operation
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800102 for (auto i = this->data.begin(); i != this->data.end(); ++i) {
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -0400103 socket.async_send(boost::asio::buffer((*i)->wireEncode()),
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800104 std::bind(&TcpBulkInsertFixture::onSendFinished, this, _1, false));
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700105 }
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800106 onSendFinished(error, true);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700107 }
108
109 void
110 onSendFinished(const boost::system::error_code& error, bool isFinal)
111 {
112 if (error) {
113 BOOST_FAIL("TCP connection aborted");
114 return;
115 }
116
117 if (isFinal) {
Davide Pesavento8891c832019-03-20 23:20:35 -0400118 guardEvent.cancel();
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700119
120 // In case there are some outstanding handlers
Davide Pesavento8891c832019-03-20 23:20:35 -0400121 scheduler.schedule(1_s, [this] { stop(); });
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700122 }
123 }
124
125 void
126 fail(const std::string& info)
127 {
128 ioService.stop();
129 BOOST_FAIL(info);
130 }
131
132 void
133 stop()
134 {
135 // Terminate test
136 socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
137 socket.close();
138
139 bulkInserter.stop();
140 // may be ioService.stop() as well
141 }
142
143public:
144 Scheduler scheduler;
Davide Pesavento9a8d7d82019-03-15 20:14:25 -0400145 ndn::scheduler::EventId guardEvent;
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700146 repo::TcpBulkInsertHandle bulkInserter;
147};
148
Alexander Afanasyev595b2bd2014-12-14 12:55:36 -0800149BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, CommonDatasets, TcpBulkInsertFixture<T>)
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700150{
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700151 // start bulk inserter
152 this->bulkInserter.listen("localhost", "17376");
153
154 // start test
155 this->start("localhost", "17376");
156
157 // actually run the test
158 this->ioService.run();
159
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700160 // Read (all items should exist)
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800161 for (auto i = this->interests.begin(); i != this->interests.end(); ++i) {
162 BOOST_CHECK_EQUAL(*this->handle->readData(i->first), *i->second);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700163 }
164}
165
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700166BOOST_AUTO_TEST_SUITE_END()
167
Davide Pesavento11904062022-04-14 22:33:28 -0400168} // namespace repo::tests