Copilot commented on code in PR #3500: URL: https://github.com/apache/brpc/pull/3500#discussion_r3880072300
########## test/brpc_thrift_protocol_unittest.cpp: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// brpc - A framework to host and access services throughout Baidu. + +#include <gtest/gtest.h> +#include <gflags/gflags.h> +#include <unistd.h> + +int main(int argc, char* argv[]) { + testing::InitGoogleTest(&argc, argv); + GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + return RUN_ALL_TESTS(); +} + +// The malformed T_EXCEPTION reply is decoded by the Thrift framed protocol, +// which is only compiled in when the build has THRIFT support enabled (the +// WITH_THRIFT build flag / brpc_with_thrift config, off by default). Keep this +// test a trivial pass on builds without Thrift so it links cleanly there too. +#ifdef ENABLE_THRIFT_FRAMED_PROTOCOL + +#include <arpa/inet.h> +#include "butil/macros.h" +#include "brpc/socket.h" +#include "brpc/controller.h" +#include "brpc/policy/most_common_message.h" +#include "brpc/policy/thrift_protocol.h" + +namespace { + +// A nominal thrift message-type value for a T_EXCEPTION reply. +static const uint32_t THRIFT_TYPE_EXCEPTION = 3; +static const uint32_t THRIFT_HEAD_VERSION_1 = 0x80010000; + +class ThriftProtocolTest : public ::testing::Test { +protected: + ThriftProtocolTest() { + _pipe_fds[0] = _pipe_fds[1] = -1; + EXPECT_EQ(0, pipe(_pipe_fds)); + brpc::SocketId id; + brpc::SocketOptions options; + options.fd = _pipe_fds[1]; + EXPECT_EQ(0, brpc::Socket::Create(options, &id)); + EXPECT_EQ(0, brpc::Socket::Address(id, &_socket)); + } + + virtual void SetUp() {} + virtual void TearDown() { + // Close the pipe fds and destroy the test socket so no OS resources + // are leaked across tests. + if (_pipe_fds[0] >= 0) { + close(_pipe_fds[0]); + _pipe_fds[0] = -1; + } + if (_pipe_fds[1] >= 0) { + close(_pipe_fds[1]); + _pipe_fds[1] = -1; + } + _socket.reset(); + } Review Comment: In TearDown(), `_pipe_fds[1]` is the write-end passed into `SocketOptions::fd`, and `SocketOptions` explicitly transfers ownership of that fd to `brpc::Socket` (which closes it on recycle/destruction). Closing `_pipe_fds[1]` here can double-close the fd (and even close an unrelated fd if the number gets reused), leading to flaky tests. -- 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]
