From fe4cf49fcd16619f95205eda7511c59660179271 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 26 Nov 2026 20:41:49 -0500 Subject: [PATCH] Check bounded WebSocket reads by their timeouts, not by wall time check_bounded_read_resumes() sent each piece 200 ms after the last and required every read() to return within 150 ms, which a stalled macOS runner missed (167 ms, 174 ms). The sender now waits, after each piece is sent, until read() has timed out twice, so a read() that waits for the rest of the message still fails the test and a slow machine only waits longer. TCP_NODELAY keeps Nagle's algorithm from holding a piece back until the next, and the two tests drop from about 1.4 s to 70 ms. --- test/test.cc | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/test/test.cc b/test/test.cc index afff7252..41269b4f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -31390,12 +31390,13 @@ TEST(WebSocketTest, StopMidBoundedMessageClosesWithGoingAway) { // A handler that bounds its reads gets control back within its timeout even // while a message arrives in pieces, and the message whole once the last has // come: what was read of it is kept across the timeouts. Each of `pieces` is -// sent a while after the one before, much longer than the handler's timeout. +// sent once read() has timed out twice since the one before was sent, so the +// second timeout came from a read() that had that piece to take. static void check_bounded_read_resumes(const std::vector &pieces, const std::string &expected) { std::atomic done{false}; + std::atomic timeouts{0}; std::string received; - std::chrono::steady_clock::duration longest{}; Server svr; svr.set_websocket_ping_interval(0); @@ -31403,11 +31404,9 @@ static void check_bounded_read_resumes(const std::vector &pieces, ws.set_read_timeout(0, 10000); std::string msg; ws::ReadResult r; - do { - auto began = std::chrono::steady_clock::now(); - r = ws.read(msg); - longest = (std::max)(longest, std::chrono::steady_clock::now() - began); - } while (r == ws::Timeout); + while ((r = ws.read(msg)) == ws::Timeout) { + timeouts++; + } if (r == ws::Text) { received = msg; } done = true; }); @@ -31423,19 +31422,24 @@ static void check_bounded_read_resumes(const std::vector &pieces, auto sock = connect_raw_websocket(port, "/ws"); ASSERT_NE(INVALID_SOCKET, sock); auto se_sock = detail::scope_exit([&] { detail::close_socket(sock); }); - for (const auto &piece : pieces) { - ASSERT_EQ(static_cast(piece.size()), - detail::send_socket(sock, piece.data(), piece.size(), 0)); - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + // Each piece goes out at once, not held back by Nagle's algorithm + ASSERT_TRUE(set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1)); + for (size_t i = 0; i < pieces.size(); i++) { + ASSERT_EQ(static_cast(pieces[i].size()), + detail::send_socket(sock, pieces[i].data(), pieces[i].size(), 0)); + if (i + 1 == pieces.size()) { break; } + auto before = timeouts.load(); + for (int j = 0; j < 5000 && timeouts < before + 2; j++) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + ASSERT_GE(timeouts.load(), before + 2) << "piece " << i; } - for (int i = 0; i < 500 && !done; i++) { - std::this_thread::sleep_for(std::chrono::milliseconds(10)); + for (int i = 0; i < 5000 && !done; i++) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } ASSERT_TRUE(done); EXPECT_EQ(expected, received); - // Far under the 200 ms between the pieces, however loaded the machine - EXPECT_LT(longest, std::chrono::milliseconds(150)); } TEST(WebSocketTest, BoundedReadResumesAFrameInPieces) {