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   bool DoRpcSoakTest(int32_t soak_iterations, int32_t max_failures,
82                      int64_t max_acceptable_per_iteration_latency_ms);
83   bool DoLongLivedChannelTest(int32_t soak_iterations,
84                               int32_t iteration_interval);
85 
86   // Auth tests.
87   // username is a string containing the user email
88   bool DoJwtTokenCreds(const grpc::string& username);
89   bool DoComputeEngineCreds(const grpc::string& default_service_account,
90                             const grpc::string& oauth_scope);
91   // username the GCE default service account email
92   bool DoOauth2AuthToken(const grpc::string& username,
93                          const grpc::string& oauth_scope);
94   // username is a string containing the user email
95   bool DoPerRpcCreds(const grpc::string& json_key);
96   // default_service_account is the GCE default service account email
97   bool DoGoogleDefaultCredentials(const grpc::string& default_service_account);
98 
99  private:
100   class ServiceStub {
101    public:
102     // If new_stub_every_call = true, pointer to a new instance of
103     // TestServce::Stub is returned by Get() everytime it is called
104     ServiceStub(ChannelCreationFunc channel_creation_func,
105                 bool new_stub_every_call);
106 
107     TestService::Stub* Get();
108     UnimplementedService::Stub* GetUnimplementedServiceStub();
109 
110     // forces channel to be recreated.
111     void ResetChannel();
112 
113    private:
114     ChannelCreationFunc channel_creation_func_;
115     std::unique_ptr<TestService::Stub> stub_;
116     std::unique_ptr<UnimplementedService::Stub> unimplemented_service_stub_;
117     std::shared_ptr<Channel> channel_;
118     bool new_stub_every_call_;  // If true, a new stub is returned by every
119                                 // Get() call
120   };
121 
122   bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response);
123 
124   /// Run \a custom_check_fn as an additional check.
125   bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response,
126                          const CheckerFn& custom_checks_fn);
127   bool AssertStatusOk(const Status& s,
128                       const grpc::string& optional_debug_string);
129   bool AssertStatusCode(const Status& s, StatusCode expected_code,
130                         const grpc::string& optional_debug_string);
131   bool TransientFailureOrAbort();
132 
133   std::tuple<bool, int32_t, std::string> PerformOneSoakTestIteration(
134       const bool reset_channel,
135       const int32_t max_acceptable_per_iteration_latency_ms);
136 
137   void PerformSoakTest(const bool reset_channel_per_iteration,
138                        const int32_t soak_iterations,
139                        const int32_t max_failures,
140                        const int32_t max_acceptable_per_iteration_latency_ms);
141 
142   ServiceStub serviceStub_;
143   /// If true, abort() is not called for transient failures
144   bool do_not_abort_on_transient_failures_;
145 };
146 
147 }  // namespace testing
148 }  // namespace grpc
149 
150 #endif  // GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H
151