Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: third_party/grpc/test/cpp/end2end/test_service_impl.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #include "test/cpp/end2end/test_service_impl.h"
35
36 #include <thread>
37
38 #include <grpc++/security/credentials.h>
39 #include <grpc++/server_context.h>
40 #include <grpc/grpc.h>
41 #include <gtest/gtest.h>
42
43 #include "src/proto/grpc/testing/echo.grpc.pb.h"
44 #include "test/cpp/util/string_ref_helper.h"
45
46 using std::chrono::system_clock;
47
48 namespace grpc {
49 namespace testing {
50 namespace {
51
52 // When echo_deadline is requested, deadline seen in the ServerContext is set in
53 // the response in seconds.
54 void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request,
55 EchoResponse* response) {
56 if (request->has_param() && request->param().echo_deadline()) {
57 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
58 if (context->deadline() != system_clock::time_point::max()) {
59 Timepoint2Timespec(context->deadline(), &deadline);
60 }
61 response->mutable_param()->set_request_deadline(deadline.tv_sec);
62 }
63 }
64
65 void CheckServerAuthContext(const ServerContext* context,
66 const grpc::string& expected_client_identity) {
67 std::shared_ptr<const AuthContext> auth_ctx = context->auth_context();
68 std::vector<grpc::string_ref> ssl =
69 auth_ctx->FindPropertyValues("transport_security_type");
70 EXPECT_EQ(1u, ssl.size());
71 EXPECT_EQ("ssl", ToString(ssl[0]));
72 if (expected_client_identity.length() == 0) {
73 EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty());
74 EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty());
75 EXPECT_FALSE(auth_ctx->IsPeerAuthenticated());
76 } else {
77 auto identity = auth_ctx->GetPeerIdentity();
78 EXPECT_TRUE(auth_ctx->IsPeerAuthenticated());
79 EXPECT_EQ(1u, identity.size());
80 EXPECT_EQ(expected_client_identity, identity[0]);
81 }
82 }
83 } // namespace
84
85 Status TestServiceImpl::Echo(ServerContext* context, const EchoRequest* request,
86 EchoResponse* response) {
87 int server_try_cancel = GetIntValueFromMetadata(
88 kServerTryCancelRequest, context->client_metadata(), DO_NOT_CANCEL);
89 if (server_try_cancel > DO_NOT_CANCEL) {
90 // Since this is a unary RPC, by the time this server handler is called,
91 // the 'request' message is already read from the client. So the scenarios
92 // in server_try_cancel don't make much sense. Just cancel the RPC as long
93 // as server_try_cancel is not DO_NOT_CANCEL
94 ServerTryCancel(context);
95 return Status::CANCELLED;
96 }
97
98 response->set_message(request->message());
99 MaybeEchoDeadline(context, request, response);
100 if (host_) {
101 response->mutable_param()->set_host(*host_);
102 }
103 if (request->has_param() && request->param().client_cancel_after_us()) {
104 {
105 std::unique_lock<std::mutex> lock(mu_);
106 signal_client_ = true;
107 }
108 while (!context->IsCancelled()) {
109 gpr_sleep_until(gpr_time_add(
110 gpr_now(GPR_CLOCK_REALTIME),
111 gpr_time_from_micros(request->param().client_cancel_after_us(),
112 GPR_TIMESPAN)));
113 }
114 return Status::CANCELLED;
115 } else if (request->has_param() &&
116 request->param().server_cancel_after_us()) {
117 gpr_sleep_until(gpr_time_add(
118 gpr_now(GPR_CLOCK_REALTIME),
119 gpr_time_from_micros(request->param().server_cancel_after_us(),
120 GPR_TIMESPAN)));
121 return Status::CANCELLED;
122 } else if (!request->has_param() ||
123 !request->param().skip_cancelled_check()) {
124 EXPECT_FALSE(context->IsCancelled());
125 }
126
127 if (request->has_param() && request->param().echo_metadata()) {
128 const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata =
129 context->client_metadata();
130 for (
131 std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
132 client_metadata.begin();
133 iter != client_metadata.end(); ++iter) {
134 context->AddTrailingMetadata(ToString(iter->first),
135 ToString(iter->second));
136 }
137 }
138 if (request->has_param() &&
139 (request->param().expected_client_identity().length() > 0 ||
140 request->param().check_auth_context())) {
141 CheckServerAuthContext(context,
142 request->param().expected_client_identity());
143 }
144 if (request->has_param() && request->param().response_message_length() > 0) {
145 response->set_message(
146 grpc::string(request->param().response_message_length(), '\0'));
147 }
148 if (request->has_param() && request->param().echo_peer()) {
149 response->mutable_param()->set_peer(context->peer());
150 }
151 return Status::OK;
152 }
153
154 // Unimplemented is left unimplemented to test the returned error.
155
156 Status TestServiceImpl::RequestStream(ServerContext* context,
157 ServerReader<EchoRequest>* reader,
158 EchoResponse* response) {
159 // If 'server_try_cancel' is set in the metadata, the RPC is cancelled by
160 // the server by calling ServerContext::TryCancel() depending on the value:
161 // CANCEL_BEFORE_PROCESSING: The RPC is cancelled before the server reads
162 // any message from the client
163 // CANCEL_DURING_PROCESSING: The RPC is cancelled while the server is
164 // reading messages from the client
165 // CANCEL_AFTER_PROCESSING: The RPC is cancelled after the server reads
166 // all the messages from the client
167 int server_try_cancel = GetIntValueFromMetadata(
168 kServerTryCancelRequest, context->client_metadata(), DO_NOT_CANCEL);
169
170 // If 'cancel_after_reads' is set in the metadata AND non-zero, the server
171 // will cancel the RPC (by just returning Status::CANCELLED - doesn't call
172 // ServerContext::TryCancel()) after reading the number of records specified
173 // by the 'cancel_after_reads' value set in the metadata.
174 int cancel_after_reads = GetIntValueFromMetadata(
175 kServerCancelAfterReads, context->client_metadata(), 0);
176
177 EchoRequest request;
178 response->set_message("");
179
180 if (server_try_cancel == CANCEL_BEFORE_PROCESSING) {
181 ServerTryCancel(context);
182 return Status::CANCELLED;
183 }
184
185 std::thread* server_try_cancel_thd = NULL;
186 if (server_try_cancel == CANCEL_DURING_PROCESSING) {
187 server_try_cancel_thd =
188 new std::thread(&TestServiceImpl::ServerTryCancel, this, context);
189 }
190
191 int num_msgs_read = 0;
192 while (reader->Read(&request)) {
193 if (cancel_after_reads == 1) {
194 gpr_log(GPR_INFO, "return cancel status");
195 return Status::CANCELLED;
196 } else if (cancel_after_reads > 0) {
197 cancel_after_reads--;
198 }
199 response->mutable_message()->append(request.message());
200 }
201 gpr_log(GPR_INFO, "Read: %d messages", num_msgs_read);
202
203 if (server_try_cancel_thd != NULL) {
204 server_try_cancel_thd->join();
205 delete server_try_cancel_thd;
206 return Status::CANCELLED;
207 }
208
209 if (server_try_cancel == CANCEL_AFTER_PROCESSING) {
210 ServerTryCancel(context);
211 return Status::CANCELLED;
212 }
213
214 return Status::OK;
215 }
216
217 // Return 'kNumResponseStreamMsgs' messages.
218 // TODO(yangg) make it generic by adding a parameter into EchoRequest
219 Status TestServiceImpl::ResponseStream(ServerContext* context,
220 const EchoRequest* request,
221 ServerWriter<EchoResponse>* writer) {
222 // If server_try_cancel is set in the metadata, the RPC is cancelled by the
223 // server by calling ServerContext::TryCancel() depending on the value:
224 // CANCEL_BEFORE_PROCESSING: The RPC is cancelled before the server writes
225 // any messages to the client
226 // CANCEL_DURING_PROCESSING: The RPC is cancelled while the server is
227 // writing messages to the client
228 // CANCEL_AFTER_PROCESSING: The RPC is cancelled after the server writes
229 // all the messages to the client
230 int server_try_cancel = GetIntValueFromMetadata(
231 kServerTryCancelRequest, context->client_metadata(), DO_NOT_CANCEL);
232
233 if (server_try_cancel == CANCEL_BEFORE_PROCESSING) {
234 ServerTryCancel(context);
235 return Status::CANCELLED;
236 }
237
238 EchoResponse response;
239 std::thread* server_try_cancel_thd = NULL;
240 if (server_try_cancel == CANCEL_DURING_PROCESSING) {
241 server_try_cancel_thd =
242 new std::thread(&TestServiceImpl::ServerTryCancel, this, context);
243 }
244
245 for (int i = 0; i < kNumResponseStreamsMsgs; i++) {
246 response.set_message(request->message() + std::to_string(i));
247 writer->Write(response);
248 }
249
250 if (server_try_cancel_thd != NULL) {
251 server_try_cancel_thd->join();
252 delete server_try_cancel_thd;
253 return Status::CANCELLED;
254 }
255
256 if (server_try_cancel == CANCEL_AFTER_PROCESSING) {
257 ServerTryCancel(context);
258 return Status::CANCELLED;
259 }
260
261 return Status::OK;
262 }
263
264 Status TestServiceImpl::BidiStream(
265 ServerContext* context,
266 ServerReaderWriter<EchoResponse, EchoRequest>* stream) {
267 // If server_try_cancel is set in the metadata, the RPC is cancelled by the
268 // server by calling ServerContext::TryCancel() depending on the value:
269 // CANCEL_BEFORE_PROCESSING: The RPC is cancelled before the server reads/
270 // writes any messages from/to the client
271 // CANCEL_DURING_PROCESSING: The RPC is cancelled while the server is
272 // reading/writing messages from/to the client
273 // CANCEL_AFTER_PROCESSING: The RPC is cancelled after the server
274 // reads/writes all messages from/to the client
275 int server_try_cancel = GetIntValueFromMetadata(
276 kServerTryCancelRequest, context->client_metadata(), DO_NOT_CANCEL);
277
278 EchoRequest request;
279 EchoResponse response;
280
281 if (server_try_cancel == CANCEL_BEFORE_PROCESSING) {
282 ServerTryCancel(context);
283 return Status::CANCELLED;
284 }
285
286 std::thread* server_try_cancel_thd = NULL;
287 if (server_try_cancel == CANCEL_DURING_PROCESSING) {
288 server_try_cancel_thd =
289 new std::thread(&TestServiceImpl::ServerTryCancel, this, context);
290 }
291
292 while (stream->Read(&request)) {
293 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
294 response.set_message(request.message());
295 stream->Write(response);
296 }
297
298 if (server_try_cancel_thd != NULL) {
299 server_try_cancel_thd->join();
300 delete server_try_cancel_thd;
301 return Status::CANCELLED;
302 }
303
304 if (server_try_cancel == CANCEL_AFTER_PROCESSING) {
305 ServerTryCancel(context);
306 return Status::CANCELLED;
307 }
308
309 return Status::OK;
310 }
311
312 int TestServiceImpl::GetIntValueFromMetadata(
313 const char* key,
314 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
315 int default_value) {
316 if (metadata.find(key) != metadata.end()) {
317 std::istringstream iss(ToString(metadata.find(key)->second));
318 iss >> default_value;
319 gpr_log(GPR_INFO, "%s : %d", key, default_value);
320 }
321
322 return default_value;
323 }
324
325 void TestServiceImpl::ServerTryCancel(ServerContext* context) {
326 EXPECT_FALSE(context->IsCancelled());
327 context->TryCancel();
328 gpr_log(GPR_INFO, "Server called TryCancel() to cancel the request");
329 // Now wait until it's really canceled
330 while (!context->IsCancelled()) {
331 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
332 gpr_time_from_micros(1000, GPR_TIMESPAN)));
333 }
334 }
335
336 } // namespace testing
337 } // namespace grpc
OLDNEW
« no previous file with comments | « third_party/grpc/test/cpp/end2end/test_service_impl.h ('k') | third_party/grpc/test/cpp/end2end/thread_stress_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698