wwbmmm commented on code in PR #3574:
URL: https://github.com/apache/brpc/pull/3574#discussion_r4151753954


##########
test/brpc_nshead_mcpack_protocol_unittest.cpp:
##########
@@ -0,0 +1,190 @@
+// 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.
+
+// Unit tests of the nshead_mcpack protocol, focused on the client-side
+// response handler: `ProcessNsheadMcpackResponse' must always release the
+// bthread_id lock (via `accessor.OnResponse') on every path, otherwise the
+// RPC caller hangs on Join() forever and even the timeout mechanism cannot
+// rescue it.
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <gtest/gtest.h>
+#include <gflags/gflags.h>
+#include <google/protobuf/descriptor.h>
+#include "butil/time.h"
+#include "butil/macros.h"
+#include "bthread/id.h"
+#include "brpc/socket.h"
+#include "brpc/acceptor.h"
+#include "brpc/server.h"
+#include "brpc/nshead.h"
+#include "brpc/controller.h"
+#include "brpc/policy/nshead_mcpack_protocol.h"
+#include "brpc/policy/nshead_protocol.h"
+#include "brpc/policy/most_common_message.h"
+#include "mcpack2pb/mcpack2pb.h"
+#include "echo.pb.h"
+
+namespace {
+
+static const std::string EXP_RESPONSE = "world";
+
+// Simulates the mcpack parser facing a garbage body: drains the input and
+// returns 0 so that `parse_from_iobuf' reports failure.
+static size_t FailingParse(google::protobuf::Message*,
+                           google::protobuf::io::ZeroCopyInputStream* input) {
+    const void* data;
+    int size;
+    while (input->Next(&data, &size)) {}
+    return 0;
+}
+
+// Simulates a successful mcpack parsing which fills the response.
+static size_t SuccessfulParse(google::protobuf::Message* msg,
+                              google::protobuf::io::ZeroCopyInputStream* 
input) {
+    const void* data;
+    int size;
+    size_t total = 0;
+    while (input->Next(&data, &size)) {
+        total += size;
+    }
+    static_cast<test::EchoRequest*>(msg)->set_message(EXP_RESPONSE);
+    return total;
+}
+
+static void RegisterMcpackHandlers() {
+    static bool registered = []() {
+        mcpack2pb::MessageHandler failing_handler = {
+            FailingParse, nullptr, nullptr, nullptr};
+        mcpack2pb::register_message_handler_or_die(
+            test::EchoResponse::descriptor()->full_name(), failing_handler);
+        mcpack2pb::MessageHandler working_handler = {
+            SuccessfulParse, nullptr, nullptr, nullptr};
+        mcpack2pb::register_message_handler_or_die(
+            test::EchoRequest::descriptor()->full_name(), working_handler);
+        return true;
+    }();
+    (void)registered;
+}
+
+class NsheadMcpackTest : public ::testing::Test {
+protected:
+    NsheadMcpackTest() {
+        RegisterMcpackHandlers();
+        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 ~NsheadMcpackTest() {};

Review Comment:
   [replied by brpc-oncall robot] Good catch — fixed in the follow-up commit: 
the fixture destructor now closes `_pipe_fds[0]`. The write end stays owned by 
`_socket` and is released automatically when the fixture is destroyed, so only 
the unused read end needed an explicit close.



-- 
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