Skip to content

Commit

Permalink
ping: Make tests that verify current behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
xim committed Aug 30, 2023
1 parent 6667dbc commit 0d83f11
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
63 changes: 63 additions & 0 deletions test/beast/websocket/ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<test::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<test::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
Expand Down
6 changes: 6 additions & 0 deletions test/beast/websocket/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ class websocket_test_suite
this));
}

void
async_ping()
{
ws_.async_ping("", [](error_code){});
}

void
async_close()
{
Expand Down

0 comments on commit 0d83f11

Please sign in to comment.