http2_client.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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 
20 
21 #include <thread>
22 
23 #include "absl/flags/flag.h"
24 
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpcpp/channel.h>
28 #include <grpcpp/client_context.h>
29 
32 #include "src/proto/grpc/testing/messages.pb.h"
33 #include "src/proto/grpc/testing/test.grpc.pb.h"
36 
37 namespace grpc {
38 namespace testing {
39 
40 namespace {
41 const int kLargeRequestSize = 271828;
42 const int kLargeResponseSize = 314159;
43 } // namespace
44 
45 Http2Client::ServiceStub::ServiceStub(const std::shared_ptr<Channel>& channel)
46  : channel_(channel) {
47  stub_ = TestService::NewStub(channel);
48 }
49 
50 TestService::Stub* Http2Client::ServiceStub::Get() { return stub_.get(); }
51 
52 Http2Client::Http2Client(const std::shared_ptr<Channel>& channel)
56 
57 bool Http2Client::AssertStatusCode(const Status& s, StatusCode expected_code) {
58  if (s.error_code() == expected_code) {
59  return true;
60  }
61 
62  gpr_log(GPR_ERROR, "Error status code: %d (expected: %d), message: %s",
63  s.error_code(), expected_code, s.error_message().c_str());
64  abort();
65 }
66 
69  return serviceStub_.Get()->UnaryCall(&context, defaultRequest_, response);
70 }
71 
74  request.set_response_size(kLargeResponseSize);
75  std::string payload(kLargeRequestSize, '\0');
76  request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
77  return request;
78 }
79 
81  gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream after header");
82 
85  GPR_ASSERT(!response.has_payload()); // no data should be received
86 
87  gpr_log(GPR_DEBUG, "Done testing reset stream after header");
88  return true;
89 }
90 
92  gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream after data");
93 
96  // There is no guarantee that data would be received.
97 
98  gpr_log(GPR_DEBUG, "Done testing reset stream after data");
99  return true;
100 }
101 
103  gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream during data");
104 
107  GPR_ASSERT(!response.has_payload()); // no data should be received
108 
109  gpr_log(GPR_DEBUG, "Done testing reset stream during data");
110  return true;
111 }
112 
114  gpr_log(GPR_DEBUG, "Sending two RPCs and expecting goaway");
117  GPR_ASSERT(response.payload().body() ==
118  std::string(kLargeResponseSize, '\0'));
119 
120  // Sleep for one second to give time for client to receive goaway frame.
121  gpr_timespec sleep_time = gpr_time_add(
123  gpr_sleep_until(sleep_time);
124 
125  response.Clear();
127  GPR_ASSERT(response.payload().body() ==
128  std::string(kLargeResponseSize, '\0'));
129  gpr_log(GPR_DEBUG, "Done testing goaway");
130  return true;
131 }
132 
134  gpr_log(GPR_DEBUG, "Sending RPC and expecting ping");
137  GPR_ASSERT(response.payload().body() ==
138  std::string(kLargeResponseSize, '\0'));
139  gpr_log(GPR_DEBUG, "Done testing ping");
140  return true;
141 }
142 
144  const std::shared_ptr<grpc::Channel>& /*channel*/) {
147  GPR_ASSERT(response.payload().body() ==
148  std::string(kLargeResponseSize, '\0'));
149 }
150 
152  gpr_log(GPR_DEBUG, "Testing max streams");
153 
154  // Make an initial call on the channel to ensure the server's max streams
155  // setting is received
158  GPR_ASSERT(response.payload().body() ==
159  std::string(kLargeResponseSize, '\0'));
160 
161  std::vector<std::thread> test_threads;
162  test_threads.reserve(10);
163  for (int i = 0; i < 10; i++) {
164  test_threads.emplace_back(
166  }
167 
168  for (auto it = test_threads.begin(); it != test_threads.end(); it++) {
169  it->join();
170  }
171 
172  gpr_log(GPR_DEBUG, "Done testing max streams");
173  return true;
174 }
175 
176 } // namespace testing
177 } // namespace grpc
178 
179 ABSL_FLAG(int32_t, server_port, 0, "Server port.");
180 ABSL_FLAG(std::string, server_host, "localhost", "Server host to connect to");
181 ABSL_FLAG(std::string, test_case, "rst_after_header",
182  "Configure different test cases. Valid options are:\n\n"
183  "goaway\n"
184  "max_streams\n"
185  "ping\n"
186  "rst_after_data\n"
187  "rst_after_header\n"
188  "rst_during_data\n");
189 
190 int main(int argc, char** argv) {
191  grpc::testing::InitTest(&argc, &argv, true);
192  GPR_ASSERT(absl::GetFlag(FLAGS_server_port));
193  const int host_port_buf_size = 1024;
194  char host_port[host_port_buf_size];
195  snprintf(host_port, host_port_buf_size, "%s:%d",
196  absl::GetFlag(FLAGS_server_host).c_str(),
197  absl::GetFlag(FLAGS_server_port));
198  std::shared_ptr<grpc::Channel> channel =
200  GPR_ASSERT(channel->WaitForConnected(gpr_time_add(
203  gpr_log(GPR_INFO, "Testing case: %s", absl::GetFlag(FLAGS_test_case).c_str());
204  int ret = 0;
205  if (absl::GetFlag(FLAGS_test_case) == "rst_after_header") {
206  client.DoRstAfterHeader();
207  } else if (absl::GetFlag(FLAGS_test_case) == "rst_after_data") {
208  client.DoRstAfterData();
209  } else if (absl::GetFlag(FLAGS_test_case) == "rst_during_data") {
210  client.DoRstDuringData();
211  } else if (absl::GetFlag(FLAGS_test_case) == "goaway") {
212  client.DoGoaway();
213  } else if (absl::GetFlag(FLAGS_test_case) == "ping") {
214  client.DoPing();
215  } else if (absl::GetFlag(FLAGS_test_case) == "max_streams") {
216  client.DoMaxStreams();
217  } else {
218  const char* testcases[] = {
219  "goaway", "max_streams", "ping",
220  "rst_after_data", "rst_after_header", "rst_during_data"};
221  char* joined_testcases =
222  gpr_strjoin_sep(testcases, GPR_ARRAY_SIZE(testcases), "\n", nullptr);
223 
224  gpr_log(GPR_ERROR, "Unsupported test case %s. Valid options are\n%s",
225  absl::GetFlag(FLAGS_test_case).c_str(), joined_testcases);
226  gpr_free(joined_testcases);
227  ret = 1;
228  }
229 
230  return ret;
231 }
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
grpc::testing::InitTest
void InitTest(int *argc, char ***argv, bool remove_flags)
Definition: test_config_cc.cc:28
messages_pb2.SimpleRequest
SimpleRequest
Definition: messages_pb2.py:597
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
regen-readme.it
it
Definition: regen-readme.py:15
log.h
stub_
std::unique_ptr< grpc::testing::EchoTestService::Stub > stub_
Definition: client_channel_stress_test.cc:331
grpc::testing::Http2Client::MaxStreamsWorker
void MaxStreamsWorker(const std::shared_ptr< grpc::Channel > &channel)
Definition: http2_client.cc:143
grpc
Definition: grpcpp/alarm.h:33
client
Definition: examples/python/async_streaming/client.py:1
grpc::testing::Http2Client::AssertStatusCode
bool AssertStatusCode(const Status &s, StatusCode expected_code)
Definition: http2_client.cc:57
string.h
benchmark.request
request
Definition: benchmark.py:77
server_port
static int server_port
Definition: bad_server_response_test.cc:86
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::testing::Http2Client::channel_
std::shared_ptr< Channel > channel_
Definition: http2_client.h:62
grpc::testing::Http2Client::DoGoaway
bool DoGoaway()
Definition: http2_client.cc:113
grpc::testing::Http2Client
Definition: http2_client.h:33
create_test_channel.h
grpc::testing::Http2Client::serviceStub_
ServiceStub serviceStub_
Definition: http2_client.h:61
client
static uv_tcp_t client
Definition: test-callback-stack.c:33
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
grpc::testing::Http2Client::defaultRequest_
SimpleRequest defaultRequest_
Definition: http2_client.h:63
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc.StatusCode.OK
tuple OK
Definition: src/python/grpcio/grpc/__init__.py:260
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
grpc.StatusCode
Definition: src/python/grpcio/grpc/__init__.py:232
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
grpc::testing::Http2Client::DoRstDuringData
bool DoRstDuringData()
Definition: http2_client.cc:102
gpr_strjoin_sep
char * gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep, size_t *final_length)
Definition: string.cc:239
channel_
RefCountedPtr< Channel > channel_
Definition: channel_connectivity.cc:209
channel.h
grpc::testing::Http2Client::DoPing
bool DoPing()
Definition: http2_client.cc:133
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
grpc::testing::Http2Client::DoRstAfterData
bool DoRstAfterData()
Definition: http2_client.cc:91
ABSL_FLAG
ABSL_FLAG(int32_t, server_port, 0, "Server port.")
grpc::CreateTestChannel
std::shared_ptr< Channel > CreateTestChannel(const std::string &server, const std::string &cred_type, const std::string &override_hostname, bool use_prod_roots, const std::shared_ptr< CallCredentials > &creds, const ChannelArguments &args)
Definition: create_test_channel.cc:88
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
grpc::testing::Http2Client::ServiceStub::ServiceStub
ServiceStub(const std::shared_ptr< Channel > &channel)
Definition: http2_client.cc:45
grpc::testing::Http2Client::DoMaxStreams
bool DoMaxStreams()
Definition: http2_client.cc:151
grpc::testing::Http2Client::BuildDefaultRequest
SimpleRequest BuildDefaultRequest()
Definition: http2_client.cc:72
grpc::testing::Http2Client::Http2Client
Http2Client(const std::shared_ptr< Channel > &channel)
Definition: http2_client.cc:52
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
client_context.h
http2_server_health_check.server_host
server_host
Definition: http2_server_health_check.py:27
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
grpc::testing::INSECURE
@ INSECURE
Definition: create_test_channel.h:34
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
grpc::testing::Http2Client::ServiceStub::Get
TestService::Stub * Get()
Definition: http2_client.cc:50
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
http2_client.h
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
test_config.h
grpc::testing::Http2Client::ServiceStub::stub_
std::unique_ptr< TestService::Stub > stub_
Definition: http2_client.h:53
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
main
int main(int argc, char **argv)
Definition: http2_client.cc:190
gpr_timespec
Definition: gpr_types.h:50
grpc.StatusCode.INTERNAL
tuple INTERNAL
Definition: src/python/grpcio/grpc/__init__.py:277
messages_pb2.SimpleResponse
SimpleResponse
Definition: messages_pb2.py:604
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
gen_server_registered_method_bad_client_test_body.payload
list payload
Definition: gen_server_registered_method_bad_client_test_body.py:40
grpc::testing::Http2Client::DoRstAfterHeader
bool DoRstAfterHeader()
Definition: http2_client.cc:80
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
grpc::testing::Http2Client::SendUnaryCall
Status SendUnaryCall(SimpleResponse *response)
Definition: http2_client.cc:67
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
gpr_time_from_seconds
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:123


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:02