1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #ifndef GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H 20 #define GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H 21 22 #include <memory> 23 24 #include <grpc/grpc.h> 25 #include <grpcpp/channel.h> 26 #include "src/proto/grpc/testing/messages.pb.h" 27 #include "src/proto/grpc/testing/test.grpc.pb.h" 28 29 namespace grpc { 30 namespace testing { 31 32 // Function pointer for custom checks. 33 typedef std::function<void(const InteropClientContextInspector&, 34 const SimpleRequest*, const SimpleResponse*)> 35 CheckerFn; 36 37 typedef std::function<std::shared_ptr<Channel>(void)> ChannelCreationFunc; 38 39 class InteropClient { 40 public: 41 /// If new_stub_every_test_case is true, a new TestService::Stub object is 42 /// created for every test case 43 /// If do_not_abort_on_transient_failures is true, abort() is not called in 44 /// case of transient failures (like connection failures) 45 explicit InteropClient(ChannelCreationFunc channel_creation_func, 46 bool new_stub_every_test_case, 47 bool do_not_abort_on_transient_failures); ~InteropClient()48 ~InteropClient() {} 49 50 void Reset(const std::shared_ptr<Channel>& channel); 51 52 bool DoEmpty(); 53 bool DoLargeUnary(); 54 bool DoServerCompressedUnary(); 55 bool DoClientCompressedUnary(); 56 bool DoPingPong(); 57 bool DoHalfDuplex(); 58 bool DoRequestStreaming(); 59 bool DoResponseStreaming(); 60 bool DoServerCompressedStreaming(); 61 bool DoClientCompressedStreaming(); 62 bool DoResponseStreamingWithSlowConsumer(); 63 bool DoCancelAfterBegin(); 64 bool DoCancelAfterFirstResponse(); 65 bool DoTimeoutOnSleepingServer(); 66 bool DoEmptyStream(); 67 bool DoStatusWithMessage(); 68 bool DoCustomMetadata(); 69 bool DoUnimplementedMethod(); 70 bool DoUnimplementedService(); 71 bool DoCacheableUnary(); 72 // all requests are sent to one server despite multiple servers are resolved 73 bool DoPickFirstUnary(); 74 75 // The following interop test are not yet part of the interop spec, and are 76 // not implemented cross-language. They are considered experimental for now, 77 // but at some point in the future, might be codified and implemented in all 78 // languages 79 bool DoChannelSoakTest(int32_t soak_iterations, int32_t max_failures, 80 int64_t max_acceptable_per_iteration_latency_ms, 81 int32_t overall_timeout_seconds); 82 bool DoRpcSoakTest(int32_t soak_iterations, int32_t max_failures, 83 int64_t max_acceptable_per_iteration_latency_ms, 84 int32_t overall_timeout_seconds); 85 bool DoLongLivedChannelTest(int32_t soak_iterations, 86 int32_t iteration_interval); 87 88 // Auth tests. 89 // username is a string containing the user email 90 bool DoJwtTokenCreds(const std::string& username); 91 bool DoComputeEngineCreds(const std::string& default_service_account, 92 const std::string& oauth_scope); 93 // username the GCE default service account email 94 bool DoOauth2AuthToken(const std::string& username, 95 const std::string& oauth_scope); 96 // username is a string containing the user email 97 bool DoPerRpcCreds(const std::string& json_key); 98 // default_service_account is the GCE default service account email 99 bool DoGoogleDefaultCredentials(const std::string& default_service_account); 100 101 private: 102 class ServiceStub { 103 public: 104 // If new_stub_every_call = true, pointer to a new instance of 105 // TestServce::Stub is returned by Get() everytime it is called 106 ServiceStub(ChannelCreationFunc channel_creation_func, 107 bool new_stub_every_call); 108 109 TestService::Stub* Get(); 110 UnimplementedService::Stub* GetUnimplementedServiceStub(); 111 112 // forces channel to be recreated. 113 void ResetChannel(); 114 115 private: 116 ChannelCreationFunc channel_creation_func_; 117 std::unique_ptr<TestService::Stub> stub_; 118 std::unique_ptr<UnimplementedService::Stub> unimplemented_service_stub_; 119 std::shared_ptr<Channel> channel_; 120 bool new_stub_every_call_; // If true, a new stub is returned by every 121 // Get() call 122 }; 123 124 bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response); 125 126 /// Run \a custom_check_fn as an additional check. 127 bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response, 128 const CheckerFn& custom_checks_fn); 129 bool AssertStatusOk(const Status& s, 130 const std::string& optional_debug_string); 131 bool AssertStatusCode(const Status& s, StatusCode expected_code, 132 const std::string& optional_debug_string); 133 bool TransientFailureOrAbort(); 134 135 std::tuple<bool, int32_t, std::string> PerformOneSoakTestIteration( 136 const bool reset_channel, 137 const int32_t max_acceptable_per_iteration_latency_ms); 138 139 void PerformSoakTest(const bool reset_channel_per_iteration, 140 const int32_t soak_iterations, 141 const int32_t max_failures, 142 const int32_t max_acceptable_per_iteration_latency_ms, 143 const int32_t overall_timeout_seconds); 144 145 ServiceStub serviceStub_; 146 /// If true, abort() is not called for transient failures 147 bool do_not_abort_on_transient_failures_; 148 }; 149 150 } // namespace testing 151 } // namespace grpc 152 153 #endif // GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H 154