Files
beast/test/core/buffered_read_stream.cpp
T

144 lines
4.3 KiB
C++
Raw Normal View History

2016-04-01 11:46:32 -04:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-04-01 11:46:32 -04:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Test that header file is self-contained.
#include <beast/core/buffered_read_stream.hpp>
2016-04-30 13:00:33 -04:00
#include <beast/core/multi_buffer.hpp>
2016-05-07 17:06:46 -04:00
#include <beast/test/fail_stream.hpp>
2017-01-09 11:13:19 -05:00
#include <beast/test/string_istream.hpp>
2016-05-07 17:06:46 -04:00
#include <beast/test/yield_to.hpp>
2016-05-06 19:14:17 -04:00
#include <beast/unit_test/suite.hpp>
2016-04-30 13:00:33 -04:00
#include <boost/asio.hpp>
namespace beast {
class buffered_read_stream_test
2016-05-07 17:06:46 -04:00
: public unit_test::suite
, public test::enable_yield_to
2016-04-30 13:00:33 -04:00
{
using self = buffered_read_stream_test;
2016-05-07 17:06:46 -04:00
2016-04-30 13:00:33 -04:00
public:
2016-05-07 17:06:46 -04:00
void testSpecialMembers()
2016-04-30 13:00:33 -04:00
{
using socket_type = boost::asio::ip::tcp::socket;
boost::asio::io_service ios;
{
buffered_read_stream<socket_type, multi_buffer> srs(ios);
buffered_read_stream<socket_type, multi_buffer> srs2(std::move(srs));
2016-04-30 13:00:33 -04:00
srs = std::move(srs2);
2016-08-03 15:34:23 -04:00
BEAST_EXPECT(&srs.get_io_service() == &ios);
BEAST_EXPECT(&srs.get_io_service() == &srs2.get_io_service());
2016-04-30 13:00:33 -04:00
}
{
socket_type sock(ios);
buffered_read_stream<socket_type&, multi_buffer> srs(sock);
buffered_read_stream<socket_type&, multi_buffer> srs2(std::move(srs));
2016-04-30 13:00:33 -04:00
}
2016-05-07 17:06:46 -04:00
}
void testRead(yield_context do_yield)
{
using boost::asio::buffer;
using boost::asio::buffer_copy;
static std::size_t constexpr limit = 100;
std::size_t n;
std::string s;
s.resize(13);
for(n = 0; n < limit; ++n)
{
test::fail_stream<
2017-01-09 11:13:19 -05:00
test::string_istream> fs(n, ios_, ", world!");
buffered_read_stream<
decltype(fs)&, multi_buffer> srs(fs);
2016-05-07 17:06:46 -04:00
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
2016-10-04 18:00:11 -04:00
error_code ec;
2016-05-07 17:06:46 -04:00
boost::asio::read(srs, buffer(&s[0], s.size()), ec);
if(! ec)
{
2016-08-03 15:34:23 -04:00
BEAST_EXPECT(s == "Hello, world!");
2016-05-07 17:06:46 -04:00
break;
}
}
2016-08-03 15:34:23 -04:00
BEAST_EXPECT(n < limit);
2016-05-07 17:06:46 -04:00
for(n = 0; n < limit; ++n)
{
test::fail_stream<
2017-01-09 11:13:19 -05:00
test::string_istream> fs(n, ios_, ", world!");
buffered_read_stream<
decltype(fs)&, multi_buffer> srs(fs);
2016-05-07 17:06:46 -04:00
srs.capacity(3);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
2016-10-04 18:00:11 -04:00
error_code ec;
2016-05-07 17:06:46 -04:00
boost::asio::read(srs, buffer(&s[0], s.size()), ec);
if(! ec)
{
2016-08-03 15:34:23 -04:00
BEAST_EXPECT(s == "Hello, world!");
2016-05-07 17:06:46 -04:00
break;
}
}
2016-08-03 15:34:23 -04:00
BEAST_EXPECT(n < limit);
2016-05-07 17:06:46 -04:00
for(n = 0; n < limit; ++n)
{
test::fail_stream<
2017-01-09 11:13:19 -05:00
test::string_istream> fs(n, ios_, ", world!");
buffered_read_stream<
decltype(fs)&, multi_buffer> srs(fs);
2016-05-07 17:06:46 -04:00
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
2016-10-04 18:00:11 -04:00
error_code ec;
2016-05-07 17:06:46 -04:00
boost::asio::async_read(
srs, buffer(&s[0], s.size()), do_yield[ec]);
if(! ec)
{
2016-08-03 15:34:23 -04:00
BEAST_EXPECT(s == "Hello, world!");
2016-05-07 17:06:46 -04:00
break;
}
}
2016-08-03 15:34:23 -04:00
BEAST_EXPECT(n < limit);
2016-05-07 17:06:46 -04:00
for(n = 0; n < limit; ++n)
{
test::fail_stream<
2017-01-09 11:13:19 -05:00
test::string_istream> fs(n, ios_, ", world!");
buffered_read_stream<
decltype(fs)&, multi_buffer> srs(fs);
2016-05-07 17:06:46 -04:00
srs.capacity(3);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
2016-10-04 18:00:11 -04:00
error_code ec;
2016-05-07 17:06:46 -04:00
boost::asio::async_read(
srs, buffer(&s[0], s.size()), do_yield[ec]);
if(! ec)
{
2016-08-03 15:34:23 -04:00
BEAST_EXPECT(s == "Hello, world!");
2016-05-07 17:06:46 -04:00
break;
}
}
2016-08-03 15:34:23 -04:00
BEAST_EXPECT(n < limit);
2016-04-30 13:00:33 -04:00
}
void run() override
{
2016-05-07 17:06:46 -04:00
testSpecialMembers();
2017-05-24 19:42:24 -07:00
yield_to([&](yield_context yield){
testRead(yield);});
2016-04-30 13:00:33 -04:00
}
};
BEAST_DEFINE_TESTSUITE(buffered_read_stream,core,beast);
2016-04-30 13:00:33 -04:00
} // beast