]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/test/buffered_write_stream.cpp
2 // buffered_write_stream.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 // Disable autolinking for unit tests.
12 #if !defined(BOOST_ALL_NO_LIB)
13 #define BOOST_ALL_NO_LIB 1
14 #endif // !defined(BOOST_ALL_NO_LIB)
16 // Test that header file is self-contained.
17 #include <boost/asio/buffered_write_stream.hpp>
20 #include "archetypes/async_result.hpp"
21 #include <boost/asio/buffer.hpp>
22 #include <boost/asio/io_context.hpp>
23 #include <boost/asio/ip/tcp.hpp>
24 #include <boost/system/system_error.hpp>
25 #include "unit_test.hpp"
27 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
28 # include <boost/array.hpp>
29 #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
31 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
33 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
34 # include <boost/bind.hpp>
35 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
36 # include <functional>
37 #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
39 typedef boost::asio::buffered_write_stream
<
40 boost::asio::ip::tcp::socket
> stream_type
;
42 void write_some_handler(const boost::system::error_code
&, std::size_t)
46 void flush_handler(const boost::system::error_code
&, std::size_t)
50 void read_some_handler(const boost::system::error_code
&, std::size_t)
56 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
58 #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
60 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
62 using namespace boost::asio
;
67 char mutable_char_buffer
[128] = "";
68 const char const_char_buffer
[128] = "";
69 array
<boost::asio::mutable_buffer
, 2> mutable_buffers
= {{
70 boost::asio::buffer(mutable_char_buffer
, 10),
71 boost::asio::buffer(mutable_char_buffer
+ 10, 10) }};
72 array
<boost::asio::const_buffer
, 2> const_buffers
= {{
73 boost::asio::buffer(const_char_buffer
, 10),
74 boost::asio::buffer(const_char_buffer
+ 10, 10) }};
75 archetypes::lazy_handler lazy
;
76 boost::system::error_code ec
;
78 stream_type
stream1(ioc
);
79 stream_type
stream2(ioc
, 1024);
81 stream_type::executor_type ex
= stream1
.get_executor();
84 #if !defined(BOOST_ASIO_NO_DEPRECATED)
85 io_context
& ioc_ref
= stream1
.get_io_context();
88 io_context
& ioc_ref2
= stream1
.get_io_service();
90 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
92 stream_type::lowest_layer_type
& lowest_layer
= stream1
.lowest_layer();
95 stream1
.write_some(buffer(mutable_char_buffer
));
96 stream1
.write_some(buffer(const_char_buffer
));
97 stream1
.write_some(mutable_buffers
);
98 stream1
.write_some(const_buffers
);
99 stream1
.write_some(null_buffers());
100 stream1
.write_some(buffer(mutable_char_buffer
), ec
);
101 stream1
.write_some(buffer(const_char_buffer
), ec
);
102 stream1
.write_some(mutable_buffers
, ec
);
103 stream1
.write_some(const_buffers
, ec
);
104 stream1
.write_some(null_buffers(), ec
);
106 stream1
.async_write_some(buffer(mutable_char_buffer
), &write_some_handler
);
107 stream1
.async_write_some(buffer(const_char_buffer
), &write_some_handler
);
108 stream1
.async_write_some(mutable_buffers
, &write_some_handler
);
109 stream1
.async_write_some(const_buffers
, &write_some_handler
);
110 stream1
.async_write_some(null_buffers(), &write_some_handler
);
111 int i1
= stream1
.async_write_some(buffer(mutable_char_buffer
), lazy
);
113 int i2
= stream1
.async_write_some(buffer(const_char_buffer
), lazy
);
115 int i3
= stream1
.async_write_some(mutable_buffers
, lazy
);
117 int i4
= stream1
.async_write_some(const_buffers
, lazy
);
119 int i5
= stream1
.async_write_some(null_buffers(), lazy
);
125 stream1
.async_flush(&flush_handler
);
126 int i6
= stream1
.async_flush(lazy
);
129 stream1
.read_some(buffer(mutable_char_buffer
));
130 stream1
.read_some(mutable_buffers
);
131 stream1
.read_some(null_buffers());
132 stream1
.read_some(buffer(mutable_char_buffer
), ec
);
133 stream1
.read_some(mutable_buffers
, ec
);
134 stream1
.read_some(null_buffers(), ec
);
136 stream1
.async_read_some(buffer(mutable_char_buffer
), &read_some_handler
);
137 stream1
.async_read_some(mutable_buffers
, &read_some_handler
);
138 stream1
.async_read_some(null_buffers(), &read_some_handler
);
139 int i7
= stream1
.async_read_some(buffer(mutable_char_buffer
), lazy
);
141 int i8
= stream1
.async_read_some(mutable_buffers
, lazy
);
143 int i9
= stream1
.async_read_some(null_buffers(), lazy
);
146 catch (std::exception
&)
151 void test_sync_operations()
153 using namespace std
; // For memcmp.
155 boost::asio::io_context io_context
;
157 boost::asio::ip::tcp::acceptor
acceptor(io_context
,
158 boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
159 boost::asio::ip::tcp::endpoint server_endpoint
= acceptor
.local_endpoint();
160 server_endpoint
.address(boost::asio::ip::address_v4::loopback());
162 stream_type
client_socket(io_context
);
163 client_socket
.lowest_layer().connect(server_endpoint
);
165 stream_type
server_socket(io_context
);
166 acceptor
.accept(server_socket
.lowest_layer());
168 const char write_data
[]
169 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
170 const boost::asio::const_buffer write_buf
= boost::asio::buffer(write_data
);
172 std::size_t bytes_written
= 0;
173 while (bytes_written
< sizeof(write_data
))
175 bytes_written
+= client_socket
.write_some(
176 boost::asio::buffer(write_buf
+ bytes_written
));
177 client_socket
.flush();
180 char read_data
[sizeof(write_data
)];
181 const boost::asio::mutable_buffer read_buf
= boost::asio::buffer(read_data
);
183 std::size_t bytes_read
= 0;
184 while (bytes_read
< sizeof(read_data
))
186 bytes_read
+= server_socket
.read_some(
187 boost::asio::buffer(read_buf
+ bytes_read
));
190 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
191 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
192 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
195 while (bytes_written
< sizeof(write_data
))
197 bytes_written
+= server_socket
.write_some(
198 boost::asio::buffer(write_buf
+ bytes_written
));
199 server_socket
.flush();
203 while (bytes_read
< sizeof(read_data
))
205 bytes_read
+= client_socket
.read_some(
206 boost::asio::buffer(read_buf
+ bytes_read
));
209 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
210 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
211 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
213 server_socket
.close();
214 boost::system::error_code error
;
215 bytes_read
= client_socket
.read_some(
216 boost::asio::buffer(read_buf
), error
);
218 BOOST_ASIO_CHECK(bytes_read
== 0);
219 BOOST_ASIO_CHECK(error
== boost::asio::error::eof
);
221 client_socket
.close(error
);
224 void handle_accept(const boost::system::error_code
& e
)
226 BOOST_ASIO_CHECK(!e
);
229 void handle_write(const boost::system::error_code
& e
,
230 std::size_t bytes_transferred
,
231 std::size_t* total_bytes_written
)
233 BOOST_ASIO_CHECK(!e
);
235 throw boost::system::system_error(e
); // Terminate test.
236 *total_bytes_written
+= bytes_transferred
;
239 void handle_flush(const boost::system::error_code
& e
)
241 BOOST_ASIO_CHECK(!e
);
244 void handle_read(const boost::system::error_code
& e
,
245 std::size_t bytes_transferred
,
246 std::size_t* total_bytes_read
)
248 BOOST_ASIO_CHECK(!e
);
250 throw boost::system::system_error(e
); // Terminate test.
251 *total_bytes_read
+= bytes_transferred
;
254 void handle_read_eof(const boost::system::error_code
& e
,
255 std::size_t bytes_transferred
)
257 BOOST_ASIO_CHECK(e
== boost::asio::error::eof
);
258 BOOST_ASIO_CHECK(bytes_transferred
== 0);
261 void test_async_operations()
263 using namespace std
; // For memcmp.
265 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
266 namespace bindns
= boost
;
267 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
268 namespace bindns
= std
;
269 using std::placeholders::_1
;
270 using std::placeholders::_2
;
271 #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
273 boost::asio::io_context io_context
;
275 boost::asio::ip::tcp::acceptor
acceptor(io_context
,
276 boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
277 boost::asio::ip::tcp::endpoint server_endpoint
= acceptor
.local_endpoint();
278 server_endpoint
.address(boost::asio::ip::address_v4::loopback());
280 stream_type
client_socket(io_context
);
281 client_socket
.lowest_layer().connect(server_endpoint
);
283 stream_type
server_socket(io_context
);
284 acceptor
.async_accept(server_socket
.lowest_layer(), &handle_accept
);
286 io_context
.restart();
288 const char write_data
[]
289 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
290 const boost::asio::const_buffer write_buf
= boost::asio::buffer(write_data
);
292 std::size_t bytes_written
= 0;
293 while (bytes_written
< sizeof(write_data
))
295 client_socket
.async_write_some(
296 boost::asio::buffer(write_buf
+ bytes_written
),
297 bindns::bind(handle_write
, _1
, _2
, &bytes_written
));
299 io_context
.restart();
300 client_socket
.async_flush(
301 bindns::bind(handle_flush
, _1
));
303 io_context
.restart();
306 char read_data
[sizeof(write_data
)];
307 const boost::asio::mutable_buffer read_buf
= boost::asio::buffer(read_data
);
309 std::size_t bytes_read
= 0;
310 while (bytes_read
< sizeof(read_data
))
312 server_socket
.async_read_some(
313 boost::asio::buffer(read_buf
+ bytes_read
),
314 bindns::bind(handle_read
, _1
, _2
, &bytes_read
));
316 io_context
.restart();
319 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
320 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
321 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
324 while (bytes_written
< sizeof(write_data
))
326 server_socket
.async_write_some(
327 boost::asio::buffer(write_buf
+ bytes_written
),
328 bindns::bind(handle_write
, _1
, _2
, &bytes_written
));
330 io_context
.restart();
331 server_socket
.async_flush(
332 bindns::bind(handle_flush
, _1
));
334 io_context
.restart();
338 while (bytes_read
< sizeof(read_data
))
340 client_socket
.async_read_some(
341 boost::asio::buffer(read_buf
+ bytes_read
),
342 bindns::bind(handle_read
, _1
, _2
, &bytes_read
));
344 io_context
.restart();
347 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
348 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
349 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
351 server_socket
.close();
352 client_socket
.async_read_some(boost::asio::buffer(read_buf
), handle_read_eof
);
355 BOOST_ASIO_TEST_SUITE
357 "buffered_write_stream",
358 BOOST_ASIO_TEST_CASE(test_compile
)
359 BOOST_ASIO_TEST_CASE(test_sync_operations
)
360 BOOST_ASIO_TEST_CASE(test_async_operations
)