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