blob: 2c6a32e6b0cf37ba8b26214218211d7cabb52b81 [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 Pesaventod8521aa2023-09-17 14:21:27 -04003 * Copyright (c) 2014-2023, 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:
Junxiao Shie1801312017-07-22 19:16:49 +000034 virtual
35 ~TcpClient() = default;
36
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070037 void
38 start(const std::string& host, const std::string& port)
39 {
40 using namespace boost::asio;
41
Davide Pesaventod8521aa2023-09-17 14:21:27 -040042 ip::tcp::resolver resolver(ioCtx);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070043 ip::tcp::resolver::query query(host, port);
44
45 ip::tcp::resolver::iterator endpoint = resolver.resolve(query);
46 ip::tcp::resolver::iterator end;
47
48 if (endpoint == end)
49 BOOST_FAIL("Cannot resolve [" + host + ":" + port + "]");
50
51 ip::tcp::endpoint serverEndpoint = *endpoint;
52
53 socket.async_connect(serverEndpoint,
weijia yuan3aa8d2b2018-03-06 15:35:57 -080054 std::bind(&TcpClient::onSuccessfullConnect, this, _1));
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070055 }
56
57 virtual void
58 onSuccessfullConnect(const boost::system::error_code& error)
59 {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080060 if (error) {
61 BOOST_FAIL("TCP connection aborted");
62 return;
63 }
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070064 }
65
66public:
Davide Pesaventod8521aa2023-09-17 14:21:27 -040067 boost::asio::io_context ioCtx;
68 boost::asio::ip::tcp::socket socket{ioCtx};
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070069};
70
71template<class Dataset>
72class TcpBulkInsertFixture : public TcpClient,
Weiqi Shif0330d52014-07-09 10:54:27 -070073 public RepoStorageFixture,
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070074 public Dataset
75{
76public:
77 TcpBulkInsertFixture()
Davide Pesaventod8521aa2023-09-17 14:21:27 -040078 : scheduler(ioCtx)
79 , bulkInserter(ioCtx, *handle)
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070080 {
Davide Pesavento8891c832019-03-20 23:20:35 -040081 guardEvent = scheduler.schedule(2_s, std::bind(&TcpBulkInsertFixture::fail, this, "Test timed out"));
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070082 }
83
84 virtual void
85 onSuccessfullConnect(const boost::system::error_code& error)
86 {
87 TcpClient::onSuccessfullConnect(error);
88
89 // This value may need to be adjusted if some dataset exceeds 100k
90 socket.set_option(boost::asio::socket_base::send_buffer_size(100000));
91
92 // Initially I wrote the following to use scatter-gather approach (using
93 // std::vector<const_buffer> and a single socket.async_send operation). Unfortunately, as
94 // described in http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/overview/implementation.html,
95 // scatter-gather is limited to at most `min(64,IOV_MAX)` buffers to be transmitted
96 // in a single operation
weijia yuan3aa8d2b2018-03-06 15:35:57 -080097 for (auto i = this->data.begin(); i != this->data.end(); ++i) {
Davide Pesavento9c0bd8d2022-03-14 16:48:12 -040098 socket.async_send(boost::asio::buffer((*i)->wireEncode()),
weijia yuan3aa8d2b2018-03-06 15:35:57 -080099 std::bind(&TcpBulkInsertFixture::onSendFinished, this, _1, false));
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700100 }
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800101 onSendFinished(error, true);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700102 }
103
104 void
105 onSendFinished(const boost::system::error_code& error, bool isFinal)
106 {
107 if (error) {
108 BOOST_FAIL("TCP connection aborted");
109 return;
110 }
111
112 if (isFinal) {
Davide Pesavento8891c832019-03-20 23:20:35 -0400113 guardEvent.cancel();
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700114
115 // In case there are some outstanding handlers
Davide Pesavento8891c832019-03-20 23:20:35 -0400116 scheduler.schedule(1_s, [this] { stop(); });
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700117 }
118 }
119
120 void
121 fail(const std::string& info)
122 {
Davide Pesaventod8521aa2023-09-17 14:21:27 -0400123 ioCtx.stop();
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700124 BOOST_FAIL(info);
125 }
126
127 void
128 stop()
129 {
130 // Terminate test
131 socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
132 socket.close();
133
134 bulkInserter.stop();
Davide Pesaventod8521aa2023-09-17 14:21:27 -0400135 // may be ioCtx.stop() as well
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700136 }
137
138public:
139 Scheduler scheduler;
Davide Pesavento9a8d7d82019-03-15 20:14:25 -0400140 ndn::scheduler::EventId guardEvent;
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700141 repo::TcpBulkInsertHandle bulkInserter;
142};
143
Alexander Afanasyev595b2bd2014-12-14 12:55:36 -0800144BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, CommonDatasets, TcpBulkInsertFixture<T>)
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700145{
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700146 // start bulk inserter
147 this->bulkInserter.listen("localhost", "17376");
148
149 // start test
150 this->start("localhost", "17376");
151
152 // actually run the test
Davide Pesaventod8521aa2023-09-17 14:21:27 -0400153 this->ioCtx.run();
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700154
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700155 // Read (all items should exist)
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800156 for (auto i = this->interests.begin(); i != this->interests.end(); ++i) {
157 BOOST_CHECK_EQUAL(*this->handle->readData(i->first), *i->second);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700158 }
159}
160
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700161BOOST_AUTO_TEST_SUITE_END()
162
Davide Pesavento11904062022-04-14 22:33:28 -0400163} // namespace repo::tests