chenBright commented on code in PR #3749:
URL: https://github.com/apache/brpc/pull/3749#discussion_r4584978563
##########
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:
Keeping the direct setup here. This test intentionally starts after the
handshake and uses a small window to isolate DATA flow-control accounting.
Existing protocol tests follow this pattern and are built with
`-fno-access-control`. Adding a test-only production API or exercising the
handshake would expand the scope without being necessary for this regression.
##########
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:
No change needed here. `butil::fd_guard` explicitly provides `operator
int()` and has no `get()` or `fd()` accessor. Passing it directly to file
descriptor APIs is its intended interface and follows existing repository usage.
--
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]