xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/interop_client.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <cstdint>
23 #include <memory>
24 
25 #include <grpc/grpc.h>
26 #include <grpcpp/channel.h>
27 
28 #include "src/proto/grpc/testing/messages.pb.h"
29 #include "src/proto/grpc/testing/test.grpc.pb.h"
30 #include "test/cpp/interop/backend_metrics_lb_policy.h"
31 
32 namespace grpc {
33 namespace testing {
34 
35 // Function pointer for custom checks.
36 typedef std::function<void(const InteropClientContextInspector&,
37                            const SimpleRequest*, const SimpleResponse*)>
38     CheckerFn;
39 
40 typedef std::function<std::shared_ptr<Channel>(ChannelArguments)>
41     ChannelCreationFunc;
42 
43 class InteropClient {
44  public:
45   /// If new_stub_every_test_case is true, a new TestService::Stub object is
46   /// created for every test case
47   /// If do_not_abort_on_transient_failures is true, abort() is not called
48   /// in case of transient failures (like connection failures)
49   explicit InteropClient(ChannelCreationFunc channel_creation_func,
50                          bool new_stub_every_test_case,
51                          bool do_not_abort_on_transient_failures);
~InteropClient()52   ~InteropClient() {}
53 
54   void Reset(const std::shared_ptr<Channel>& channel);
55 
56   bool DoEmpty();
57   bool DoLargeUnary();
58   bool DoServerCompressedUnary();
59   bool DoClientCompressedUnary();
60   bool DoPingPong();
61   bool DoHalfDuplex();
62   bool DoRequestStreaming();
63   bool DoResponseStreaming();
64   bool DoServerCompressedStreaming();
65   bool DoClientCompressedStreaming();
66   bool DoResponseStreamingWithSlowConsumer();
67   bool DoCancelAfterBegin();
68   bool DoCancelAfterFirstResponse();
69   bool DoTimeoutOnSleepingServer();
70   bool DoEmptyStream();
71   bool DoStatusWithMessage();
72   // Verifies Unicode and Whitespace is correctly processed in status message.
73   bool DoSpecialStatusMessage();
74   bool DoCustomMetadata();
75   bool DoUnimplementedMethod();
76   bool DoUnimplementedService();
77   // all requests are sent to one server despite multiple servers are resolved
78   bool DoPickFirstUnary();
79   bool DoOrcaPerRpc();
80   bool DoOrcaOob();
81 
82   // The following interop test are not yet part of the interop spec, and are
83   // not implemented cross-language. They are considered experimental for now,
84   // but at some point in the future, might be codified and implemented in all
85   // languages
86   bool DoChannelSoakTest(const std::string& server_uri, int32_t soak_iterations,
87                          int32_t max_failures,
88                          int64_t max_acceptable_per_iteration_latency_ms,
89                          int32_t soak_min_time_ms_between_rpcs,
90                          int32_t overall_timeout_seconds, int32_t request_size,
91                          int32_t response_size);
92   bool DoRpcSoakTest(const std::string& server_uri, int32_t soak_iterations,
93                      int32_t max_failures,
94                      int64_t max_acceptable_per_iteration_latency_ms,
95                      int32_t soak_min_time_ms_between_rpcs,
96                      int32_t overall_timeout_seconds, int32_t request_size,
97                      int32_t response_size);
98   bool DoLongLivedChannelTest(int32_t soak_iterations,
99                               int32_t iteration_interval);
100 
101   // Auth tests.
102   // username is a string containing the user email
103   bool DoJwtTokenCreds(const std::string& username);
104   bool DoComputeEngineCreds(const std::string& default_service_account,
105                             const std::string& oauth_scope);
106   // username the GCE default service account email
107   bool DoOauth2AuthToken(const std::string& username,
108                          const std::string& oauth_scope);
109   // username is a string containing the user email
110   bool DoPerRpcCreds(const std::string& json_key);
111   // default_service_account is the GCE default service account email
112   bool DoGoogleDefaultCredentials(const std::string& default_service_account);
113 
114  private:
115   class ServiceStub {
116    public:
117     typedef std::function<std::shared_ptr<Channel>()> ChannelCreationFunc;
118     // If new_stub_every_call = true, pointer to a new instance of
119     // TestServce::Stub is returned by Get() everytime it is called
120     ServiceStub(ChannelCreationFunc channel_creation_func,
121                 bool new_stub_every_call);
122 
123     TestService::Stub* Get();
124     UnimplementedService::Stub* GetUnimplementedServiceStub();
125 
126     // forces channel to be recreated.
127     void ResetChannel();
128 
129    private:
130     ChannelCreationFunc channel_creation_func_;
131     std::unique_ptr<TestService::Stub> stub_;
132     std::unique_ptr<UnimplementedService::Stub> unimplemented_service_stub_;
133     std::shared_ptr<Channel> channel_;
134     bool new_stub_every_call_;  // If true, a new stub is returned by every
135                                 // Get() call
136   };
137 
138   bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response);
139 
140   /// Run \a custom_check_fn as an additional check.
141   bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response,
142                          const CheckerFn& custom_checks_fn);
143   bool AssertStatusOk(const Status& s,
144                       const std::string& optional_debug_string);
145   bool AssertStatusCode(const Status& s, StatusCode expected_code,
146                         const std::string& optional_debug_string);
147   bool TransientFailureOrAbort();
148 
149   std::tuple<bool, int32_t, std::string, std::string>
150   PerformOneSoakTestIteration(
151       const bool reset_channel,
152       const int32_t max_acceptable_per_iteration_latency_ms,
153       const int32_t request_size, const int32_t response_size);
154 
155   void PerformSoakTest(const std::string& server_uri,
156                        const bool reset_channel_per_iteration,
157                        const int32_t soak_iterations,
158                        const int32_t max_failures,
159                        const int32_t max_acceptable_per_iteration_latency_ms,
160                        const int32_t min_time_ms_between_rpcs,
161                        const int32_t overall_timeout_seconds,
162                        const int32_t request_size, const int32_t response_size);
163 
164   ServiceStub serviceStub_;
165   /// If true, abort() is not called for transient failures
166   bool do_not_abort_on_transient_failures_;
167   // Load Orca metrics captured by the custom LB policy.
168   LoadReportTracker load_report_tracker_;
169 };
170 
171 }  // namespace testing
172 }  // namespace grpc
173 
174 #endif  // GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H
175