blob: 8e2d778cff7489d19b01d6e9f9f4a6667856b00e [file] [log] [blame]
Yang Gaoa4002072015-04-09 23:25:21 -07001/*
2 *
3 * Copyright 2015, 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/interop/interop_client.h"
35
David Garcia Quintasc8993192015-07-22 09:10:39 -070036#include <fstream>
Yang Gaoa4002072015-04-09 23:25:21 -070037#include <memory>
38
39#include <unistd.h>
40
41#include <grpc/grpc.h>
42#include <grpc/support/log.h>
David Garcia Quintasc8993192015-07-22 09:10:39 -070043#include <grpc/support/string_util.h>
Yang Gaoa4002072015-04-09 23:25:21 -070044#include <grpc++/channel_interface.h>
45#include <grpc++/client_context.h>
yang-g5bf510b2015-07-14 10:54:29 -070046#include <grpc++/credentials.h>
Yang Gaoa4002072015-04-09 23:25:21 -070047#include <grpc++/status.h>
48#include <grpc++/stream.h>
David Garcia Quintas80f39952015-07-21 16:07:36 -070049
50#include "test/cpp/interop/client_helper.h"
Abhishek Kumar1b3e3cd2015-04-16 20:10:29 -070051#include "test/proto/test.grpc.pb.h"
Abhishek Kumar60572d42015-04-16 20:45:25 -070052#include "test/proto/empty.grpc.pb.h"
53#include "test/proto/messages.grpc.pb.h"
David Garcia Quintasc8993192015-07-22 09:10:39 -070054#include "src/core/transport/stream_op.h"
Yang Gaoa4002072015-04-09 23:25:21 -070055
56namespace grpc {
57namespace testing {
58
David Garcia Quintasc8993192015-07-22 09:10:39 -070059static const char* kRandomFile = "test/cpp/interop/rnd.dat";
60
Yang Gaoa4002072015-04-09 23:25:21 -070061namespace {
62// The same value is defined by the Java client.
63const std::vector<int> request_stream_sizes = {27182, 8, 1828, 45904};
64const std::vector<int> response_stream_sizes = {31415, 9, 2653, 58979};
65const int kNumResponseMessages = 2000;
66const int kResponseMessageSize = 1030;
67const int kReceiveDelayMilliSeconds = 20;
Xudong Maa5861f32015-05-26 15:24:11 -070068const int kLargeRequestSize = 271828;
69const int kLargeResponseSize = 314159;
Yang Gaoa4002072015-04-09 23:25:21 -070070} // namespace
71
72InteropClient::InteropClient(std::shared_ptr<ChannelInterface> channel)
73 : channel_(channel) {}
74
75void InteropClient::AssertOkOrPrintErrorStatus(const Status& s) {
Yang Gaoc1a2c312015-06-16 10:59:46 -070076 if (s.ok()) {
Yang Gaoa4002072015-04-09 23:25:21 -070077 return;
78 }
Yang Gaoc1a2c312015-06-16 10:59:46 -070079 gpr_log(GPR_INFO, "Error status code: %d, message: %s", s.error_code(),
80 s.error_message().c_str());
Yang Gaoa4002072015-04-09 23:25:21 -070081 GPR_ASSERT(0);
82}
83
84void InteropClient::DoEmpty() {
85 gpr_log(GPR_INFO, "Sending an empty rpc...");
86 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
87
88 Empty request = Empty::default_instance();
89 Empty response = Empty::default_instance();
90 ClientContext context;
91
92 Status s = stub->EmptyCall(&context, request, &response);
93 AssertOkOrPrintErrorStatus(s);
94
95 gpr_log(GPR_INFO, "Empty rpc done.");
96}
97
98// Shared code to set large payload, make rpc and check response payload.
99void InteropClient::PerformLargeUnary(SimpleRequest* request,
100 SimpleResponse* response) {
101 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
102
103 ClientContext context;
David Garcia Quintas80f39952015-07-21 16:07:36 -0700104 InteropClientContextInspector inspector(context);
Yang Gaoa4002072015-04-09 23:25:21 -0700105 request->set_response_size(kLargeResponseSize);
106 grpc::string payload(kLargeRequestSize, '\0');
107 request->mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
108
109 Status s = stub->UnaryCall(&context, *request, response);
110
David Garcia Quintasc8993192015-07-22 09:10:39 -0700111 // Compression related checks.
David Garcia Quintas80f39952015-07-21 16:07:36 -0700112 GPR_ASSERT(request->response_compression() ==
113 GetInteropCompressionTypeFromCompressionAlgorithm(
114 inspector.GetCallCompressionAlgorithm()));
David Garcia Quintasc8993192015-07-22 09:10:39 -0700115 if (request->response_compression() == NONE) {
116 GPR_ASSERT(!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS));
117 } else if (request->response_type() == PayloadType::COMPRESSABLE) {
118 // requested compression and compressable response => results should always
119 // be compressed.
120 GPR_ASSERT(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS);
121 }
122
Yang Gaoa4002072015-04-09 23:25:21 -0700123 AssertOkOrPrintErrorStatus(s);
David Garcia Quintasc8993192015-07-22 09:10:39 -0700124
125 // Payload related checks.
126 if (request->response_type() != PayloadType::RANDOM) {
127 GPR_ASSERT(response->payload().type() == request->response_type());
128 }
129 switch (response->payload().type()) {
130 case PayloadType::COMPRESSABLE:
131 GPR_ASSERT(response->payload().body() ==
132 grpc::string(kLargeResponseSize, '\0'));
133 break;
134 case PayloadType::UNCOMPRESSABLE: {
135 std::ifstream rnd_file(kRandomFile);
136 GPR_ASSERT(rnd_file.good());
137 for (int i = 0; i < kLargeResponseSize; i++) {
138 GPR_ASSERT(response->payload().body()[i] == (char)rnd_file.get());
139 }
140 }
141 break;
142 default:
143 GPR_ASSERT(false);
144 }
Yang Gaoa4002072015-04-09 23:25:21 -0700145}
146
147void InteropClient::DoComputeEngineCreds(
148 const grpc::string& default_service_account,
149 const grpc::string& oauth_scope) {
150 gpr_log(GPR_INFO,
151 "Sending a large unary rpc with compute engine credentials ...");
152 SimpleRequest request;
153 SimpleResponse response;
154 request.set_fill_username(true);
155 request.set_fill_oauth_scope(true);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700156 request.set_response_type(PayloadType::COMPRESSABLE);
Yang Gaoa4002072015-04-09 23:25:21 -0700157 PerformLargeUnary(&request, &response);
158 gpr_log(GPR_INFO, "Got username %s", response.username().c_str());
159 gpr_log(GPR_INFO, "Got oauth_scope %s", response.oauth_scope().c_str());
160 GPR_ASSERT(!response.username().empty());
161 GPR_ASSERT(response.username().c_str() == default_service_account);
162 GPR_ASSERT(!response.oauth_scope().empty());
163 const char* oauth_scope_str = response.oauth_scope().c_str();
164 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
165 gpr_log(GPR_INFO, "Large unary with compute engine creds done.");
166}
167
168void InteropClient::DoServiceAccountCreds(const grpc::string& username,
169 const grpc::string& oauth_scope) {
170 gpr_log(GPR_INFO,
171 "Sending a large unary rpc with service account credentials ...");
172 SimpleRequest request;
173 SimpleResponse response;
174 request.set_fill_username(true);
175 request.set_fill_oauth_scope(true);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700176 request.set_response_type(PayloadType::COMPRESSABLE);
Yang Gaoa4002072015-04-09 23:25:21 -0700177 PerformLargeUnary(&request, &response);
178 GPR_ASSERT(!response.username().empty());
179 GPR_ASSERT(!response.oauth_scope().empty());
180 GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
181 const char* oauth_scope_str = response.oauth_scope().c_str();
182 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
183 gpr_log(GPR_INFO, "Large unary with service account creds done.");
184}
185
yang-gbe5f0592015-07-13 11:11:50 -0700186void InteropClient::DoOauth2AuthToken(const grpc::string& username,
187 const grpc::string& oauth_scope) {
188 gpr_log(GPR_INFO,
yang-g463cde72015-07-17 15:21:39 -0700189 "Sending a unary rpc with raw oauth2 access token credentials ...");
yang-gbe5f0592015-07-13 11:11:50 -0700190 SimpleRequest request;
191 SimpleResponse response;
192 request.set_fill_username(true);
193 request.set_fill_oauth_scope(true);
yang-g463cde72015-07-17 15:21:39 -0700194 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
195
196 ClientContext context;
197
198 Status s = stub->UnaryCall(&context, request, &response);
199
200 AssertOkOrPrintErrorStatus(s);
yang-gbe5f0592015-07-13 11:11:50 -0700201 GPR_ASSERT(!response.username().empty());
202 GPR_ASSERT(!response.oauth_scope().empty());
203 GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
204 const char* oauth_scope_str = response.oauth_scope().c_str();
205 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
yang-g463cde72015-07-17 15:21:39 -0700206 gpr_log(GPR_INFO, "Unary with oauth2 access token credentials done.");
yang-gbe5f0592015-07-13 11:11:50 -0700207}
208
yang-g5bf510b2015-07-14 10:54:29 -0700209void InteropClient::DoPerRpcCreds(const grpc::string& username,
210 const grpc::string& oauth_scope) {
211 gpr_log(GPR_INFO,
yang-g8c31ee22015-07-15 13:36:27 -0700212 "Sending a unary rpc with per-rpc raw oauth2 access token ...");
yang-g5bf510b2015-07-14 10:54:29 -0700213 SimpleRequest request;
214 SimpleResponse response;
215 request.set_fill_username(true);
216 request.set_fill_oauth_scope(true);
217 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
218
219 ClientContext context;
220 grpc::string access_token = GetOauth2AccessToken();
221 std::shared_ptr<Credentials> creds = AccessTokenCredentials(access_token);
222 context.set_credentials(creds);
yang-g5bf510b2015-07-14 10:54:29 -0700223
224 Status s = stub->UnaryCall(&context, request, &response);
225
226 AssertOkOrPrintErrorStatus(s);
yang-g5bf510b2015-07-14 10:54:29 -0700227 GPR_ASSERT(!response.username().empty());
228 GPR_ASSERT(!response.oauth_scope().empty());
229 GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
230 const char* oauth_scope_str = response.oauth_scope().c_str();
231 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
yang-g8c31ee22015-07-15 13:36:27 -0700232 gpr_log(GPR_INFO, "Unary with per-rpc oauth2 access token done.");
yang-g5bf510b2015-07-14 10:54:29 -0700233}
234
Yang Gaoa4002072015-04-09 23:25:21 -0700235void InteropClient::DoJwtTokenCreds(const grpc::string& username) {
236 gpr_log(GPR_INFO, "Sending a large unary rpc with JWT token credentials ...");
237 SimpleRequest request;
238 SimpleResponse response;
239 request.set_fill_username(true);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700240 request.set_response_type(PayloadType::COMPRESSABLE);
Yang Gaoa4002072015-04-09 23:25:21 -0700241 PerformLargeUnary(&request, &response);
242 GPR_ASSERT(!response.username().empty());
243 GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
244 gpr_log(GPR_INFO, "Large unary with JWT token creds done.");
245}
246
247void InteropClient::DoLargeUnary() {
David Garcia Quintas80f39952015-07-21 16:07:36 -0700248 const CompressionType compression_types[] = {NONE, GZIP, DEFLATE};
249 const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM};
250 for (const auto payload_type : payload_types) {
251 for (const auto compression_type : compression_types) {
David Garcia Quintasc8993192015-07-22 09:10:39 -0700252 char* log_suffix;
253 gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)",
254 CompressionType_Name(compression_type).c_str(),
255 PayloadType_Name(payload_type).c_str());
256
257 gpr_log(GPR_INFO, "Sending a large unary rpc %s.", log_suffix);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700258 SimpleRequest request;
259 SimpleResponse response;
260 request.set_response_type(payload_type);
261 request.set_response_compression(compression_type);
262 PerformLargeUnary(&request, &response);
David Garcia Quintasc8993192015-07-22 09:10:39 -0700263 gpr_log(GPR_INFO, "Large unary done %s.", log_suffix);
264 gpr_free(log_suffix);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700265 }
266 }
Yang Gaoa4002072015-04-09 23:25:21 -0700267}
268
269void InteropClient::DoRequestStreaming() {
270 gpr_log(GPR_INFO, "Sending request steaming rpc ...");
271 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
272
273 ClientContext context;
274 StreamingInputCallRequest request;
275 StreamingInputCallResponse response;
276
277 std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream(
278 stub->StreamingInputCall(&context, &response));
279
280 int aggregated_payload_size = 0;
281 for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) {
282 Payload* payload = request.mutable_payload();
283 payload->set_body(grpc::string(request_stream_sizes[i], '\0'));
284 GPR_ASSERT(stream->Write(request));
285 aggregated_payload_size += request_stream_sizes[i];
286 }
287 stream->WritesDone();
288 Status s = stream->Finish();
289
290 GPR_ASSERT(response.aggregated_payload_size() == aggregated_payload_size);
291 AssertOkOrPrintErrorStatus(s);
292 gpr_log(GPR_INFO, "Request streaming done.");
293}
294
295void InteropClient::DoResponseStreaming() {
Yang Gaoa4002072015-04-09 23:25:21 -0700296 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
297
David Garcia Quintasc8993192015-07-22 09:10:39 -0700298 const CompressionType compression_types[] = {NONE, GZIP, DEFLATE};
299 const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM};
300 for (const auto payload_type : payload_types) {
301 for (const auto compression_type : compression_types) {
302 ClientContext context;
303 InteropClientContextInspector inspector(context);
304 StreamingOutputCallRequest request;
David Garcia Quintas80f39952015-07-21 16:07:36 -0700305
David Garcia Quintasc8993192015-07-22 09:10:39 -0700306 char* log_suffix;
307 gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)",
308 CompressionType_Name(compression_type).c_str(),
309 PayloadType_Name(payload_type).c_str());
310
311 gpr_log(GPR_INFO, "Receiving response steaming rpc %s.", log_suffix);
312
313 request.set_response_type(payload_type);
314 request.set_response_compression(compression_type);
315
316 for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
317 ResponseParameters* response_parameter =
318 request.add_response_parameters();
319 response_parameter->set_size(response_stream_sizes[i]);
320 }
321 StreamingOutputCallResponse response;
322
323 std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
324 stub->StreamingOutputCall(&context, request));
325
326 unsigned int i = 0;
327 while (stream->Read(&response)) {
328 GPR_ASSERT(response.payload().body() ==
329 grpc::string(response_stream_sizes[i], '\0'));
330
331 // Compression related checks.
332 GPR_ASSERT(request.response_compression() ==
333 GetInteropCompressionTypeFromCompressionAlgorithm(
334 inspector.GetCallCompressionAlgorithm()));
335 if (request.response_compression() == NONE) {
336 GPR_ASSERT(
337 !(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS));
338 } else if (request.response_type() == PayloadType::COMPRESSABLE) {
339 // requested compression and compressable response => results should
340 // always be compressed.
341 GPR_ASSERT(inspector.GetMessageFlags() &
342 GRPC_WRITE_INTERNAL_COMPRESS);
343 }
344
345 ++i;
346 }
347
348 GPR_ASSERT(response_stream_sizes.size() == i);
349 Status s = stream->Finish();
350
351 AssertOkOrPrintErrorStatus(s);
352 gpr_log(GPR_INFO, "Response streaming done %s.", log_suffix);
353 gpr_free(log_suffix);
354 }
Yang Gaoa4002072015-04-09 23:25:21 -0700355 }
Yang Gaoa4002072015-04-09 23:25:21 -0700356}
357
358void InteropClient::DoResponseStreamingWithSlowConsumer() {
359 gpr_log(GPR_INFO, "Receiving response steaming rpc with slow consumer ...");
360 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
361
362 ClientContext context;
363 StreamingOutputCallRequest request;
364
365 for (int i = 0; i < kNumResponseMessages; ++i) {
366 ResponseParameters* response_parameter = request.add_response_parameters();
367 response_parameter->set_size(kResponseMessageSize);
368 }
369 StreamingOutputCallResponse response;
370 std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
371 stub->StreamingOutputCall(&context, request));
372
373 int i = 0;
374 while (stream->Read(&response)) {
375 GPR_ASSERT(response.payload().body() ==
376 grpc::string(kResponseMessageSize, '\0'));
377 gpr_log(GPR_INFO, "received message %d", i);
378 usleep(kReceiveDelayMilliSeconds * 1000);
379 ++i;
380 }
381 GPR_ASSERT(kNumResponseMessages == i);
382 Status s = stream->Finish();
383
384 AssertOkOrPrintErrorStatus(s);
385 gpr_log(GPR_INFO, "Response streaming done.");
386}
387
388void InteropClient::DoHalfDuplex() {
389 gpr_log(GPR_INFO, "Sending half-duplex streaming rpc ...");
390 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
391
392 ClientContext context;
393 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
394 StreamingOutputCallResponse>>
395 stream(stub->HalfDuplexCall(&context));
396
397 StreamingOutputCallRequest request;
398 ResponseParameters* response_parameter = request.add_response_parameters();
399 for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
400 response_parameter->set_size(response_stream_sizes[i]);
401 GPR_ASSERT(stream->Write(request));
402 }
403 stream->WritesDone();
404
405 unsigned int i = 0;
406 StreamingOutputCallResponse response;
407 while (stream->Read(&response)) {
408 GPR_ASSERT(response.payload().has_body());
409 GPR_ASSERT(response.payload().body() ==
410 grpc::string(response_stream_sizes[i], '\0'));
411 ++i;
412 }
413 GPR_ASSERT(response_stream_sizes.size() == i);
414 Status s = stream->Finish();
415 AssertOkOrPrintErrorStatus(s);
416 gpr_log(GPR_INFO, "Half-duplex streaming rpc done.");
417}
418
419void InteropClient::DoPingPong() {
420 gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ...");
421 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
422
423 ClientContext context;
424 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
425 StreamingOutputCallResponse>>
426 stream(stub->FullDuplexCall(&context));
427
428 StreamingOutputCallRequest request;
429 request.set_response_type(PayloadType::COMPRESSABLE);
430 ResponseParameters* response_parameter = request.add_response_parameters();
431 Payload* payload = request.mutable_payload();
432 StreamingOutputCallResponse response;
433 for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) {
434 response_parameter->set_size(response_stream_sizes[i]);
435 payload->set_body(grpc::string(request_stream_sizes[i], '\0'));
436 GPR_ASSERT(stream->Write(request));
437 GPR_ASSERT(stream->Read(&response));
438 GPR_ASSERT(response.payload().has_body());
439 GPR_ASSERT(response.payload().body() ==
440 grpc::string(response_stream_sizes[i], '\0'));
441 }
442
443 stream->WritesDone();
444 GPR_ASSERT(!stream->Read(&response));
445 Status s = stream->Finish();
446 AssertOkOrPrintErrorStatus(s);
447 gpr_log(GPR_INFO, "Ping pong streaming done.");
448}
449
Yang Gao68d61572015-04-24 14:42:42 -0700450void InteropClient::DoCancelAfterBegin() {
451 gpr_log(GPR_INFO, "Sending request steaming rpc ...");
452 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
453
454 ClientContext context;
455 StreamingInputCallRequest request;
456 StreamingInputCallResponse response;
457
458 std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream(
459 stub->StreamingInputCall(&context, &response));
460
461 gpr_log(GPR_INFO, "Trying to cancel...");
462 context.TryCancel();
463 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700464 GPR_ASSERT(s.error_code() == StatusCode::CANCELLED);
Yang Gao68d61572015-04-24 14:42:42 -0700465 gpr_log(GPR_INFO, "Canceling streaming done.");
466}
467
468void InteropClient::DoCancelAfterFirstResponse() {
469 gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ...");
470 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
471
472 ClientContext context;
473 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
474 StreamingOutputCallResponse>>
475 stream(stub->FullDuplexCall(&context));
476
477 StreamingOutputCallRequest request;
478 request.set_response_type(PayloadType::COMPRESSABLE);
479 ResponseParameters* response_parameter = request.add_response_parameters();
480 response_parameter->set_size(31415);
481 request.mutable_payload()->set_body(grpc::string(27182, '\0'));
482 StreamingOutputCallResponse response;
483 GPR_ASSERT(stream->Write(request));
484 GPR_ASSERT(stream->Read(&response));
485 GPR_ASSERT(response.payload().has_body());
486 GPR_ASSERT(response.payload().body() == grpc::string(31415, '\0'));
487 gpr_log(GPR_INFO, "Trying to cancel...");
488 context.TryCancel();
489
490 Status s = stream->Finish();
491 gpr_log(GPR_INFO, "Canceling pingpong streaming done.");
492}
493
yang-g69563b92015-07-10 15:32:11 -0700494void InteropClient::DoTimeoutOnSleepingServer() {
495 gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc with a short deadline...");
496 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
497
498 ClientContext context;
499 std::chrono::system_clock::time_point deadline =
500 std::chrono::system_clock::now() + std::chrono::milliseconds(1);
501 context.set_deadline(deadline);
502 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
503 StreamingOutputCallResponse>>
504 stream(stub->FullDuplexCall(&context));
505
506 StreamingOutputCallRequest request;
507 request.mutable_payload()->set_body(grpc::string(27182, '\0'));
508 stream->Write(request);
509
510 Status s = stream->Finish();
511 GPR_ASSERT(s.error_code() == StatusCode::DEADLINE_EXCEEDED);
512 gpr_log(GPR_INFO, "Pingpong streaming timeout done.");
513}
514
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700515void InteropClient::DoStatusWithMessage() {
516 gpr_log(GPR_INFO, "Sending RPC with a request for status code 2 and message");
517 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
518
519 ClientContext context;
520 SimpleRequest request;
521 SimpleResponse response;
522 EchoStatus *requested_status = request.mutable_response_status();
523 requested_status->set_code(grpc::StatusCode::UNKNOWN);
524 grpc::string test_msg = "This is a test message";
525 requested_status->set_message(test_msg);
526
527 Status s = stub->UnaryCall(&context, request, &response);
528
529 GPR_ASSERT(s.error_code() == grpc::StatusCode::UNKNOWN);
530 GPR_ASSERT(s.error_message() == test_msg);
531 gpr_log(GPR_INFO, "Done testing Status and Message");
532}
533
Yang Gaoa4002072015-04-09 23:25:21 -0700534} // namespace testing
535} // namespace grpc