forked from boostorg/beast
websocket idle pings
This commit is contained in:
1 parent
62aa1b9717
commit
28d3b41a43
7 files changed
+260
-184
No files matched your search
@@ -15,9 +15,6 @@
|
||||
#include <boost/beast/_experimental/unit_test/suite.hpp>
|
||||
#include "test.hpp"
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
namespace websocket {
|
||||
|
||||
@@ -238,96 +238,6 @@ public:
|
||||
);
|
||||
}
|
||||
|
||||
void
|
||||
testTimeout()
|
||||
{
|
||||
using tcp = net::ip::tcp;
|
||||
|
||||
net::io_context ioc;
|
||||
|
||||
// success
|
||||
|
||||
{
|
||||
stream<tcp::socket> ws1(ioc);
|
||||
stream<tcp::socket> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.async_handshake("test", "/", test::success_handler());
|
||||
ws2.async_accept(test::success_handler());
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
{
|
||||
stream<test::stream> ws1(ioc);
|
||||
stream<test::stream> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.async_handshake("test", "/", test::success_handler());
|
||||
ws2.async_accept(test::success_handler());
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
// success, timeout enabled
|
||||
|
||||
{
|
||||
stream<tcp::socket> ws1(ioc);
|
||||
stream<tcp::socket> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.set_option(stream_base::timeout{
|
||||
std::chrono::milliseconds(50),
|
||||
stream_base::none(),
|
||||
false});
|
||||
ws1.async_handshake("test", "/", test::success_handler());
|
||||
ws2.async_accept(test::success_handler());
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
{
|
||||
stream<test::stream> ws1(ioc);
|
||||
stream<test::stream> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.set_option(stream_base::timeout{
|
||||
std::chrono::milliseconds(50),
|
||||
stream_base::none(),
|
||||
false});
|
||||
ws1.async_handshake("test", "/", test::success_handler());
|
||||
ws2.async_accept(test::success_handler());
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
// timeout
|
||||
|
||||
{
|
||||
stream<tcp::socket> ws1(ioc);
|
||||
stream<tcp::socket> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.set_option(stream_base::timeout{
|
||||
std::chrono::milliseconds(50),
|
||||
stream_base::none(),
|
||||
false});
|
||||
ws1.async_handshake("test", "/",
|
||||
test::fail_handler(beast::error::timeout));
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
{
|
||||
stream<test::stream> ws1(ioc);
|
||||
stream<test::stream> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.set_option(stream_base::timeout{
|
||||
std::chrono::milliseconds(50),
|
||||
stream_base::none(),
|
||||
false});
|
||||
ws1.async_handshake("test", "/",
|
||||
test::fail_handler(beast::error::timeout));
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
// Compression Extensions for WebSocket
|
||||
//
|
||||
// https://tools.ietf.org/html/rfc7692
|
||||
@@ -597,15 +507,117 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
testAsync()
|
||||
{
|
||||
using tcp = net::ip::tcp;
|
||||
|
||||
net::io_context ioc;
|
||||
|
||||
// success, no timeout
|
||||
|
||||
{
|
||||
stream<tcp::socket> ws1(ioc);
|
||||
stream<tcp::socket> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.async_handshake("test", "/", test::success_handler());
|
||||
ws2.async_accept(test::success_handler());
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
{
|
||||
stream<test::stream> ws1(ioc);
|
||||
stream<test::stream> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.async_handshake("test", "/", test::success_handler());
|
||||
ws2.async_accept(test::success_handler());
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
// success, timeout enabled
|
||||
|
||||
{
|
||||
stream<tcp::socket> ws1(ioc);
|
||||
stream<tcp::socket> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.set_option(stream_base::timeout{
|
||||
std::chrono::milliseconds(50),
|
||||
stream_base::none(),
|
||||
false});
|
||||
ws1.async_handshake("test", "/", test::success_handler());
|
||||
ws2.async_accept(test::success_handler());
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
{
|
||||
stream<test::stream> ws1(ioc);
|
||||
stream<test::stream> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.set_option(stream_base::timeout{
|
||||
std::chrono::milliseconds(50),
|
||||
stream_base::none(),
|
||||
false});
|
||||
ws1.async_handshake("test", "/", test::success_handler());
|
||||
ws2.async_accept(test::success_handler());
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
// timeout
|
||||
|
||||
{
|
||||
stream<tcp::socket> ws1(ioc);
|
||||
stream<tcp::socket> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.set_option(stream_base::timeout{
|
||||
std::chrono::milliseconds(50),
|
||||
stream_base::none(),
|
||||
false});
|
||||
ws1.async_handshake("test", "/",
|
||||
test::fail_handler(beast::error::timeout));
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
{
|
||||
stream<test::stream> ws1(ioc);
|
||||
stream<test::stream> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
|
||||
ws1.set_option(stream_base::timeout{
|
||||
std::chrono::milliseconds(50),
|
||||
stream_base::none(),
|
||||
false});
|
||||
ws1.async_handshake("test", "/",
|
||||
test::fail_handler(beast::error::timeout));
|
||||
test::run_for(ioc, std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
// abandoned operation
|
||||
|
||||
{
|
||||
{
|
||||
stream<tcp::socket> ws1(ioc);
|
||||
ws1.async_handshake("test", "/",
|
||||
test::fail_handler(
|
||||
net::error::operation_aborted));
|
||||
}
|
||||
test::run(ioc);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testHandshake();
|
||||
testTimeout();
|
||||
testExtRead();
|
||||
testExtWrite();
|
||||
testExtNegotiate();
|
||||
testMoveOnly();
|
||||
testAsync();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -10,24 +10,85 @@
|
||||
// Test that header file is self-contained.
|
||||
#include <boost/beast/websocket/stream.hpp>
|
||||
|
||||
#include <boost/beast/_experimental/test/stream.hpp>
|
||||
#include <boost/beast/_experimental/test/tcp.hpp>
|
||||
#include <boost/beast/_experimental/unit_test/suite.hpp>
|
||||
#include <boost/beast/core/flat_buffer.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include "test.hpp"
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
namespace websocket {
|
||||
|
||||
class timer_test
|
||||
: public websocket_test_suite
|
||||
struct timer_test : unit_test::suite
|
||||
{
|
||||
public:
|
||||
using tcp = boost::asio::ip::tcp;
|
||||
|
||||
void
|
||||
testIdlePing()
|
||||
{
|
||||
net::io_context ioc;
|
||||
|
||||
// idle ping, no timeout
|
||||
|
||||
{
|
||||
stream<tcp::socket> ws1(ioc);
|
||||
stream<tcp::socket> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
ws1.async_accept(test::success_handler());
|
||||
ws2.async_handshake("test", "/", test::success_handler());
|
||||
test::run(ioc);
|
||||
|
||||
ws2.set_option(stream_base::timeout{
|
||||
stream_base::none(),
|
||||
std::chrono::milliseconds(50),
|
||||
true});
|
||||
flat_buffer b1;
|
||||
flat_buffer b2;
|
||||
bool received = false;
|
||||
ws1.control_callback(
|
||||
[&received](frame_type ft, string_view)
|
||||
{
|
||||
received = true;
|
||||
BEAST_EXPECT(ft == frame_type::ping);
|
||||
});
|
||||
ws1.async_read(b1, test::fail_handler(
|
||||
net::error::operation_aborted));
|
||||
ws2.async_read(b2, test::fail_handler(
|
||||
net::error::operation_aborted));
|
||||
test::run_for(ioc, std::chrono::milliseconds(100));
|
||||
BEAST_EXPECT(received);
|
||||
}
|
||||
|
||||
test::run(ioc);
|
||||
|
||||
// idle ping, timeout
|
||||
|
||||
{
|
||||
stream<tcp::socket> ws1(ioc);
|
||||
stream<tcp::socket> ws2(ioc);
|
||||
test::connect(ws1.next_layer(), ws2.next_layer());
|
||||
ws1.async_accept(test::success_handler());
|
||||
ws2.async_handshake("test", "/", test::success_handler());
|
||||
test::run(ioc);
|
||||
|
||||
ws2.set_option(stream_base::timeout{
|
||||
stream_base::none(),
|
||||
std::chrono::milliseconds(50),
|
||||
true});
|
||||
flat_buffer b;
|
||||
ws2.async_read(b,
|
||||
test::fail_handler(beast::error::timeout));
|
||||
test::run(ioc);
|
||||
}
|
||||
|
||||
test::run(ioc);
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
pass();
|
||||
testIdlePing();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user