2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/beast
10 // Test that header file is self-contained.
11 #include <boost/beast/core/buffered_read_stream.hpp>
13 #include <boost/beast/core/multi_buffer.hpp>
14 #include <boost/beast/_experimental/test/stream.hpp>
15 #include <boost/beast/_experimental/unit_test/suite.hpp>
16 #include <boost/beast/core/bind_handler.hpp>
17 #include <boost/beast/test/yield_to.hpp>
18 #include <boost/asio/buffer.hpp>
19 #include <boost/asio/io_context.hpp>
20 #include <boost/asio/read.hpp>
21 #include <boost/asio/strand.hpp>
22 #include <boost/optional.hpp>
23 #if BOOST_ASIO_HAS_CO_AWAIT
24 #include <boost/asio/use_awaitable.hpp>
30 class buffered_read_stream_test
31 : public unit_test::suite
32 , public test::enable_yield_to
34 using self
= buffered_read_stream_test
;
37 void testSpecialMembers()
41 buffered_read_stream
<test::stream
, multi_buffer
> srs(ioc
);
42 buffered_read_stream
<test::stream
, multi_buffer
> srs2(std::move(srs
));
43 srs
= std::move(srs2
);
44 BEAST_EXPECT(&net::query(srs
.get_executor(), net::execution::context
) == &ioc
);
46 &net::query(srs
.get_executor(), net::execution::context
) ==
47 &net::query(srs2
.get_executor(), net::execution::context
));
51 buffered_read_stream
<test::stream
&, multi_buffer
> srs(ts
);
55 struct loop
: std::enable_shared_from_this
<loop
>
57 static std::size_t constexpr limit
= 100;
61 unit_test::suite
& suite_
;
62 net::io_context
& ioc_
;
63 boost::optional
<test::stream
> ts_
;
64 boost::optional
<test::fail_count
> fc_
;
65 boost::optional
<buffered_read_stream
<
66 test::stream
&, multi_buffer
>> brs_
;
69 unit_test::suite
& suite
,
85 on_read(error_code ec
, std::size_t)
90 "Hello, world!", __FILE__
, __LINE__
);
94 if(! suite_
.expect(n_
< limit
, __FILE__
, __LINE__
))
105 ts_
.emplace(ioc_
, *fc_
, ", world!");
107 brs_
->buffer().commit(net::buffer_copy(
108 brs_
->buffer().prepare(5), net::buffer("Hello", 5)));
109 net::async_read(*brs_
,
110 net::buffer(&s_
[0], s_
.size()),
113 shared_from_this()));
120 std::make_shared
<loop
>(*this, ioc_
, 0)->run();
121 std::make_shared
<loop
>(*this, ioc_
, 3)->run();
124 void testRead(yield_context do_yield
)
126 static std::size_t constexpr limit
= 100;
131 for(n
= 0; n
< limit
; ++n
)
133 test::fail_count fc
{n
};
134 test::stream
ts(ioc_
, fc
, ", world!");
135 buffered_read_stream
<
136 test::stream
&, multi_buffer
> srs(ts
);
137 srs
.buffer().commit(net::buffer_copy(
138 srs
.buffer().prepare(5), net::buffer("Hello", 5)));
139 error_code ec
= test::error::test_failure
;
140 net::read(srs
, net::buffer(&s
[0], s
.size()), ec
);
143 BEAST_EXPECT(s
== "Hello, world!");
147 BEAST_EXPECT(n
< limit
);
149 for(n
= 0; n
< limit
; ++n
)
151 test::fail_count fc
{n
};
152 test::stream
ts(ioc_
, fc
, ", world!");
153 buffered_read_stream
<
154 test::stream
&, multi_buffer
> srs(ts
);
156 srs
.buffer().commit(net::buffer_copy(
157 srs
.buffer().prepare(5), net::buffer("Hello", 5)));
158 error_code ec
= test::error::test_failure
;
159 net::read(srs
, net::buffer(&s
[0], s
.size()), ec
);
162 BEAST_EXPECT(s
== "Hello, world!");
166 BEAST_EXPECT(n
< limit
);
168 for(n
= 0; n
< limit
; ++n
)
170 test::fail_count fc
{n
};
171 test::stream
ts(ioc_
, fc
, ", world!");
172 buffered_read_stream
<
173 test::stream
&, multi_buffer
> srs(ts
);
174 srs
.buffer().commit(net::buffer_copy(
175 srs
.buffer().prepare(5), net::buffer("Hello", 5)));
176 error_code ec
= test::error::test_failure
;
178 srs
, net::buffer(&s
[0], s
.size()), do_yield
[ec
]);
181 BEAST_EXPECT(s
== "Hello, world!");
185 BEAST_EXPECT(n
< limit
);
187 for(n
= 0; n
< limit
; ++n
)
189 test::fail_count fc
{n
};
190 test::stream
ts(ioc_
, fc
, ", world!");
191 buffered_read_stream
<
192 test::stream
&, multi_buffer
> srs(ts
);
194 srs
.buffer().commit(net::buffer_copy(
195 srs
.buffer().prepare(5), net::buffer("Hello", 5)));
196 error_code ec
= test::error::test_failure
;
198 srs
, net::buffer(&s
[0], s
.size()), do_yield
[ec
]);
201 BEAST_EXPECT(s
== "Hello, world!");
205 BEAST_EXPECT(n
< limit
);
208 struct copyable_handler
210 template<class... Args
>
212 operator()(Args
&&...) const
217 #if BOOST_ASIO_HAS_CO_AWAIT
218 void testAwaitableCompiles(
219 buffered_read_stream
<test::stream
, flat_buffer
>& stream
,
220 net::mutable_buffer rxbuf
,
221 net::const_buffer txbuf
)
223 static_assert(std::is_same_v
<
224 net::awaitable
<std::size_t>, decltype(
225 stream
.async_read_some(rxbuf
, net::use_awaitable
))>);
227 static_assert(std::is_same_v
<
228 net::awaitable
<std::size_t>, decltype(
229 stream
.async_write_some(txbuf
, net::use_awaitable
))>);
235 testSpecialMembers();
237 yield_to([&](yield_context yield
)
243 #if BOOST_ASIO_HAS_CO_AWAIT
244 boost::ignore_unused(&buffered_read_stream_test::testAwaitableCompiles
);
249 BEAST_DEFINE_TESTSUITE(beast
,core
,buffered_read_stream
);