From 8bab28db0818bcc3c91e89c7aeb82f54f6135827 Mon Sep 17 00:00:00 2001 From: Donna Dionne Date: Thu, 16 Apr 2020 12:20:57 -0700 Subject: [PATCH] Rearrange test_service_impl.h/cc to fix build failures. --- test/cpp/end2end/BUILD | 15 +---- test/cpp/end2end/test_service_impl.cc | 96 ++++++++++++++++++++++++--- test/cpp/end2end/test_service_impl.h | 66 ++---------------- 3 files changed, 96 insertions(+), 81 deletions(-) diff --git a/test/cpp/end2end/BUILD b/test/cpp/end2end/BUILD index bb6be0c4f02..a20128dcc2d 100644 --- a/test/cpp/end2end/BUILD +++ b/test/cpp/end2end/BUILD @@ -35,19 +35,6 @@ grpc_cc_library( ], ) -grpc_cc_library( - name = "test_multiple_service_impl", - testonly = True, - hdrs = ["test_service_impl.h"], - external_deps = [ - "gtest", - ], - deps = [ - "//src/proto/grpc/testing:echo_proto", - "//test/cpp/util:test_util", - ], -) - grpc_cc_library( name = "test_health_check_service_impl", testonly = True, @@ -525,7 +512,7 @@ grpc_cc_test( "no_windows", ], # TODO(jtattermusch): fix test on windows deps = [ - ":test_multiple_service_impl", + ":test_service_impl", "//:gpr", "//:grpc", "//:grpc++", diff --git a/test/cpp/end2end/test_service_impl.cc b/test/cpp/end2end/test_service_impl.cc index 98f3cf44bec..12928c70b48 100644 --- a/test/cpp/end2end/test_service_impl.cc +++ b/test/cpp/end2end/test_service_impl.cc @@ -34,7 +34,87 @@ using std::chrono::system_clock; namespace grpc { namespace testing { -namespace { +namespace internal { + +// When echo_deadline is requested, deadline seen in the ServerContext is set in +// the response in seconds. +void MaybeEchoDeadline(experimental::ServerContextBase* context, + const EchoRequest* request, EchoResponse* response) { + if (request->has_param() && request->param().echo_deadline()) { + gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME); + if (context->deadline() != system_clock::time_point::max()) { + Timepoint2Timespec(context->deadline(), &deadline); + } + response->mutable_param()->set_request_deadline(deadline.tv_sec); + } +} + +void CheckServerAuthContext( + const experimental::ServerContextBase* context, + const grpc::string& expected_transport_security_type, + const grpc::string& expected_client_identity) { + std::shared_ptr auth_ctx = context->auth_context(); + std::vector tst = + auth_ctx->FindPropertyValues("transport_security_type"); + EXPECT_EQ(1u, tst.size()); + EXPECT_EQ(expected_transport_security_type, ToString(tst[0])); + if (expected_client_identity.empty()) { + EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty()); + EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty()); + EXPECT_FALSE(auth_ctx->IsPeerAuthenticated()); + } else { + auto identity = auth_ctx->GetPeerIdentity(); + EXPECT_TRUE(auth_ctx->IsPeerAuthenticated()); + EXPECT_EQ(1u, identity.size()); + EXPECT_EQ(expected_client_identity, identity[0]); + } +} + +// Returns the number of pairs in metadata that exactly match the given +// key-value pair. Returns -1 if the pair wasn't found. +int MetadataMatchCount( + const std::multimap& metadata, + const grpc::string& key, const grpc::string& value) { + int count = 0; + for (const auto& metadatum : metadata) { + if (ToString(metadatum.first) == key && + ToString(metadatum.second) == value) { + count++; + } + } + return count; +} + +int GetIntValueFromMetadataHelper( + const char* key, + const std::multimap& metadata, + int default_value) { + if (metadata.find(key) != metadata.end()) { + std::istringstream iss(ToString(metadata.find(key)->second)); + iss >> default_value; + gpr_log(GPR_INFO, "%s : %d", key, default_value); + } + + return default_value; +} + +int GetIntValueFromMetadata( + const char* key, + const std::multimap& metadata, + int default_value) { + return GetIntValueFromMetadataHelper(key, metadata, default_value); +} + +void ServerTryCancel(ServerContext* context) { + EXPECT_FALSE(context->IsCancelled()); + context->TryCancel(); + gpr_log(GPR_INFO, "Server called TryCancel() to cancel the request"); + // Now wait until it's really canceled + while (!context->IsCancelled()) { + gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_micros(1000, GPR_TIMESPAN))); + } +} void ServerTryCancelNonblocking(experimental::CallbackServerContext* context) { EXPECT_FALSE(context->IsCancelled()); @@ -43,7 +123,7 @@ void ServerTryCancelNonblocking(experimental::CallbackServerContext* context) { "Server called TryCancelNonblocking() to cancel the request"); } -} // namespace +} // namespace internal experimental::ServerUnaryReactor* CallbackTestServiceImpl::Echo( experimental::CallbackServerContext* context, const EchoRequest* request, @@ -275,7 +355,7 @@ CallbackTestServiceImpl::RequestStream( int server_try_cancel = internal::GetIntValueFromMetadata( kServerTryCancelRequest, context->client_metadata(), DO_NOT_CANCEL); if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { - ServerTryCancelNonblocking(context); + internal::ServerTryCancelNonblocking(context); // Don't need to provide a reactor since the RPC is canceled return nullptr; } @@ -316,7 +396,7 @@ CallbackTestServiceImpl::RequestStream( return; } if (server_try_cancel_ == CANCEL_AFTER_PROCESSING) { - ServerTryCancelNonblocking(ctx_); + internal::ServerTryCancelNonblocking(ctx_); return; } FinishOnce(Status::OK); @@ -361,7 +441,7 @@ CallbackTestServiceImpl::ResponseStream( int server_try_cancel = internal::GetIntValueFromMetadata( kServerTryCancelRequest, context->client_metadata(), DO_NOT_CANCEL); if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { - ServerTryCancelNonblocking(context); + internal::ServerTryCancelNonblocking(context); } class Reactor @@ -399,7 +479,7 @@ CallbackTestServiceImpl::ResponseStream( } else if (server_try_cancel_ == CANCEL_DURING_PROCESSING) { // Let OnCancel recover this } else if (server_try_cancel_ == CANCEL_AFTER_PROCESSING) { - ServerTryCancelNonblocking(ctx_); + internal::ServerTryCancelNonblocking(ctx_); } else { FinishOnce(Status::OK); } @@ -462,7 +542,7 @@ CallbackTestServiceImpl::BidiStream( server_write_last_ = internal::GetIntValueFromMetadata( kServerFinishAfterNReads, ctx->client_metadata(), 0); if (server_try_cancel_ == CANCEL_BEFORE_PROCESSING) { - ServerTryCancelNonblocking(ctx); + internal::ServerTryCancelNonblocking(ctx); } else { if (server_try_cancel_ == CANCEL_DURING_PROCESSING) { ctx->TryCancel(); @@ -502,7 +582,7 @@ CallbackTestServiceImpl::BidiStream( if (server_try_cancel_ == CANCEL_DURING_PROCESSING) { // Let OnCancel handle this } else if (server_try_cancel_ == CANCEL_AFTER_PROCESSING) { - ServerTryCancelNonblocking(ctx_); + internal::ServerTryCancelNonblocking(ctx_); } else { FinishOnce(Status::OK); } diff --git a/test/cpp/end2end/test_service_impl.h b/test/cpp/end2end/test_service_impl.h index 3210d5ec25f..d890bdbaff0 100644 --- a/test/cpp/end2end/test_service_impl.h +++ b/test/cpp/end2end/test_service_impl.h @@ -61,82 +61,30 @@ namespace internal { // When echo_deadline is requested, deadline seen in the ServerContext is set in // the response in seconds. void MaybeEchoDeadline(experimental::ServerContextBase* context, - const EchoRequest* request, EchoResponse* response) { - if (request->has_param() && request->param().echo_deadline()) { - gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME); - if (context->deadline() != system_clock::time_point::max()) { - Timepoint2Timespec(context->deadline(), &deadline); - } - response->mutable_param()->set_request_deadline(deadline.tv_sec); - } -} + const EchoRequest* request, EchoResponse* response); void CheckServerAuthContext( const experimental::ServerContextBase* context, const grpc::string& expected_transport_security_type, - const grpc::string& expected_client_identity) { - std::shared_ptr auth_ctx = context->auth_context(); - std::vector tst = - auth_ctx->FindPropertyValues("transport_security_type"); - EXPECT_EQ(1u, tst.size()); - EXPECT_EQ(expected_transport_security_type, ToString(tst[0])); - if (expected_client_identity.empty()) { - EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty()); - EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty()); - EXPECT_FALSE(auth_ctx->IsPeerAuthenticated()); - } else { - auto identity = auth_ctx->GetPeerIdentity(); - EXPECT_TRUE(auth_ctx->IsPeerAuthenticated()); - EXPECT_EQ(1u, identity.size()); - EXPECT_EQ(expected_client_identity, identity[0]); - } -} + const grpc::string& expected_client_identity); // Returns the number of pairs in metadata that exactly match the given // key-value pair. Returns -1 if the pair wasn't found. int MetadataMatchCount( const std::multimap& metadata, - const grpc::string& key, const grpc::string& value) { - int count = 0; - for (const auto& metadatum : metadata) { - if (ToString(metadatum.first) == key && - ToString(metadatum.second) == value) { - count++; - } - } - return count; -} + const grpc::string& key, const grpc::string& value); int GetIntValueFromMetadataHelper( const char* key, const std::multimap& metadata, - int default_value) { - if (metadata.find(key) != metadata.end()) { - std::istringstream iss(ToString(metadata.find(key)->second)); - iss >> default_value; - gpr_log(GPR_INFO, "%s : %d", key, default_value); - } - - return default_value; -} + int default_value); int GetIntValueFromMetadata( const char* key, const std::multimap& metadata, - int default_value) { - return GetIntValueFromMetadataHelper(key, metadata, default_value); -} - -void ServerTryCancel(ServerContext* context) { - EXPECT_FALSE(context->IsCancelled()); - context->TryCancel(); - gpr_log(GPR_INFO, "Server called TryCancel() to cancel the request"); - // Now wait until it's really canceled - while (!context->IsCancelled()) { - gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_micros(1000, GPR_TIMESPAN))); - } -} + int default_value); + +void ServerTryCancel(ServerContext* context); } // namespace internal class TestServiceSignaller {