Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 9a8d7d8 | 2019-03-15 20:14:25 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 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/>. |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 20 | #include "handles/tcp-bulk-insert-handle.hpp" |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 21 | #include "storage/sqlite-storage.hpp" |
| 22 | #include "../repo-storage-fixture.hpp" |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 23 | #include "../dataset-fixtures.hpp" |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 24 | |
| 25 | #include <boost/test/unit_test.hpp> |
| 26 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 27 | namespace repo::tests { |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(TcpBulkInsertHandle) |
| 30 | |
| 31 | class TcpClient |
| 32 | { |
| 33 | public: |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 34 | void |
| 35 | start(const std::string& host, const std::string& port) |
| 36 | { |
| 37 | using namespace boost::asio; |
| 38 | |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 39 | ip::tcp::resolver resolver(ioCtx); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 40 | ip::tcp::resolver::query query(host, port); |
| 41 | |
| 42 | ip::tcp::resolver::iterator endpoint = resolver.resolve(query); |
| 43 | ip::tcp::resolver::iterator end; |
| 44 | |
| 45 | if (endpoint == end) |
| 46 | BOOST_FAIL("Cannot resolve [" + host + ":" + port + "]"); |
| 47 | |
| 48 | ip::tcp::endpoint serverEndpoint = *endpoint; |
| 49 | |
| 50 | socket.async_connect(serverEndpoint, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 51 | std::bind(&TcpClient::onSuccessfullConnect, this, _1)); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | virtual void |
| 55 | onSuccessfullConnect(const boost::system::error_code& error) |
| 56 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 57 | if (error) { |
| 58 | BOOST_FAIL("TCP connection aborted"); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 59 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public: |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 63 | boost::asio::io_context ioCtx; |
| 64 | boost::asio::ip::tcp::socket socket{ioCtx}; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | template<class Dataset> |
| 68 | class TcpBulkInsertFixture : public TcpClient, |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 69 | public RepoStorageFixture, |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 70 | public Dataset |
| 71 | { |
| 72 | public: |
| 73 | TcpBulkInsertFixture() |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 74 | : scheduler(ioCtx) |
| 75 | , bulkInserter(ioCtx, *handle) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 76 | { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 77 | guardEvent = scheduler.schedule(2_s, std::bind(&TcpBulkInsertFixture::fail, this, "Test timed out")); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame^] | 80 | void |
| 81 | onSuccessfullConnect(const boost::system::error_code& error) override |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 82 | { |
| 83 | TcpClient::onSuccessfullConnect(error); |
| 84 | |
| 85 | // This value may need to be adjusted if some dataset exceeds 100k |
| 86 | socket.set_option(boost::asio::socket_base::send_buffer_size(100000)); |
| 87 | |
| 88 | // Initially I wrote the following to use scatter-gather approach (using |
| 89 | // std::vector<const_buffer> and a single socket.async_send operation). Unfortunately, as |
| 90 | // described in http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/overview/implementation.html, |
| 91 | // scatter-gather is limited to at most `min(64,IOV_MAX)` buffers to be transmitted |
| 92 | // in a single operation |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 93 | for (auto i = this->data.begin(); i != this->data.end(); ++i) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 94 | socket.async_send(boost::asio::buffer((*i)->wireEncode()), |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 95 | std::bind(&TcpBulkInsertFixture::onSendFinished, this, _1, false)); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 96 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 97 | onSendFinished(error, true); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | onSendFinished(const boost::system::error_code& error, bool isFinal) |
| 102 | { |
| 103 | if (error) { |
| 104 | BOOST_FAIL("TCP connection aborted"); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | if (isFinal) { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 109 | guardEvent.cancel(); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 110 | |
| 111 | // In case there are some outstanding handlers |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 112 | scheduler.schedule(1_s, [this] { stop(); }); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | fail(const std::string& info) |
| 118 | { |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 119 | ioCtx.stop(); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 120 | BOOST_FAIL(info); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | stop() |
| 125 | { |
| 126 | // Terminate test |
| 127 | socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both); |
| 128 | socket.close(); |
| 129 | |
| 130 | bulkInserter.stop(); |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 131 | // may be ioCtx.stop() as well |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | public: |
| 135 | Scheduler scheduler; |
Davide Pesavento | 9a8d7d8 | 2019-03-15 20:14:25 -0400 | [diff] [blame] | 136 | ndn::scheduler::EventId guardEvent; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 137 | repo::TcpBulkInsertHandle bulkInserter; |
| 138 | }; |
| 139 | |
Alexander Afanasyev | 595b2bd | 2014-12-14 12:55:36 -0800 | [diff] [blame] | 140 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, CommonDatasets, TcpBulkInsertFixture<T>) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 141 | { |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 142 | // start bulk inserter |
| 143 | this->bulkInserter.listen("localhost", "17376"); |
| 144 | |
| 145 | // start test |
| 146 | this->start("localhost", "17376"); |
| 147 | |
| 148 | // actually run the test |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 149 | this->ioCtx.run(); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 150 | |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 151 | // Read (all items should exist) |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 152 | for (auto i = this->interests.begin(); i != this->interests.end(); ++i) { |
| 153 | BOOST_CHECK_EQUAL(*this->handle->readData(i->first), *i->second); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 157 | BOOST_AUTO_TEST_SUITE_END() |
| 158 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 159 | } // namespace repo::tests |