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