Copilot commented on code in PR #3749:
URL: https://github.com/apache/brpc/pull/3749#discussion_r4584139026


##########
test/brpc_http_rpc_protocol_unittest.cpp:
##########
@@ -2285,6 +2287,161 @@ TEST_F(HttpTest, 
http2_deferred_response_notify_registration_failure_resets_stre
     EXPECT_EQ(0, bthread_id_error(dummy_id, 0));
 }
 
+TEST(Http2FlowControlTest, oversized_body_returns_connection_window) {
+    brpc::GlobalInitializeOrDie();
+    GFLAGS_NAMESPACE::FlagSaver flag_saver;
+    brpc::FLAGS_max_body_size = 4;
+
+    for (bool client_side : {false, true}) {
+        SCOPED_TRACE(client_side);
+        int fds[2];
+        ASSERT_EQ(0, pipe(fds));
+        butil::fd_guard read_fd(fds[0]);
+        brpc::SocketOptions options;
+        options.fd = fds[1];
+        if (client_side) {
+            options.user = brpc::get_client_side_messenger();
+        }
+        brpc::SocketId id;
+        ASSERT_EQ(0, brpc::Socket::Create(options, &id));

Review Comment:
   `fds[1]` (write end) is not guarded; if an `ASSERT_*` fails before 
`Socket::Create` successfully transfers ownership, the FD can leak and impact 
the rest of the test process. Consider wrapping `fds[1]` in a `butil::fd_guard` 
and only releasing it after `Socket::Create` succeeds (mandatory for leak-proof 
tests).



##########
test/brpc_http_rpc_protocol_unittest.cpp:
##########
@@ -2285,6 +2287,161 @@ TEST_F(HttpTest, 
http2_deferred_response_notify_registration_failure_resets_stre
     EXPECT_EQ(0, bthread_id_error(dummy_id, 0));
 }
 
+TEST(Http2FlowControlTest, oversized_body_returns_connection_window) {
+    brpc::GlobalInitializeOrDie();
+    GFLAGS_NAMESPACE::FlagSaver flag_saver;
+    brpc::FLAGS_max_body_size = 4;
+
+    for (bool client_side : {false, true}) {
+        SCOPED_TRACE(client_side);
+        int fds[2];
+        ASSERT_EQ(0, pipe(fds));
+        butil::fd_guard read_fd(fds[0]);
+        brpc::SocketOptions options;
+        options.fd = fds[1];
+        if (client_side) {
+            options.user = brpc::get_client_side_messenger();
+        }
+        brpc::SocketId id;
+        ASSERT_EQ(0, brpc::Socket::Create(options, &id));
+        brpc::SocketUniquePtr socket;
+        ASSERT_EQ(0, brpc::Socket::Address(id, &socket));
+        ASSERT_EQ(0, socket->ReleaseAdditionalReference());
+
+        brpc::policy::H2Context ctx(socket.get(), nullptr);
+        ASSERT_EQ(0, ctx.Init());
+        ASSERT_EQ(client_side, ctx.is_client_side());
+        ctx._conn_state = brpc::policy::H2_CONNECTION_READY;
+        // Flush connection credit on each rejected body, while keeping the
+        // first three bytes of a split body below the stream-update threshold.
+        ctx._local_settings.stream_window_size = 8;
+        ctx._unack_local_settings.stream_window_size = 8;

Review Comment:
   The test directly mutates internal `H2Context` state (`_conn_state`, 
`_local_settings`, `_unack_local_settings`), which makes it tightly coupled to 
implementation details and more likely to break during refactors. Prefer 
driving the context to READY via existing public/semantic entry points, or 
adding a small test-only helper API to configure these fields in a supported 
way (recommended to reduce brittleness).



##########
test/brpc_http_rpc_protocol_unittest.cpp:
##########
@@ -2285,6 +2287,161 @@ TEST_F(HttpTest, 
http2_deferred_response_notify_registration_failure_resets_stre
     EXPECT_EQ(0, bthread_id_error(dummy_id, 0));
 }
 
+TEST(Http2FlowControlTest, oversized_body_returns_connection_window) {
+    brpc::GlobalInitializeOrDie();
+    GFLAGS_NAMESPACE::FlagSaver flag_saver;
+    brpc::FLAGS_max_body_size = 4;
+
+    for (bool client_side : {false, true}) {
+        SCOPED_TRACE(client_side);
+        int fds[2];
+        ASSERT_EQ(0, pipe(fds));
+        butil::fd_guard read_fd(fds[0]);
+        brpc::SocketOptions options;
+        options.fd = fds[1];
+        if (client_side) {
+            options.user = brpc::get_client_side_messenger();
+        }
+        brpc::SocketId id;
+        ASSERT_EQ(0, brpc::Socket::Create(options, &id));
+        brpc::SocketUniquePtr socket;
+        ASSERT_EQ(0, brpc::Socket::Address(id, &socket));
+        ASSERT_EQ(0, socket->ReleaseAdditionalReference());
+
+        brpc::policy::H2Context ctx(socket.get(), nullptr);
+        ASSERT_EQ(0, ctx.Init());
+        ASSERT_EQ(client_side, ctx.is_client_side());
+        ctx._conn_state = brpc::policy::H2_CONNECTION_READY;
+        // Flush connection credit on each rejected body, while keeping the
+        // first three bytes of a split body below the stream-update threshold.
+        ctx._local_settings.stream_window_size = 8;
+        ctx._unack_local_settings.stream_window_size = 8;
+
+        auto consume_frame = [&](brpc::policy::H2FrameType type, uint8_t flags,
+                                 int stream_id, const std::string& payload) {
+            char head[brpc::policy::FRAME_HEAD_SIZE];
+            brpc::policy::SerializeFrameHead(
+                head, payload.size(), type, flags, stream_id);
+            butil::IOBuf buf;
+            buf.append(head, sizeof(head));
+            buf.append(payload);
+            butil::IOBufBytesIterator it(buf);
+            brpc::ParseResult result = ctx.Consume(it, socket.get());
+            EXPECT_EQ(0u, it.bytes_left());
+            return result;
+        };
+
+        auto start_stream = [&](int stream_id) {
+            if (client_side) {
+                // A response arrives on a stream already opened by a request.
+                std::unique_ptr<brpc::policy::H2StreamContext> sctx(
+                    new brpc::policy::H2StreamContext(false));
+                sctx->Init(&ctx, stream_id);
+                ASSERT_EQ(0, ctx.TryToInsertStream(stream_id, sctx.get()));
+                sctx.release();
+            }
+            // HPACK static entries: :status 200, or :method POST, :scheme 
http,
+            // :path /. END_HEADERS leaves the body open.
+            std::string headers = client_side ? "\x88" : "\x83\x86\x84";
+            brpc::ParseResult result = consume_frame(
+                brpc::policy::H2_FRAME_HEADERS, 0x4, stream_id, headers);
+            ASSERT_TRUE(result.is_ok()) << result.error_str();
+            ASSERT_EQ(nullptr, result.message());
+        };
+
+        auto check_replies = [&](int stream_id, bool reset,
+                                 uint32_t expected_credit) {
+            int bytes = 0;
+            ASSERT_EQ(0, ioctl(read_fd, FIONREAD, &bytes));
+            ASSERT_GT(bytes, 0);
+            butil::IOPortal replies;
+            ASSERT_EQ(bytes,
+                      replies.append_from_file_descriptor(read_fd, bytes));

Review Comment:
   This relies on an implicit conversion from `butil::fd_guard` to an `int` FD 
(if available). Using an explicit accessor (e.g., `read_fd.fd()` / 
`read_fd.get()` depending on the API) makes the code clearer and avoids build 
breaks if the guard type changes (optional but recommended).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to