diff --git a/test/cpp/interop/client.cc b/test/cpp/interop/client.cc index 69d24299c6e..05bd90bcaba 100644 --- a/test/cpp/interop/client.cc +++ b/test/cpp/interop/client.cc @@ -111,8 +111,32 @@ grpc::string GetServiceAccountJsonKey() { return json_key; } -void DoEmpty(std::shared_ptr channel) { +std::shared_ptr CreateChannelForTestCase( + const grpc::string& test_case) { + GPR_ASSERT(FLAGS_server_port); + const int host_port_buf_size = 1024; + char host_port[host_port_buf_size]; + snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(), + FLAGS_server_port); + + if (test_case == "service_account_creds") { + std::unique_ptr creds; + GPR_ASSERT(FLAGS_enable_ssl); + grpc::string json_key = GetServiceAccountJsonKey(); + creds = CredentialsFactory::ServiceAccountCredentials( + json_key, FLAGS_oauth_scope, std::chrono::seconds(3600)); + return CreateTestChannel(host_port, FLAGS_server_host_override, + FLAGS_enable_ssl, FLAGS_use_prod_roots, creds); + } else { + return CreateTestChannel(host_port, FLAGS_server_host_override, + FLAGS_enable_ssl, FLAGS_use_prod_roots); + } +} + +void DoEmpty() { gpr_log(GPR_INFO, "Sending an empty rpc..."); + std::shared_ptr channel = + CreateChannelForTestCase("empty_unary"); std::unique_ptr stub(TestService::NewStub(channel)); grpc::testing::Empty request = grpc::testing::Empty::default_instance(); @@ -125,43 +149,58 @@ void DoEmpty(std::shared_ptr channel) { gpr_log(GPR_INFO, "Empty rpc done."); } -void DoLargeUnary(std::shared_ptr channel, bool auth) { - gpr_log(GPR_INFO, "Sending a large unary rpc..."); +void LargeUnaryShared(std::shared_ptr channel, + SimpleRequest* request, SimpleResponse* response) { std::unique_ptr stub(TestService::NewStub(channel)); - SimpleRequest request; - SimpleResponse response; ClientContext context; - request.set_response_type(grpc::testing::PayloadType::COMPRESSABLE); - request.set_response_size(kLargeResponseSize); - if (auth) { - request.set_fill_username(true); - request.set_fill_oauth_scope(true); - } + request->set_response_type(grpc::testing::PayloadType::COMPRESSABLE); + request->set_response_size(kLargeResponseSize); grpc::string payload(kLargeRequestSize, '\0'); - request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize); + request->mutable_payload()->set_body(payload.c_str(), kLargeRequestSize); - grpc::Status s = stub->UnaryCall(&context, request, &response); + grpc::Status s = stub->UnaryCall(&context, *request, response); GPR_ASSERT(s.IsOk()); - GPR_ASSERT(response.payload().type() == + GPR_ASSERT(response->payload().type() == grpc::testing::PayloadType::COMPRESSABLE); - GPR_ASSERT(response.payload().body() == + GPR_ASSERT(response->payload().body() == grpc::string(kLargeResponseSize, '\0')); - if (auth) { - GPR_ASSERT(!response.username().empty()); - GPR_ASSERT(!response.oauth_scope().empty()); - grpc::string json_key = GetServiceAccountJsonKey(); - GPR_ASSERT(json_key.find(response.username()) != grpc::string::npos); - GPR_ASSERT(FLAGS_oauth_scope.find(response.oauth_scope()) != - grpc::string::npos); - } +} + +void DoServiceAccountCreds() { + gpr_log(GPR_INFO, + "Sending a large unary rpc with service account credentials ..."); + std::shared_ptr channel = + CreateChannelForTestCase("service_account_creds"); + SimpleRequest request; + SimpleResponse response; + request.set_fill_username(true); + request.set_fill_oauth_scope(true); + LargeUnaryShared(channel, &request, &response); + GPR_ASSERT(!response.username().empty()); + GPR_ASSERT(!response.oauth_scope().empty()); + grpc::string json_key = GetServiceAccountJsonKey(); + GPR_ASSERT(json_key.find(response.username()) != grpc::string::npos); + GPR_ASSERT(FLAGS_oauth_scope.find(response.oauth_scope()) != + grpc::string::npos); + gpr_log(GPR_INFO, "Large unary with service account creds done."); +} +void DoLargeUnary() { + gpr_log(GPR_INFO, "Sending a large unary rpc..."); + std::shared_ptr channel = + CreateChannelForTestCase("large_unary"); + SimpleRequest request; + SimpleResponse response; + LargeUnaryShared(channel, &request, &response); gpr_log(GPR_INFO, "Large unary done."); } -void DoRequestStreaming(std::shared_ptr channel) { +void DoRequestStreaming() { gpr_log(GPR_INFO, "Sending request steaming rpc ..."); + std::shared_ptr channel = + CreateChannelForTestCase("client_streaming"); std::unique_ptr stub(TestService::NewStub(channel)); grpc::ClientContext context; @@ -186,8 +225,10 @@ void DoRequestStreaming(std::shared_ptr channel) { gpr_log(GPR_INFO, "Request streaming done."); } -void DoResponseStreaming(std::shared_ptr channel) { +void DoResponseStreaming() { gpr_log(GPR_INFO, "Receiving response steaming rpc ..."); + std::shared_ptr channel = + CreateChannelForTestCase("server_streaming"); std::unique_ptr stub(TestService::NewStub(channel)); grpc::ClientContext context; @@ -213,9 +254,10 @@ void DoResponseStreaming(std::shared_ptr channel) { gpr_log(GPR_INFO, "Response streaming done."); } -void DoResponseStreamingWithSlowConsumer( - std::shared_ptr channel) { +void DoResponseStreamingWithSlowConsumer() { gpr_log(GPR_INFO, "Receiving response steaming rpc with slow consumer ..."); + std::shared_ptr channel = + CreateChannelForTestCase("slow_consumer"); std::unique_ptr stub(TestService::NewStub(channel)); grpc::ClientContext context; @@ -245,8 +287,10 @@ void DoResponseStreamingWithSlowConsumer( gpr_log(GPR_INFO, "Response streaming done."); } -void DoHalfDuplex(std::shared_ptr channel) { +void DoHalfDuplex() { gpr_log(GPR_INFO, "Sending half-duplex streaming rpc ..."); + std::shared_ptr channel = + CreateChannelForTestCase("half_duplex"); std::unique_ptr stub(TestService::NewStub(channel)); grpc::ClientContext context; @@ -276,8 +320,10 @@ void DoHalfDuplex(std::shared_ptr channel) { gpr_log(GPR_INFO, "Half-duplex streaming rpc done."); } -void DoPingPong(std::shared_ptr channel) { +void DoPingPong() { gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ..."); + std::shared_ptr channel = + CreateChannelForTestCase("ping_pong"); std::unique_ptr stub(TestService::NewStub(channel)); grpc::ClientContext context; @@ -312,57 +358,41 @@ int main(int argc, char** argv) { google::ParseCommandLineFlags(&argc, &argv, true); - GPR_ASSERT(FLAGS_server_port); - const int host_port_buf_size = 1024; - char host_port[host_port_buf_size]; - snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(), - FLAGS_server_port); - - std::shared_ptr channel( - CreateTestChannel(host_port, FLAGS_server_host_override, FLAGS_enable_ssl, - FLAGS_use_prod_roots)); - if (FLAGS_test_case == "empty_unary") { - DoEmpty(channel); + DoEmpty(); } else if (FLAGS_test_case == "large_unary") { - DoLargeUnary(channel, false); + DoLargeUnary(); } else if (FLAGS_test_case == "client_streaming") { - DoRequestStreaming(channel); + DoRequestStreaming(); } else if (FLAGS_test_case == "server_streaming") { - DoResponseStreaming(channel); + DoResponseStreaming(); } else if (FLAGS_test_case == "slow_consumer") { - DoResponseStreamingWithSlowConsumer(channel); + DoResponseStreamingWithSlowConsumer(); } else if (FLAGS_test_case == "half_duplex") { - DoHalfDuplex(channel); + DoHalfDuplex(); } else if (FLAGS_test_case == "ping_pong") { - DoPingPong(channel); + DoPingPong(); } else if (FLAGS_test_case == "service_account_creds") { - std::unique_ptr creds; - GPR_ASSERT(FLAGS_enable_ssl); - grpc::string json_key = GetServiceAccountJsonKey(); - creds = CredentialsFactory::ServiceAccountCredentials( - json_key, FLAGS_oauth_scope, std::chrono::seconds(3600)); - std::shared_ptr auth_channel( - CreateTestChannel(host_port, FLAGS_server_host_override, - FLAGS_enable_ssl, FLAGS_use_prod_roots, creds)); - DoLargeUnary(auth_channel, true); + DoServiceAccountCreds(); } else if (FLAGS_test_case == "all") { - DoEmpty(channel); - DoLargeUnary(channel, false); - DoRequestStreaming(channel); - DoResponseStreaming(channel); - DoHalfDuplex(channel); - DoPingPong(channel); - // TODO(yangg) add service_account_creds? + DoEmpty(); + DoLargeUnary(); + DoRequestStreaming(); + DoResponseStreaming(); + DoHalfDuplex(); + DoPingPong(); + if (FLAGS_enable_ssl) { + DoServiceAccountCreds(); + } } else { gpr_log( GPR_ERROR, "Unsupported test case %s. Valid options are all|empty_unary|" - "large_unary|client_streaming|server_streaming|half_duplex|ping_pong", + "large_unary|client_streaming|server_streaming|half_duplex|ping_pong|" + "service_account_creds", FLAGS_test_case.c_str()); } - channel.reset(); grpc_shutdown(); return 0; }