Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 27 | namespace repo { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TcpBulkInsertHandle) |
| 31 | |
| 32 | class TcpClient |
| 33 | { |
| 34 | public: |
| 35 | TcpClient() |
| 36 | : socket(ioService) |
| 37 | { |
| 38 | } |
| 39 | |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 40 | virtual |
| 41 | ~TcpClient() = default; |
| 42 | |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 43 | 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 | |
| 73 | public: |
| 74 | boost::asio::io_service ioService; |
| 75 | boost::asio::ip::tcp::socket socket; |
| 76 | }; |
| 77 | |
| 78 | template<class Dataset> |
| 79 | class TcpBulkInsertFixture : public TcpClient, |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 80 | public RepoStorageFixture, |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 81 | public Dataset |
| 82 | { |
| 83 | public: |
| 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 | |
| 152 | public: |
| 153 | Scheduler scheduler; |
| 154 | ndn::EventId guardEvent; |
| 155 | repo::TcpBulkInsertHandle bulkInserter; |
| 156 | }; |
| 157 | |
| 158 | |
Alexander Afanasyev | 595b2bd | 2014-12-14 12:55:36 -0800 | [diff] [blame] | 159 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, CommonDatasets, TcpBulkInsertFixture<T>) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 160 | { |
| 161 | BOOST_TEST_MESSAGE(T::getName()); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 162 | |
| 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 Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 172 | // Read (all items should exist) |
| 173 | for (typename T::InterestContainer::iterator i = this->interests.begin(); |
| 174 | i != this->interests.end(); ++i) { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 175 | BOOST_CHECK_EQUAL(*this->handle->readData(i->first), *i->second); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | BOOST_AUTO_TEST_SUITE_END() |
| 181 | |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 182 | } // namespace tests |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 183 | } // namespace repo |