From ec5d977a5fba5b2ea95a8adabc1d78b468b8f2d8 Mon Sep 17 00:00:00 2001 From: tang yanzhao Date: Wed, 20 Oct 2021 16:55:57 +0800 Subject: [PATCH 1/2] copy codes --- include/dsn/cpp/rpc_holder.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/dsn/cpp/rpc_holder.h b/include/dsn/cpp/rpc_holder.h index 031745d333..52a5321ba0 100644 --- a/include/dsn/cpp/rpc_holder.h +++ b/include/dsn/cpp/rpc_holder.h @@ -114,6 +114,12 @@ class rpc_holder return _i->thrift_response; } + dsn::error_code &error() const + { + dassert(_i, "rpc_holder is uninitialized"); + return _i->rpc_error; + } + message_ex *dsn_request() const { dassert(_i, "rpc_holder is uninitialized"); @@ -291,7 +297,7 @@ class rpc_holder message_ex *dsn_response = dsn_request->create_response(); marshall(dsn_response, thrift_response); - dsn_rpc_reply(dsn_response); + dsn_rpc_reply(dsn_response, rpc_error); } ~internal() @@ -305,6 +311,7 @@ class rpc_holder message_ex *dsn_request; std::unique_ptr thrift_request; TResponse thrift_response; + dsn::error_code rpc_error = dsn::ERR_OK; bool auto_reply; }; From c3110edfce5655fefb44e86aad7f4a78966a3b45 Mon Sep 17 00:00:00 2001 From: tang yanzhao Date: Wed, 20 Oct 2021 17:20:30 +0800 Subject: [PATCH 2/2] format --- src/runtime/test/rpc_holder_test.cpp | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/runtime/test/rpc_holder_test.cpp b/src/runtime/test/rpc_holder_test.cpp index d35e28f052..1cb5eb0dbf 100644 --- a/src/runtime/test/rpc_holder_test.cpp +++ b/src/runtime/test/rpc_holder_test.cpp @@ -70,6 +70,19 @@ TEST(rpc_holder, construct) ASSERT_TRUE(rpc.is_initialized()); ASSERT_EQ(rpc.request().app_name, "test"); } + + { + auto request = make_unique(); + t_rpc rpc(std::move(request), RPC_CM_QUERY_PARTITION_CONFIG_BY_INDEX); + ASSERT_EQ(rpc.error(), ERR_OK); + ASSERT_TRUE(rpc.is_initialized()); + + rpc.error() = ERR_BUSY; + ASSERT_EQ(rpc.error(), ERR_BUSY); + + rpc.error() = ERR_ADDRESS_ALREADY_USED; + ASSERT_EQ(rpc.error(), ERR_ADDRESS_ALREADY_USED); + } } TEST(rpc_holder, mock_rpc_call) @@ -87,6 +100,25 @@ TEST(rpc_holder, mock_rpc_call) ASSERT_EQ(mail_box.size(), 10); } + // test in error cases + RPC_MOCKING(t_rpc) + { + auto &mail_box = t_rpc::mail_box(); + + for (int i = 0; i < 10; i++) { + auto request = make_unique(); + t_rpc rpc(std::move(request), RPC_CM_QUERY_PARTITION_CONFIG_BY_INDEX); + rpc.error() = ERR_BUSY; + rpc.call(rpc_address("127.0.0.1", 12321), nullptr, [](error_code) {}); + } + + ASSERT_EQ(mail_box.size(), 10); + + for (const auto &iter : mail_box) { + ASSERT_EQ(iter.error(), ERR_BUSY); + } + } + // instances of rpc mocking are independent RPC_MOCKING(t_rpc) {