From 0d83f11ba9a9d3451591b553541660b1bcc424f8 Mon Sep 17 00:00:00 2001 From: Morten Minde Neergaard <169057+xim@users.noreply.github.com> Date: Wed, 30 Aug 2023 13:58:10 +0200 Subject: [PATCH] ping: Make tests that verify current behaviour --- test/beast/websocket/ping.cpp | 63 +++++++++++++++++++++++++++++++++++ test/beast/websocket/test.hpp | 6 ++++ 2 files changed, 69 insertions(+) diff --git a/test/beast/websocket/ping.cpp b/test/beast/websocket/ping.cpp index 0bfb5586f4..049f5eb770 100644 --- a/test/beast/websocket/ping.cpp +++ b/test/beast/websocket/ping.cpp @@ -88,6 +88,69 @@ class ping_test : public websocket_test_suite se.code().message()); } } + + // inactivity timeout doesn't happen when you get pings + { + echo_server es{log}; + stream ws{ioc_}; + + // We have an inactivity timeout of 1s, but don't send pings + ws.set_option(stream_base::timeout{ + stream_base::none(), + std::chrono::seconds(1), + false + }); + ws.next_layer().connect(es.stream()); + ws.handshake("localhost", "/"); + flat_buffer b; + bool got_timeout = false; + ws.async_read(b, + [&](error_code ec, std::size_t) + { + if(ec != beast::error::timeout) + BOOST_THROW_EXCEPTION( + system_error{ec}); + got_timeout = true; + }); + // We are connected, base state + BEAST_EXPECT(ws.impl_->idle_counter == 0); + test::run_for(ioc_, std::chrono::milliseconds(600)); + // After 600ms idle, no timeout + BEAST_EXPECT(ws.impl_->idle_counter == 1); + es.async_ping(); + test::run_for(ioc_, std::chrono::milliseconds(250)); + // The server sent a ping, and we haven't been idle long, so back to base state. + BEAST_EXPECT(ws.impl_->idle_counter == 0); + BEAST_EXPECT(!got_timeout); + // After another second, we should have gotten a timeout + test::run_for(ioc_, std::chrono::seconds(1)); + BEAST_EXPECT(got_timeout); + } + + // inactivity timeout doesn't happen when you send pings + { + echo_server es{log}; + stream ws{ioc_}; + ws.set_option(stream_base::timeout{ + stream_base::none(), + std::chrono::milliseconds(200), + true + }); + unsigned n_pongs = 0; + ws.control_callback({[&](frame_type kind, string_view) + { + if (kind == frame_type::pong) + ++n_pongs; + }}); + ws.next_layer().connect(es.stream()); + ws.handshake("localhost", "/"); + flat_buffer b; + ws.async_read(b, test::fail_handler(asio::error::operation_aborted)); + // We are connected, base state + test::run_for(ioc_, std::chrono::seconds(1)); + // About a second later, we should have close to 10 pings/pongs, and no timeout + BEAST_EXPECT(7 < n_pongs && n_pongs <= 10); + } } void diff --git a/test/beast/websocket/test.hpp b/test/beast/websocket/test.hpp index 1575db32bb..3ea25ec23a 100644 --- a/test/beast/websocket/test.hpp +++ b/test/beast/websocket/test.hpp @@ -134,6 +134,12 @@ class websocket_test_suite this)); } + void + async_ping() + { + ws_.async_ping("", [](error_code){}); + } + void async_close() {