• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
73   // The following interop test are not yet part of the interop spec, and are
74   // not implemented cross-language. They are considered experimental for now,
75   // but at some point in the future, might be codified and implemented in all
76   // languages
77   bool DoChannelSoakTest(int32_t soak_iterations);
78   bool DoRpcSoakTest(int32_t soak_iterations);
79   bool DoLongLivedChannelTest(int32_t soak_iterations,
80                               int32_t iteration_interval);
81 
82   // Auth tests.
83   // username is a string containing the user email
84   bool DoJwtTokenCreds(const grpc::string& username);
85   bool DoComputeEngineCreds(const grpc::string& default_service_account,
86                             const grpc::string& oauth_scope);
87   // username the GCE default service account email
88   bool DoOauth2AuthToken(const grpc::string& username,
89                          const grpc::string& oauth_scope);
90   // username is a string containing the user email
91   bool DoPerRpcCreds(const grpc::string& json_key);
92 
93  private:
94   class ServiceStub {
95    public:
96     // If new_stub_every_call = true, pointer to a new instance of
97     // TestServce::Stub is returned by Get() everytime it is called
98     ServiceStub(ChannelCreationFunc channel_creation_func,
99                 bool new_stub_every_call);
100 
101     TestService::Stub* Get();
102     UnimplementedService::Stub* GetUnimplementedServiceStub();
103 
104     // forces channel to be recreated.
105     void ResetChannel();
106 
107    private:
108     ChannelCreationFunc channel_creation_func_;
109     std::unique_ptr<TestService::Stub> stub_;
110     std::unique_ptr<UnimplementedService::Stub> unimplemented_service_stub_;
111     std::shared_ptr<Channel> channel_;
112     bool new_stub_every_call_;  // If true, a new stub is returned by every
113                                 // Get() call
114   };
115 
116   bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response);
117 
118   /// Run \a custom_check_fn as an additional check.
119   bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response,
120                          const CheckerFn& custom_checks_fn);
121   bool AssertStatusOk(const Status& s,
122                       const grpc::string& optional_debug_string);
123   bool AssertStatusCode(const Status& s, StatusCode expected_code,
124                         const grpc::string& optional_debug_string);
125   bool TransientFailureOrAbort();
126 
127   ServiceStub serviceStub_;
128   /// If true, abort() is not called for transient failures
129   bool do_not_abort_on_transient_failures_;
130 };
131 
132 }  // namespace testing
133 }  // namespace grpc
134 
135 #endif  // GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H
136