• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "test/cpp/interop/http2_client.h"
20 
21 #include <grpc/support/alloc.h>
22 #include <grpcpp/channel.h>
23 #include <grpcpp/client_context.h>
24 
25 #include <thread>
26 
27 #include "absl/flags/flag.h"
28 #include "absl/log/check.h"
29 #include "absl/log/log.h"
30 #include "absl/strings/str_format.h"
31 #include "src/core/util/crash.h"
32 #include "src/core/util/string.h"
33 #include "src/core/util/useful.h"
34 #include "src/proto/grpc/testing/messages.pb.h"
35 #include "src/proto/grpc/testing/test.grpc.pb.h"
36 #include "test/cpp/util/create_test_channel.h"
37 #include "test/cpp/util/test_config.h"
38 
39 namespace grpc {
40 namespace testing {
41 
42 namespace {
43 const int kLargeRequestSize = 271828;
44 const int kLargeResponseSize = 314159;
45 }  // namespace
46 
ServiceStub(const std::shared_ptr<Channel> & channel)47 Http2Client::ServiceStub::ServiceStub(const std::shared_ptr<Channel>& channel)
48     : channel_(channel) {
49   stub_ = TestService::NewStub(channel);
50 }
51 
Get()52 TestService::Stub* Http2Client::ServiceStub::Get() { return stub_.get(); }
53 
Http2Client(const std::shared_ptr<Channel> & channel)54 Http2Client::Http2Client(const std::shared_ptr<Channel>& channel)
55     : serviceStub_(channel),
56       channel_(channel),
57       defaultRequest_(BuildDefaultRequest()) {}
58 
AssertStatusCode(const Status & s,StatusCode expected_code)59 bool Http2Client::AssertStatusCode(const Status& s, StatusCode expected_code) {
60   if (s.error_code() == expected_code) {
61     return true;
62   }
63 
64   grpc_core::Crash(absl::StrFormat(
65       "Error status code: %d (expected: %d), message: %s", s.error_code(),
66       expected_code, s.error_message().c_str()));
67 }
68 
SendUnaryCall(SimpleResponse * response)69 Status Http2Client::SendUnaryCall(SimpleResponse* response) {
70   ClientContext context;
71   return serviceStub_.Get()->UnaryCall(&context, defaultRequest_, response);
72 }
73 
BuildDefaultRequest()74 SimpleRequest Http2Client::BuildDefaultRequest() {
75   SimpleRequest request;
76   request.set_response_size(kLargeResponseSize);
77   std::string payload(kLargeRequestSize, '\0');
78   request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
79   return request;
80 }
81 
DoRstAfterHeader()82 bool Http2Client::DoRstAfterHeader() {
83   VLOG(2) << "Sending RPC and expecting reset stream after header";
84 
85   SimpleResponse response;
86   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::INTERNAL);
87   CHECK(!response.has_payload());  // no data should be received
88 
89   VLOG(2) << "Done testing reset stream after header";
90   return true;
91 }
92 
DoRstAfterData()93 bool Http2Client::DoRstAfterData() {
94   VLOG(2) << "Sending RPC and expecting reset stream after data";
95 
96   SimpleResponse response;
97   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::INTERNAL);
98   // There is no guarantee that data would be received.
99 
100   VLOG(2) << "Done testing reset stream after data";
101   return true;
102 }
103 
DoRstDuringData()104 bool Http2Client::DoRstDuringData() {
105   VLOG(2) << "Sending RPC and expecting reset stream during data";
106 
107   SimpleResponse response;
108   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::INTERNAL);
109   CHECK(!response.has_payload());  // no data should be received
110 
111   VLOG(2) << "Done testing reset stream during data";
112   return true;
113 }
114 
DoGoaway()115 bool Http2Client::DoGoaway() {
116   VLOG(2) << "Sending two RPCs and expecting goaway";
117   SimpleResponse response;
118   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
119   CHECK(response.payload().body() == std::string(kLargeResponseSize, '\0'));
120 
121   // Sleep for one second to give time for client to receive goaway frame.
122   gpr_timespec sleep_time = gpr_time_add(
123       gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(1, GPR_TIMESPAN));
124   gpr_sleep_until(sleep_time);
125 
126   response.Clear();
127   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
128   CHECK(response.payload().body() == std::string(kLargeResponseSize, '\0'));
129   VLOG(2) << "Done testing goaway";
130   return true;
131 }
132 
DoPing()133 bool Http2Client::DoPing() {
134   VLOG(2) << "Sending RPC and expecting ping";
135   SimpleResponse response;
136   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
137   CHECK(response.payload().body() == std::string(kLargeResponseSize, '\0'));
138   VLOG(2) << "Done testing ping";
139   return true;
140 }
141 
MaxStreamsWorker(const std::shared_ptr<grpc::Channel> &)142 void Http2Client::MaxStreamsWorker(
143     const std::shared_ptr<grpc::Channel>& /*channel*/) {
144   SimpleResponse response;
145   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
146   CHECK(response.payload().body() == std::string(kLargeResponseSize, '\0'));
147 }
148 
DoMaxStreams()149 bool Http2Client::DoMaxStreams() {
150   VLOG(2) << "Testing max streams";
151 
152   // Make an initial call on the channel to ensure the server's max streams
153   // setting is received
154   SimpleResponse response;
155   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
156   CHECK(response.payload().body() == std::string(kLargeResponseSize, '\0'));
157 
158   std::vector<std::thread> test_threads;
159   test_threads.reserve(10);
160   for (int i = 0; i < 10; i++) {
161     test_threads.emplace_back(
162         std::thread(&Http2Client::MaxStreamsWorker, this, channel_));
163   }
164 
165   for (auto it = test_threads.begin(); it != test_threads.end(); it++) {
166     it->join();
167   }
168 
169   VLOG(2) << "Done testing max streams";
170   return true;
171 }
172 
173 }  // namespace testing
174 }  // namespace grpc
175 
176 ABSL_FLAG(int32_t, server_port, 0, "Server port.");
177 ABSL_FLAG(std::string, server_host, "localhost", "Server host to connect to");
178 ABSL_FLAG(std::string, test_case, "rst_after_header",
179           "Configure different test cases. Valid options are:\n\n"
180           "goaway\n"
181           "max_streams\n"
182           "ping\n"
183           "rst_after_data\n"
184           "rst_after_header\n"
185           "rst_during_data\n");
186 
main(int argc,char ** argv)187 int main(int argc, char** argv) {
188   grpc::testing::InitTest(&argc, &argv, true);
189   CHECK(absl::GetFlag(FLAGS_server_port));
190   const int host_port_buf_size = 1024;
191   char host_port[host_port_buf_size];
192   snprintf(host_port, host_port_buf_size, "%s:%d",
193            absl::GetFlag(FLAGS_server_host).c_str(),
194            absl::GetFlag(FLAGS_server_port));
195   std::shared_ptr<grpc::Channel> channel =
196       grpc::CreateTestChannel(host_port, grpc::testing::INSECURE);
197   CHECK(channel->WaitForConnected(gpr_time_add(
198       gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(300, GPR_TIMESPAN))));
199   grpc::testing::Http2Client client(channel);
200   LOG(INFO) << "Testing case: " << absl::GetFlag(FLAGS_test_case);
201   int ret = 0;
202   if (absl::GetFlag(FLAGS_test_case) == "rst_after_header") {
203     client.DoRstAfterHeader();
204   } else if (absl::GetFlag(FLAGS_test_case) == "rst_after_data") {
205     client.DoRstAfterData();
206   } else if (absl::GetFlag(FLAGS_test_case) == "rst_during_data") {
207     client.DoRstDuringData();
208   } else if (absl::GetFlag(FLAGS_test_case) == "goaway") {
209     client.DoGoaway();
210   } else if (absl::GetFlag(FLAGS_test_case) == "ping") {
211     client.DoPing();
212   } else if (absl::GetFlag(FLAGS_test_case) == "max_streams") {
213     client.DoMaxStreams();
214   } else {
215     const char* testcases[] = {
216         "goaway",         "max_streams",      "ping",
217         "rst_after_data", "rst_after_header", "rst_during_data"};
218     char* joined_testcases =
219         gpr_strjoin_sep(testcases, GPR_ARRAY_SIZE(testcases), "\n", nullptr);
220 
221     LOG(ERROR) << "Unsupported test case " << absl::GetFlag(FLAGS_test_case)
222                << ". Valid options are\n"
223                << joined_testcases;
224     gpr_free(joined_testcases);
225     ret = 1;
226   }
227 
228   return ret;
229 }
230