blob: 6d911f74e81f558cc30782da5b1fc626de459d3e [file] [log] [blame]
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -07003 * Copyright (c) 2014, Regents of the University of California.
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 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
40 void
41 start(const std::string& host, const std::string& port)
42 {
43 using namespace boost::asio;
44
45 ip::tcp::resolver resolver(ioService);
46 ip::tcp::resolver::query query(host, port);
47
48 ip::tcp::resolver::iterator endpoint = resolver.resolve(query);
49 ip::tcp::resolver::iterator end;
50
51 if (endpoint == end)
52 BOOST_FAIL("Cannot resolve [" + host + ":" + port + "]");
53
54 ip::tcp::endpoint serverEndpoint = *endpoint;
55
56 socket.async_connect(serverEndpoint,
57 bind(&TcpClient::onSuccessfullConnect, this, _1));
58 }
59
60 virtual void
61 onSuccessfullConnect(const boost::system::error_code& error)
62 {
63 if (error)
64 {
65 BOOST_FAIL("TCP connection aborted");
66 return;
67 }
68 }
69
70public:
71 boost::asio::io_service ioService;
72 boost::asio::ip::tcp::socket socket;
73};
74
75template<class Dataset>
76class TcpBulkInsertFixture : public TcpClient,
Weiqi Shif0330d52014-07-09 10:54:27 -070077 public RepoStorageFixture,
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070078 public Dataset
79{
80public:
81 TcpBulkInsertFixture()
82 : scheduler(ioService)
83 , bulkInserter(ioService, *handle)
84 {
85 guardEvent = scheduler.scheduleEvent(ndn::time::seconds(2),
86 bind(&TcpBulkInsertFixture::fail, this, "Test timed out"));
87 }
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
102 for (typename Dataset::DataContainer::iterator i = this->data.begin();
103 i != this->data.end(); ++i) {
104
105 socket.async_send(boost::asio::buffer((*i)->wireEncode().wire(), (*i)->wireEncode().size()),
106 bind(&TcpBulkInsertFixture::onSendFinished, this, _1, false));
107 }
108
109 socket.async_send(boost::asio::buffer(static_cast<const uint8_t*>(0), 0),
110 bind(&TcpBulkInsertFixture::onSendFinished, this, _1, true));
111 }
112
113 void
114 onSendFinished(const boost::system::error_code& error, bool isFinal)
115 {
116 if (error) {
117 BOOST_FAIL("TCP connection aborted");
118 return;
119 }
120
121 if (isFinal) {
122 scheduler.cancelEvent(guardEvent);
123
124 // In case there are some outstanding handlers
125 // ioService.post(bind(&TcpBulkInsertFixture::stop, this));
126 scheduler.scheduleEvent(ndn::time::seconds(1),
127 bind(&TcpBulkInsertFixture::stop, this));
128 }
129 }
130
131 void
132 fail(const std::string& info)
133 {
134 ioService.stop();
135 BOOST_FAIL(info);
136 }
137
138 void
139 stop()
140 {
141 // Terminate test
142 socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
143 socket.close();
144
145 bulkInserter.stop();
146 // may be ioService.stop() as well
147 }
148
149public:
150 Scheduler scheduler;
151 ndn::EventId guardEvent;
152 repo::TcpBulkInsertHandle bulkInserter;
153};
154
155
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700156BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, CommonDatasets, TcpBulkInsertFixture<T>)
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700157{
158 BOOST_TEST_MESSAGE(T::getName());
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700159
160 // start bulk inserter
161 this->bulkInserter.listen("localhost", "17376");
162
163 // start test
164 this->start("localhost", "17376");
165
166 // actually run the test
167 this->ioService.run();
168
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700169 // Read (all items should exist)
170 for (typename T::InterestContainer::iterator i = this->interests.begin();
171 i != this->interests.end(); ++i) {
Weiqi Shif0330d52014-07-09 10:54:27 -0700172 BOOST_CHECK_EQUAL(*this->handle->readData(i->first), *i->second);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700173 }
174}
175
176
177BOOST_AUTO_TEST_SUITE_END()
178
179} // namespace repo
180} // namespace tests