Skip to content

Commit

Permalink
fixup! ping: Make tests that verify current behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
xim committed Oct 4, 2023
1 parent 767a0ee commit a76a8b9
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions test/beast/websocket/ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ class ping_test : public websocket_test_suite
echo_server es{log};
stream<test::stream> ws{ioc_};

// We have an inactivity timeout of 1s, but don't send pings
// We have an inactivity timeout of 1600ms, but don't send pings
ws.set_option(stream_base::timeout{
stream_base::none(),
std::chrono::seconds(1),
std::chrono::milliseconds(1600),
false
});
ws.next_layer().connect(es.stream());
Expand All @@ -114,16 +114,26 @@ class ping_test : public websocket_test_suite
});
// We are connected, base state
BEAST_EXPECT(ws.impl_->idle_counter == 0);
test::run_for(ioc_, std::chrono::milliseconds(740));
// After 600ms idle, no timeout

test::run_for(ioc_, std::chrono::milliseconds(1000));
// After 1s idle, no timeout but idle counter is 1
BEAST_EXPECT(ws.impl_->idle_counter == 1);

es.async_ping();
test::run_for(ioc_, std::chrono::milliseconds(240));
// The server sent a ping, and we haven't been idle long, so back to base state.
test::run_for(ioc_, std::chrono::milliseconds(400));
// The server sent a ping at 1s mark, and we're now at 1400ms mark.
// We haven't hit the idle timer yet (happens at 800ms, 1600ms, 2400ms)
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));

test::run_for(ioc_, std::chrono::milliseconds(600));
// At 2s total; should have triggered the idle timer
BEAST_EXPECT(ws.impl_->idle_counter == 1);
BEAST_EXPECT(!got_timeout);

test::run_for(ioc_, std::chrono::milliseconds(600));
// At 2.6s total; should have triggered the idle timer again without
// activity and triggered timeout.
BEAST_EXPECT(got_timeout);
}

Expand All @@ -133,7 +143,7 @@ class ping_test : public websocket_test_suite
stream<test::stream> ws{ioc_};
ws.set_option(stream_base::timeout{
stream_base::none(),
std::chrono::milliseconds(400),
std::chrono::milliseconds(600),
true
});
unsigned n_pongs = 0;
Expand All @@ -149,7 +159,7 @@ class ping_test : public websocket_test_suite
// We are connected, base state
test::run_for(ioc_, std::chrono::seconds(1));
// About a second later, we should have close to 5 pings/pongs, and no timeout
BEAST_EXPECTS(3 <= n_pongs && n_pongs <= 5, "Unexpected nr of pings: " + std::to_string(n_pongs));
BEAST_EXPECTS(2 <= n_pongs && n_pongs <= 3, "Unexpected nr of pings: " + std::to_string(n_pongs));
}
}

Expand Down

0 comments on commit a76a8b9

Please sign in to comment.