forked from boostorg/beast
Refactor websocket stream: fixes and tests
This commit is contained in:
+306
-60
@@ -124,68 +124,313 @@ public:
|
||||
void
|
||||
testPingSuspend()
|
||||
{
|
||||
echo_server es{log, kind::async};
|
||||
boost::asio::io_service ios;
|
||||
stream<test::stream> ws{ios};
|
||||
ws.next_layer().connect(es.stream());
|
||||
ws.handshake("localhost", "/");
|
||||
|
||||
// Cause close to be received
|
||||
es.async_close();
|
||||
|
||||
multi_buffer b;
|
||||
std::size_t count = 0;
|
||||
// Read a close frame.
|
||||
// Sends a close frame, blocking writes.
|
||||
ws.async_read(b,
|
||||
[&](error_code ec, std::size_t)
|
||||
{
|
||||
// Read should complete with error::closed
|
||||
++count;
|
||||
BEAST_EXPECTS(ec == error::closed,
|
||||
ec.message());
|
||||
// Pings after a close are aborted
|
||||
ws.async_ping("",
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
BEAST_EXPECTS(ec == boost::asio::
|
||||
error::operation_aborted,
|
||||
ec.message());
|
||||
});
|
||||
});
|
||||
if(! BEAST_EXPECT(run_until(ios, 100,
|
||||
[&]{ return ws.wr_close_; })))
|
||||
return;
|
||||
// Try to ping
|
||||
ws.async_ping("payload",
|
||||
[&](error_code ec)
|
||||
{
|
||||
// Pings after a close are aborted
|
||||
++count;
|
||||
BEAST_EXPECTS(ec == boost::asio::
|
||||
error::operation_aborted,
|
||||
ec.message());
|
||||
// Subsequent calls to close are aborted
|
||||
ws.async_close({},
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
BEAST_EXPECTS(ec == boost::asio::
|
||||
error::operation_aborted,
|
||||
ec.message());
|
||||
});
|
||||
});
|
||||
static std::size_t constexpr limit = 100;
|
||||
std::size_t n;
|
||||
for(n = 0; n < limit; ++n)
|
||||
// suspend on write
|
||||
doFailLoop([&](test::fail_counter& fc)
|
||||
{
|
||||
if(count >= 4)
|
||||
break;
|
||||
ios.run_one();
|
||||
echo_server es{log};
|
||||
boost::asio::io_service ios;
|
||||
stream<test::stream> ws{ios, fc};
|
||||
ws.next_layer().connect(es.stream());
|
||||
ws.handshake("localhost", "/");
|
||||
std::size_t count = 0;
|
||||
ws.async_write(sbuf("Hello, world"),
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
BEAST_EXPECT(ws.wr_block_);
|
||||
BEAST_EXPECT(count == 0);
|
||||
ws.async_ping({},
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
BEAST_EXPECT(count == 0);
|
||||
ios.run();
|
||||
BEAST_EXPECT(count == 2);
|
||||
});
|
||||
|
||||
// suspend on close
|
||||
doFailLoop([&](test::fail_counter& fc)
|
||||
{
|
||||
echo_server es{log};
|
||||
boost::asio::io_service ios;
|
||||
stream<test::stream> ws{ios, fc};
|
||||
ws.next_layer().connect(es.stream());
|
||||
ws.handshake("localhost", "/");
|
||||
std::size_t count = 0;
|
||||
ws.async_close({},
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
BEAST_EXPECT(ws.wr_block_);
|
||||
BEAST_EXPECT(count == 0);
|
||||
ws.async_ping({},
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
if(ec != boost::asio::error::operation_aborted)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
BEAST_EXPECT(count == 0);
|
||||
ios.run();
|
||||
BEAST_EXPECT(count == 2);
|
||||
});
|
||||
|
||||
// suspend on read ping + message
|
||||
doFailLoop([&](test::fail_counter& fc)
|
||||
{
|
||||
echo_server es{log};
|
||||
boost::asio::io_service ios;
|
||||
stream<test::stream> ws{ios, fc};
|
||||
ws.next_layer().connect(es.stream());
|
||||
ws.handshake("localhost", "/");
|
||||
// add a ping and message to the input
|
||||
ws.next_layer().append(string_view{
|
||||
"\x89\x00" "\x81\x01*", 5});
|
||||
std::size_t count = 0;
|
||||
multi_buffer b;
|
||||
ws.async_read(b,
|
||||
[&](error_code ec, std::size_t)
|
||||
{
|
||||
++count;
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
while(! ws.wr_block_)
|
||||
{
|
||||
ios.run_one();
|
||||
if(! BEAST_EXPECT(! ios.stopped()))
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(count == 0);
|
||||
ws.async_ping({},
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
BEAST_EXPECT(count == 0);
|
||||
ios.run();
|
||||
BEAST_EXPECT(count == 2);
|
||||
});
|
||||
|
||||
// suspend on read bad message
|
||||
doFailLoop([&](test::fail_counter& fc)
|
||||
{
|
||||
echo_server es{log};
|
||||
boost::asio::io_service ios;
|
||||
stream<test::stream> ws{ios, fc};
|
||||
ws.next_layer().connect(es.stream());
|
||||
ws.handshake("localhost", "/");
|
||||
// add an invalid frame to the input
|
||||
ws.next_layer().append(string_view{
|
||||
"\x09\x00", 2});
|
||||
|
||||
std::size_t count = 0;
|
||||
multi_buffer b;
|
||||
ws.async_read(b,
|
||||
[&](error_code ec, std::size_t)
|
||||
{
|
||||
++count;
|
||||
if(ec != error::failed)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
while(! ws.wr_block_)
|
||||
{
|
||||
ios.run_one();
|
||||
if(! BEAST_EXPECT(! ios.stopped()))
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(count == 0);
|
||||
ws.async_ping({},
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
if(ec != boost::asio::error::operation_aborted)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
BEAST_EXPECT(count == 0);
|
||||
ios.run();
|
||||
BEAST_EXPECT(count == 2);
|
||||
});
|
||||
|
||||
// suspend on read close #1
|
||||
doFailLoop([&](test::fail_counter& fc)
|
||||
{
|
||||
echo_server es{log};
|
||||
boost::asio::io_service ios;
|
||||
stream<test::stream> ws{ios, fc};
|
||||
ws.next_layer().connect(es.stream());
|
||||
ws.handshake("localhost", "/");
|
||||
// add a close frame to the input
|
||||
ws.next_layer().append(string_view{
|
||||
"\x88\x00", 2});
|
||||
std::size_t count = 0;
|
||||
multi_buffer b;
|
||||
ws.async_read(b,
|
||||
[&](error_code ec, std::size_t)
|
||||
{
|
||||
++count;
|
||||
if(ec != error::closed)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
while(! ws.wr_block_)
|
||||
{
|
||||
ios.run_one();
|
||||
if(! BEAST_EXPECT(! ios.stopped()))
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(count == 0);
|
||||
ws.async_ping({},
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
if(ec != boost::asio::error::operation_aborted)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
BEAST_EXPECT(count == 0);
|
||||
ios.run();
|
||||
BEAST_EXPECT(count == 2);
|
||||
});
|
||||
|
||||
// suspend on read close #2
|
||||
doFailLoop([&](test::fail_counter& fc)
|
||||
{
|
||||
echo_server es{log, kind::async};
|
||||
boost::asio::io_service ios;
|
||||
stream<test::stream> ws{ios, fc};
|
||||
ws.next_layer().connect(es.stream());
|
||||
ws.handshake("localhost", "/");
|
||||
// Cause close to be received
|
||||
es.async_close();
|
||||
std::size_t count = 0;
|
||||
multi_buffer b;
|
||||
ws.async_read(b,
|
||||
[&](error_code ec, std::size_t)
|
||||
{
|
||||
++count;
|
||||
if(ec != error::closed)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
while(! ws.wr_block_)
|
||||
{
|
||||
ios.run_one();
|
||||
if(! BEAST_EXPECT(! ios.stopped()))
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(count == 0);
|
||||
ws.async_ping({},
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
if(ec != boost::asio::error::operation_aborted)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
system_error{ec});
|
||||
});
|
||||
BEAST_EXPECT(count == 0);
|
||||
ios.run();
|
||||
BEAST_EXPECT(count == 2);
|
||||
});
|
||||
|
||||
{
|
||||
echo_server es{log, kind::async};
|
||||
boost::asio::io_service ios;
|
||||
stream<test::stream> ws{ios};
|
||||
ws.next_layer().connect(es.stream());
|
||||
ws.handshake("localhost", "/");
|
||||
|
||||
// Cause close to be received
|
||||
es.async_close();
|
||||
|
||||
multi_buffer b;
|
||||
std::size_t count = 0;
|
||||
// Read a close frame.
|
||||
// Sends a close frame, blocking writes.
|
||||
ws.async_read(b,
|
||||
[&](error_code ec, std::size_t)
|
||||
{
|
||||
// Read should complete with error::closed
|
||||
++count;
|
||||
BEAST_EXPECTS(ec == error::closed,
|
||||
ec.message());
|
||||
// Pings after a close are aborted
|
||||
ws.async_ping("",
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
BEAST_EXPECTS(ec == boost::asio::
|
||||
error::operation_aborted,
|
||||
ec.message());
|
||||
});
|
||||
});
|
||||
if(! BEAST_EXPECT(run_until(ios, 100,
|
||||
[&]{ return ws.wr_close_; })))
|
||||
return;
|
||||
// Try to ping
|
||||
ws.async_ping("payload",
|
||||
[&](error_code ec)
|
||||
{
|
||||
// Pings after a close are aborted
|
||||
++count;
|
||||
BEAST_EXPECTS(ec == boost::asio::
|
||||
error::operation_aborted,
|
||||
ec.message());
|
||||
// Subsequent calls to close are aborted
|
||||
ws.async_close({},
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
BEAST_EXPECTS(ec == boost::asio::
|
||||
error::operation_aborted,
|
||||
ec.message());
|
||||
});
|
||||
});
|
||||
static std::size_t constexpr limit = 100;
|
||||
std::size_t n;
|
||||
for(n = 0; n < limit; ++n)
|
||||
{
|
||||
if(count >= 4)
|
||||
break;
|
||||
ios.run_one();
|
||||
}
|
||||
BEAST_EXPECT(n < limit);
|
||||
ios.run();
|
||||
}
|
||||
BEAST_EXPECT(n < limit);
|
||||
ios.run();
|
||||
}
|
||||
|
||||
void
|
||||
testContHook()
|
||||
{
|
||||
struct handler
|
||||
{
|
||||
void operator()(error_code) {}
|
||||
};
|
||||
|
||||
stream<test::stream> ws{ios_};
|
||||
stream<test::stream>::ping_op<handler> op{
|
||||
handler{}, ws, detail::opcode::ping, {}};
|
||||
using boost::asio::asio_handler_is_continuation;
|
||||
asio_handler_is_continuation(&op);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -193,6 +438,7 @@ public:
|
||||
{
|
||||
testPing();
|
||||
testPingSuspend();
|
||||
testContHook();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user