From 019b2069268375bf386333ad7de2251ebd2f538f Mon Sep 17 00:00:00 2001 From: Donna Dionne Date: Tue, 14 Apr 2020 00:04:31 -0700 Subject: [PATCH] Fixing CR comments and build errors. --- test/cpp/end2end/test_service_impl.h | 162 +++++++++++++-------------- test/cpp/end2end/xds_end2end_test.cc | 107 ++++++------------ 2 files changed, 114 insertions(+), 155 deletions(-) diff --git a/test/cpp/end2end/test_service_impl.h b/test/cpp/end2end/test_service_impl.h index 1fed3e55d61..4e12201bacd 100644 --- a/test/cpp/end2end/test_service_impl.h +++ b/test/cpp/end2end/test_service_impl.h @@ -57,6 +57,86 @@ typedef enum { CANCEL_AFTER_PROCESSING } ServerTryCancelRequestPhase; +// 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))); + } +} + class TestServiceSignaller { public: void ClientWaitUntilRpcStarted() { @@ -93,87 +173,6 @@ class TestMultipleServiceImpl : public RpcService { explicit TestMultipleServiceImpl(const grpc::string& host) : signal_client_(false), host_(new grpc::string(host)) {} - // When echo_deadline is requested, deadline seen in the ServerContext is set - // in the response in seconds. - void static 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 static 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 static 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 static 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 static GetIntValueFromMetadata( - const char* key, - const std::multimap& metadata, - int default_value) { - return GetIntValueFromMetadataHelper(key, metadata, default_value); - } - - void static 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))); - } - } - Status Echo(ServerContext* context, const EchoRequest* request, EchoResponse* response) { if (request->has_param() && @@ -306,7 +305,6 @@ class TestMultipleServiceImpl : public RpcService { } // Unimplemented is left unimplemented to test the returned error. - Status RequestStream(ServerContext* context, ServerReader* reader, EchoResponse* response) { diff --git a/test/cpp/end2end/xds_end2end_test.cc b/test/cpp/end2end/xds_end2end_test.cc index 0b1a474d265..204df6f8687 100644 --- a/test/cpp/end2end/xds_end2end_test.cc +++ b/test/cpp/end2end/xds_end2end_test.cc @@ -1363,7 +1363,8 @@ class XdsEnd2endTest : public ::testing::TestWithParam { return backend_ports; } - Status SendRpc(EchoResponse* response = nullptr, int timeout_ms = 1000, + Status SendRpc(const string& method_name = "Echo", + EchoResponse* response = nullptr, int timeout_ms = 1000, bool wait_for_ready = false, bool server_fail = false) { const bool local_response = (response == nullptr); if (local_response) response = new EchoResponse; @@ -1376,44 +1377,26 @@ class XdsEnd2endTest : public ::testing::TestWithParam { ClientContext context; context.set_deadline(grpc_timeout_milliseconds_to_deadline(timeout_ms)); if (wait_for_ready) context.set_wait_for_ready(true); - Status status = stub_->Echo(&context, request, response); - if (local_response) delete response; - return status; - } - - Status SendEcho1Rpc(EchoResponse* response = nullptr, int timeout_ms = 1000, - bool wait_for_ready = false) { - const bool local_response = (response == nullptr); - if (local_response) response = new EchoResponse; - EchoRequest request; - request.set_message(kRequestMessage_); - ClientContext context; - context.set_deadline(grpc_timeout_milliseconds_to_deadline(timeout_ms)); - if (wait_for_ready) context.set_wait_for_ready(true); - Status status = stub1_->Echo1(&context, request, response); - if (local_response) delete response; - return status; - } - - Status SendEcho2Rpc(EchoResponse* response = nullptr, int timeout_ms = 1000, - bool wait_for_ready = false) { - const bool local_response = (response == nullptr); - if (local_response) response = new EchoResponse; - EchoRequest request; - request.set_message(kRequestMessage_); - ClientContext context; - context.set_deadline(grpc_timeout_milliseconds_to_deadline(timeout_ms)); - if (wait_for_ready) context.set_wait_for_ready(true); - Status status = stub2_->Echo2(&context, request, response); + Status status; + if (method_name == "Echo") { + status = stub_->Echo(&context, request, response); + } else if (method_name == "Echo1") { + status = stub1_->Echo1(&context, request, response); + } else if (method_name == "Echo2") { + status = stub2_->Echo2(&context, request, response); + } if (local_response) delete response; return status; } - void CheckRpcSendOk(const size_t times = 1, const int timeout_ms = 1000, + void CheckRpcSendOk(const size_t times = 1, + const string& method_name = "Echo", + const int timeout_ms = 1000, bool wait_for_ready = false) { for (size_t i = 0; i < times; ++i) { EchoResponse response; - const Status status = SendRpc(&response, timeout_ms, wait_for_ready); + const Status status = + SendRpc(method_name, &response, timeout_ms, wait_for_ready); EXPECT_TRUE(status.ok()) << "code=" << status.error_code() << " message=" << status.error_message(); EXPECT_EQ(response.message(), kRequestMessage_); @@ -1422,33 +1405,11 @@ class XdsEnd2endTest : public ::testing::TestWithParam { void CheckRpcSendFailure(const size_t times = 1, bool server_fail = false) { for (size_t i = 0; i < times; ++i) { - const Status status = SendRpc(nullptr, 1000, false, server_fail); + const Status status = SendRpc("Echo", nullptr, 1000, false, server_fail); EXPECT_FALSE(status.ok()); } } - void CheckEcho1RpcSendOk(const size_t times = 1, const int timeout_ms = 1000, - bool wait_for_ready = false) { - for (size_t i = 0; i < times; ++i) { - EchoResponse response; - const Status status = SendEcho1Rpc(&response, timeout_ms, wait_for_ready); - EXPECT_TRUE(status.ok()) << "code=" << status.error_code() - << " message=" << status.error_message(); - EXPECT_EQ(response.message(), kRequestMessage_); - } - } - - void CheckEcho2RpcSendOk(const size_t times = 1, const int timeout_ms = 1000, - bool wait_for_ready = false) { - for (size_t i = 0; i < times; ++i) { - EchoResponse response; - const Status status = SendEcho2Rpc(&response, timeout_ms, wait_for_ready); - EXPECT_TRUE(status.ok()) << "code=" << status.error_code() - << " message=" << status.error_message(); - EXPECT_EQ(response.message(), kRequestMessage_); - } - } - public: // This method could benefit test subclasses; to make it accessible // via bind with a qualified name, it needs to be public. @@ -1738,7 +1699,7 @@ TEST_P(BasicTest, InitiallyEmptyServerlist) { kDefaultResourceName)); const auto t0 = system_clock::now(); // Client will block: LB will initially send empty serverlist. - CheckRpcSendOk(1, kCallDeadlineMs, true /* wait_for_ready */); + CheckRpcSendOk(1, "Echo", kCallDeadlineMs, true /* wait_for_ready */); const auto ellapsed_ms = std::chrono::duration_cast( system_clock::now() - t0); @@ -1786,7 +1747,7 @@ TEST_P(BasicTest, BackendsRestart) { CheckRpcSendFailure(); // Restart all backends. RPCs should start succeeding again. StartAllBackends(); - CheckRpcSendOk(1 /* times */, 2000 /* timeout_ms */, + CheckRpcSendOk(1 /* times */, "Echo", 2000 /* timeout_ms */, true /* wait_for_ready */); } @@ -2307,9 +2268,9 @@ TEST_P(LdsTest, XdsRoutingPathMatching) { balancers_[0]->ads_service()->BuildListener(new_route_config); balancers_[0]->ads_service()->SetLdsResource(listener, kDefaultResourceName); WaitForAllBackends(0, 2); - CheckRpcSendOk(kNumEchoRpcs, 1000, true); - CheckEcho1RpcSendOk(kNumEcho1Rpcs, 1000, true); - CheckEcho2RpcSendOk(kNumEcho2Rpcs, 1000, true); + CheckRpcSendOk(kNumEchoRpcs, "Echo", 1000, true); + CheckRpcSendOk(kNumEcho1Rpcs, "Echo1", 1000, true); + CheckRpcSendOk(kNumEcho2Rpcs, "Echo2", 1000, true); // Make sure RPCs all go to the correct backend. for (size_t i = 0; i < 2; ++i) { EXPECT_EQ(kNumEchoRpcs / 2, @@ -2378,9 +2339,9 @@ TEST_P(LdsTest, XdsRoutingPrefixMatching) { balancers_[0]->ads_service()->BuildListener(new_route_config); balancers_[0]->ads_service()->SetLdsResource(listener, kDefaultResourceName); WaitForAllBackends(0, 2); - CheckRpcSendOk(kNumEchoRpcs, 1000, true); - CheckEcho1RpcSendOk(kNumEcho1Rpcs, 1000, true); - CheckEcho2RpcSendOk(kNumEcho2Rpcs, 1000, true); + CheckRpcSendOk(kNumEchoRpcs, "Echo", 1000, true); + CheckRpcSendOk(kNumEcho1Rpcs, "Echo1", 1000, true); + CheckRpcSendOk(kNumEcho2Rpcs, "Echo2", 1000, true); // Make sure RPCs all go to the correct backend. for (size_t i = 0; i < 2; ++i) { EXPECT_EQ(kNumEchoRpcs / 2, @@ -3090,7 +3051,7 @@ TEST_P(DropTest, Vanilla) { size_t num_drops = 0; for (size_t i = 0; i < kNumRpcs; ++i) { EchoResponse response; - const Status status = SendRpc(&response); + const Status status = SendRpc("Echo", &response); if (!status.ok() && status.error_message() == "Call dropped by load balancing policy") { ++num_drops; @@ -3130,7 +3091,7 @@ TEST_P(DropTest, DropPerHundred) { size_t num_drops = 0; for (size_t i = 0; i < kNumRpcs; ++i) { EchoResponse response; - const Status status = SendRpc(&response); + const Status status = SendRpc("Echo", &response); if (!status.ok() && status.error_message() == "Call dropped by load balancing policy") { ++num_drops; @@ -3169,7 +3130,7 @@ TEST_P(DropTest, DropPerTenThousand) { size_t num_drops = 0; for (size_t i = 0; i < kNumRpcs; ++i) { EchoResponse response; - const Status status = SendRpc(&response); + const Status status = SendRpc("Echo", &response); if (!status.ok() && status.error_message() == "Call dropped by load balancing policy") { ++num_drops; @@ -3212,7 +3173,7 @@ TEST_P(DropTest, Update) { gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); for (size_t i = 0; i < kNumRpcs; ++i) { EchoResponse response; - const Status status = SendRpc(&response); + const Status status = SendRpc("Echo", &response); if (!status.ok() && status.error_message() == "Call dropped by load balancing policy") { ++num_drops; @@ -3244,7 +3205,7 @@ TEST_P(DropTest, Update) { size_t num_rpcs = kNumRpcs; while (seen_drop_rate < kDropRateThreshold) { EchoResponse response; - const Status status = SendRpc(&response); + const Status status = SendRpc("Echo", &response); ++num_rpcs; if (!status.ok() && status.error_message() == "Call dropped by load balancing policy") { @@ -3261,7 +3222,7 @@ TEST_P(DropTest, Update) { gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH =========="); for (size_t i = 0; i < kNumRpcs; ++i) { EchoResponse response; - const Status status = SendRpc(&response); + const Status status = SendRpc("Echo", &response); if (!status.ok() && status.error_message() == "Call dropped by load balancing policy") { ++num_drops; @@ -3298,7 +3259,7 @@ TEST_P(DropTest, DropAll) { // Send kNumRpcs RPCs and all of them are dropped. for (size_t i = 0; i < kNumRpcs; ++i) { EchoResponse response; - const Status status = SendRpc(&response); + const Status status = SendRpc("Echo", &response); EXPECT_EQ(status.error_code(), StatusCode::UNAVAILABLE); EXPECT_EQ(status.error_message(), "Call dropped by load balancing policy"); } @@ -3441,7 +3402,7 @@ TEST_P(FallbackTest, FallbackEarlyWhenBalancerChannelFails) { SetNextResolutionForLbChannel({g_port_saver->GetPort()}); // Send RPC with deadline less than the fallback timeout and make sure it // succeeds. - CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000, + CheckRpcSendOk(/* times */ 1, "Echo", /* timeout_ms */ 1000, /* wait_for_ready */ false); } @@ -3456,7 +3417,7 @@ TEST_P(FallbackTest, FallbackEarlyWhenBalancerCallFails) { balancers_[0]->ads_service()->NotifyDoneWithAdsCall(); // Send RPC with deadline less than the fallback timeout and make sure it // succeeds. - CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000, + CheckRpcSendOk(/* times */ 1, "Echo", /* timeout_ms */ 1000, /* wait_for_ready */ false); } @@ -3928,7 +3889,7 @@ TEST_P(ClientLoadReportingWithDropTest, Vanilla) { // Send kNumRpcs RPCs and count the drops. for (size_t i = 0; i < kNumRpcs; ++i) { EchoResponse response; - const Status status = SendRpc(&response); + const Status status = SendRpc("Echo", &response); if (!status.ok() && status.error_message() == "Call dropped by load balancing policy") { ++num_drops;