blob: fc0e325e415bc91bc2a8a7b5579b09d5b9bebc13 [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 Quintascd37d582015-08-09 15:50:21 -0700104 InteropClientContextInspector inspector(context);
David Garcia Quintas616b3752015-08-11 15:21:02 -0700105 request->set_response_type(PayloadType::COMPRESSABLE);
David Garcia Quintascd37d582015-08-09 15:50:21 -0700106 request->set_response_size(kLargeResponseSize);
107 grpc::string payload(kLargeRequestSize, '\0');
108 request->mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
109
David Garcia Quintas2e1bb1b2015-08-10 14:05:57 -0700110 Status s = stub->UnaryCall(&context, *request, response);
David Garcia Quintascd37d582015-08-09 15:50:21 -0700111
David Garcia Quintasc8993192015-07-22 09:10:39 -0700112 // Compression related checks.
David Garcia Quintas80f39952015-07-21 16:07:36 -0700113 GPR_ASSERT(request->response_compression() ==
114 GetInteropCompressionTypeFromCompressionAlgorithm(
115 inspector.GetCallCompressionAlgorithm()));
David Garcia Quintasc8993192015-07-22 09:10:39 -0700116 if (request->response_compression() == NONE) {
117 GPR_ASSERT(!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS));
118 } else if (request->response_type() == PayloadType::COMPRESSABLE) {
119 // requested compression and compressable response => results should always
120 // be compressed.
121 GPR_ASSERT(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS);
122 }
123
Yang Gaoa4002072015-04-09 23:25:21 -0700124 AssertOkOrPrintErrorStatus(s);
David Garcia Quintasc8993192015-07-22 09:10:39 -0700125
126 // Payload related checks.
127 if (request->response_type() != PayloadType::RANDOM) {
128 GPR_ASSERT(response->payload().type() == request->response_type());
129 }
130 switch (response->payload().type()) {
131 case PayloadType::COMPRESSABLE:
132 GPR_ASSERT(response->payload().body() ==
133 grpc::string(kLargeResponseSize, '\0'));
134 break;
135 case PayloadType::UNCOMPRESSABLE: {
136 std::ifstream rnd_file(kRandomFile);
137 GPR_ASSERT(rnd_file.good());
138 for (int i = 0; i < kLargeResponseSize; i++) {
139 GPR_ASSERT(response->payload().body()[i] == (char)rnd_file.get());
140 }
141 }
142 break;
143 default:
144 GPR_ASSERT(false);
145 }
Yang Gaoa4002072015-04-09 23:25:21 -0700146}
147
148void InteropClient::DoComputeEngineCreds(
149 const grpc::string& default_service_account,
150 const grpc::string& oauth_scope) {
151 gpr_log(GPR_INFO,
152 "Sending a large unary rpc with compute engine credentials ...");
153 SimpleRequest request;
154 SimpleResponse response;
155 request.set_fill_username(true);
156 request.set_fill_oauth_scope(true);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700157 request.set_response_type(PayloadType::COMPRESSABLE);
Yang Gaoa4002072015-04-09 23:25:21 -0700158 PerformLargeUnary(&request, &response);
159 gpr_log(GPR_INFO, "Got username %s", response.username().c_str());
160 gpr_log(GPR_INFO, "Got oauth_scope %s", response.oauth_scope().c_str());
161 GPR_ASSERT(!response.username().empty());
162 GPR_ASSERT(response.username().c_str() == default_service_account);
163 GPR_ASSERT(!response.oauth_scope().empty());
164 const char* oauth_scope_str = response.oauth_scope().c_str();
165 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
166 gpr_log(GPR_INFO, "Large unary with compute engine creds done.");
167}
168
169void InteropClient::DoServiceAccountCreds(const grpc::string& username,
170 const grpc::string& oauth_scope) {
171 gpr_log(GPR_INFO,
172 "Sending a large unary rpc with service account credentials ...");
173 SimpleRequest request;
174 SimpleResponse response;
175 request.set_fill_username(true);
176 request.set_fill_oauth_scope(true);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700177 request.set_response_type(PayloadType::COMPRESSABLE);
Yang Gaoa4002072015-04-09 23:25:21 -0700178 PerformLargeUnary(&request, &response);
179 GPR_ASSERT(!response.username().empty());
180 GPR_ASSERT(!response.oauth_scope().empty());
181 GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
182 const char* oauth_scope_str = response.oauth_scope().c_str();
183 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
184 gpr_log(GPR_INFO, "Large unary with service account creds done.");
185}
186
yang-gbe5f0592015-07-13 11:11:50 -0700187void InteropClient::DoOauth2AuthToken(const grpc::string& username,
188 const grpc::string& oauth_scope) {
189 gpr_log(GPR_INFO,
yang-g463cde72015-07-17 15:21:39 -0700190 "Sending a unary rpc with raw oauth2 access token credentials ...");
yang-gbe5f0592015-07-13 11:11:50 -0700191 SimpleRequest request;
192 SimpleResponse response;
193 request.set_fill_username(true);
194 request.set_fill_oauth_scope(true);
yang-g463cde72015-07-17 15:21:39 -0700195 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
196
197 ClientContext context;
198
199 Status s = stub->UnaryCall(&context, request, &response);
200
201 AssertOkOrPrintErrorStatus(s);
yang-gbe5f0592015-07-13 11:11:50 -0700202 GPR_ASSERT(!response.username().empty());
203 GPR_ASSERT(!response.oauth_scope().empty());
204 GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
205 const char* oauth_scope_str = response.oauth_scope().c_str();
206 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
yang-g463cde72015-07-17 15:21:39 -0700207 gpr_log(GPR_INFO, "Unary with oauth2 access token credentials done.");
yang-gbe5f0592015-07-13 11:11:50 -0700208}
209
yang-g5bf510b2015-07-14 10:54:29 -0700210void InteropClient::DoPerRpcCreds(const grpc::string& username,
211 const grpc::string& oauth_scope) {
212 gpr_log(GPR_INFO,
yang-g8c31ee22015-07-15 13:36:27 -0700213 "Sending a unary rpc with per-rpc raw oauth2 access token ...");
yang-g5bf510b2015-07-14 10:54:29 -0700214 SimpleRequest request;
215 SimpleResponse response;
216 request.set_fill_username(true);
217 request.set_fill_oauth_scope(true);
218 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
219
220 ClientContext context;
221 grpc::string access_token = GetOauth2AccessToken();
222 std::shared_ptr<Credentials> creds = AccessTokenCredentials(access_token);
223 context.set_credentials(creds);
yang-g5bf510b2015-07-14 10:54:29 -0700224
225 Status s = stub->UnaryCall(&context, request, &response);
226
227 AssertOkOrPrintErrorStatus(s);
yang-g5bf510b2015-07-14 10:54:29 -0700228 GPR_ASSERT(!response.username().empty());
229 GPR_ASSERT(!response.oauth_scope().empty());
230 GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
231 const char* oauth_scope_str = response.oauth_scope().c_str();
232 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
yang-g8c31ee22015-07-15 13:36:27 -0700233 gpr_log(GPR_INFO, "Unary with per-rpc oauth2 access token done.");
yang-g5bf510b2015-07-14 10:54:29 -0700234}
235
Yang Gaoa4002072015-04-09 23:25:21 -0700236void InteropClient::DoJwtTokenCreds(const grpc::string& username) {
237 gpr_log(GPR_INFO, "Sending a large unary rpc with JWT token credentials ...");
238 SimpleRequest request;
239 SimpleResponse response;
240 request.set_fill_username(true);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700241 request.set_response_type(PayloadType::COMPRESSABLE);
Yang Gaoa4002072015-04-09 23:25:21 -0700242 PerformLargeUnary(&request, &response);
243 GPR_ASSERT(!response.username().empty());
244 GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
245 gpr_log(GPR_INFO, "Large unary with JWT token creds done.");
246}
247
248void InteropClient::DoLargeUnary() {
David Garcia Quintascd37d582015-08-09 15:50:21 -0700249 gpr_log(GPR_INFO, "Sending a large unary rpc...");
250 SimpleRequest request;
251 SimpleResponse response;
252 PerformLargeUnary(&request, &response);
253 gpr_log(GPR_INFO, "Large unary done.");
254}
255
256void InteropClient::DoLargeCompressedUnary() {
David Garcia Quintas80f39952015-07-21 16:07:36 -0700257 const CompressionType compression_types[] = {NONE, GZIP, DEFLATE};
258 const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM};
259 for (const auto payload_type : payload_types) {
260 for (const auto compression_type : compression_types) {
David Garcia Quintasc8993192015-07-22 09:10:39 -0700261 char* log_suffix;
262 gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)",
263 CompressionType_Name(compression_type).c_str(),
264 PayloadType_Name(payload_type).c_str());
265
David Garcia Quintas616b3752015-08-11 15:21:02 -0700266 gpr_log(GPR_INFO, "Sending a large compressed unary rpc %s.", log_suffix);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700267 SimpleRequest request;
268 SimpleResponse response;
269 request.set_response_type(payload_type);
270 request.set_response_compression(compression_type);
271 PerformLargeUnary(&request, &response);
David Garcia Quintas616b3752015-08-11 15:21:02 -0700272 gpr_log(GPR_INFO, "Large compressed unary done %s.", log_suffix);
David Garcia Quintasc8993192015-07-22 09:10:39 -0700273 gpr_free(log_suffix);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700274 }
275 }
Yang Gaoa4002072015-04-09 23:25:21 -0700276}
277
278void InteropClient::DoRequestStreaming() {
279 gpr_log(GPR_INFO, "Sending request steaming rpc ...");
280 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
281
282 ClientContext context;
283 StreamingInputCallRequest request;
284 StreamingInputCallResponse response;
285
286 std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream(
287 stub->StreamingInputCall(&context, &response));
288
289 int aggregated_payload_size = 0;
290 for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) {
291 Payload* payload = request.mutable_payload();
292 payload->set_body(grpc::string(request_stream_sizes[i], '\0'));
293 GPR_ASSERT(stream->Write(request));
294 aggregated_payload_size += request_stream_sizes[i];
295 }
296 stream->WritesDone();
297 Status s = stream->Finish();
298
299 GPR_ASSERT(response.aggregated_payload_size() == aggregated_payload_size);
300 AssertOkOrPrintErrorStatus(s);
301 gpr_log(GPR_INFO, "Request streaming done.");
302}
303
304void InteropClient::DoResponseStreaming() {
David Garcia Quintascd37d582015-08-09 15:50:21 -0700305 gpr_log(GPR_INFO, "Receiving response steaming rpc ...");
306 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
307
308 ClientContext context;
309 StreamingOutputCallRequest request;
310 for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
311 ResponseParameters* response_parameter = request.add_response_parameters();
312 response_parameter->set_size(response_stream_sizes[i]);
313 }
314 StreamingOutputCallResponse response;
315 std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
316 stub->StreamingOutputCall(&context, request));
317
318 unsigned int i = 0;
319 while (stream->Read(&response)) {
320 GPR_ASSERT(response.payload().body() ==
321 grpc::string(response_stream_sizes[i], '\0'));
322 ++i;
323 }
324 GPR_ASSERT(response_stream_sizes.size() == i);
325 Status s = stream->Finish();
326 AssertOkOrPrintErrorStatus(s);
327 gpr_log(GPR_INFO, "Response streaming done.");
328}
329
330void InteropClient::DoResponseCompressedStreaming() {
Yang Gaoa4002072015-04-09 23:25:21 -0700331 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
332
David Garcia Quintasc8993192015-07-22 09:10:39 -0700333 const CompressionType compression_types[] = {NONE, GZIP, DEFLATE};
334 const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM};
335 for (const auto payload_type : payload_types) {
336 for (const auto compression_type : compression_types) {
337 ClientContext context;
338 InteropClientContextInspector inspector(context);
339 StreamingOutputCallRequest request;
David Garcia Quintas80f39952015-07-21 16:07:36 -0700340
David Garcia Quintasc8993192015-07-22 09:10:39 -0700341 char* log_suffix;
342 gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)",
343 CompressionType_Name(compression_type).c_str(),
344 PayloadType_Name(payload_type).c_str());
345
346 gpr_log(GPR_INFO, "Receiving response steaming rpc %s.", log_suffix);
347
348 request.set_response_type(payload_type);
349 request.set_response_compression(compression_type);
350
351 for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
352 ResponseParameters* response_parameter =
353 request.add_response_parameters();
354 response_parameter->set_size(response_stream_sizes[i]);
355 }
356 StreamingOutputCallResponse response;
357
358 std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
359 stub->StreamingOutputCall(&context, request));
360
361 unsigned int i = 0;
362 while (stream->Read(&response)) {
363 GPR_ASSERT(response.payload().body() ==
364 grpc::string(response_stream_sizes[i], '\0'));
365
366 // Compression related checks.
367 GPR_ASSERT(request.response_compression() ==
368 GetInteropCompressionTypeFromCompressionAlgorithm(
369 inspector.GetCallCompressionAlgorithm()));
370 if (request.response_compression() == NONE) {
371 GPR_ASSERT(
372 !(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS));
373 } else if (request.response_type() == PayloadType::COMPRESSABLE) {
374 // requested compression and compressable response => results should
375 // always be compressed.
376 GPR_ASSERT(inspector.GetMessageFlags() &
377 GRPC_WRITE_INTERNAL_COMPRESS);
378 }
379
380 ++i;
381 }
382
383 GPR_ASSERT(response_stream_sizes.size() == i);
384 Status s = stream->Finish();
385
386 AssertOkOrPrintErrorStatus(s);
387 gpr_log(GPR_INFO, "Response streaming done %s.", log_suffix);
388 gpr_free(log_suffix);
389 }
Yang Gaoa4002072015-04-09 23:25:21 -0700390 }
Yang Gaoa4002072015-04-09 23:25:21 -0700391}
392
393void InteropClient::DoResponseStreamingWithSlowConsumer() {
394 gpr_log(GPR_INFO, "Receiving response steaming rpc with slow consumer ...");
395 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
396
397 ClientContext context;
398 StreamingOutputCallRequest request;
399
400 for (int i = 0; i < kNumResponseMessages; ++i) {
401 ResponseParameters* response_parameter = request.add_response_parameters();
402 response_parameter->set_size(kResponseMessageSize);
403 }
404 StreamingOutputCallResponse response;
405 std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
406 stub->StreamingOutputCall(&context, request));
407
408 int i = 0;
409 while (stream->Read(&response)) {
410 GPR_ASSERT(response.payload().body() ==
411 grpc::string(kResponseMessageSize, '\0'));
412 gpr_log(GPR_INFO, "received message %d", i);
413 usleep(kReceiveDelayMilliSeconds * 1000);
414 ++i;
415 }
416 GPR_ASSERT(kNumResponseMessages == i);
417 Status s = stream->Finish();
418
419 AssertOkOrPrintErrorStatus(s);
420 gpr_log(GPR_INFO, "Response streaming done.");
421}
422
423void InteropClient::DoHalfDuplex() {
424 gpr_log(GPR_INFO, "Sending half-duplex streaming rpc ...");
425 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
426
427 ClientContext context;
428 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
429 StreamingOutputCallResponse>>
430 stream(stub->HalfDuplexCall(&context));
431
432 StreamingOutputCallRequest request;
433 ResponseParameters* response_parameter = request.add_response_parameters();
434 for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
435 response_parameter->set_size(response_stream_sizes[i]);
436 GPR_ASSERT(stream->Write(request));
437 }
438 stream->WritesDone();
439
440 unsigned int i = 0;
441 StreamingOutputCallResponse response;
442 while (stream->Read(&response)) {
443 GPR_ASSERT(response.payload().has_body());
444 GPR_ASSERT(response.payload().body() ==
445 grpc::string(response_stream_sizes[i], '\0'));
446 ++i;
447 }
448 GPR_ASSERT(response_stream_sizes.size() == i);
449 Status s = stream->Finish();
450 AssertOkOrPrintErrorStatus(s);
451 gpr_log(GPR_INFO, "Half-duplex streaming rpc done.");
452}
453
454void InteropClient::DoPingPong() {
455 gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ...");
456 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
457
458 ClientContext context;
459 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
460 StreamingOutputCallResponse>>
461 stream(stub->FullDuplexCall(&context));
462
463 StreamingOutputCallRequest request;
464 request.set_response_type(PayloadType::COMPRESSABLE);
465 ResponseParameters* response_parameter = request.add_response_parameters();
466 Payload* payload = request.mutable_payload();
467 StreamingOutputCallResponse response;
468 for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) {
469 response_parameter->set_size(response_stream_sizes[i]);
470 payload->set_body(grpc::string(request_stream_sizes[i], '\0'));
471 GPR_ASSERT(stream->Write(request));
472 GPR_ASSERT(stream->Read(&response));
473 GPR_ASSERT(response.payload().has_body());
474 GPR_ASSERT(response.payload().body() ==
475 grpc::string(response_stream_sizes[i], '\0'));
476 }
477
478 stream->WritesDone();
479 GPR_ASSERT(!stream->Read(&response));
480 Status s = stream->Finish();
481 AssertOkOrPrintErrorStatus(s);
482 gpr_log(GPR_INFO, "Ping pong streaming done.");
483}
484
Yang Gao68d61572015-04-24 14:42:42 -0700485void InteropClient::DoCancelAfterBegin() {
486 gpr_log(GPR_INFO, "Sending request steaming rpc ...");
487 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
488
489 ClientContext context;
490 StreamingInputCallRequest request;
491 StreamingInputCallResponse response;
492
493 std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream(
494 stub->StreamingInputCall(&context, &response));
495
496 gpr_log(GPR_INFO, "Trying to cancel...");
497 context.TryCancel();
498 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700499 GPR_ASSERT(s.error_code() == StatusCode::CANCELLED);
Yang Gao68d61572015-04-24 14:42:42 -0700500 gpr_log(GPR_INFO, "Canceling streaming done.");
501}
502
503void InteropClient::DoCancelAfterFirstResponse() {
504 gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ...");
505 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
506
507 ClientContext context;
508 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
509 StreamingOutputCallResponse>>
510 stream(stub->FullDuplexCall(&context));
511
512 StreamingOutputCallRequest request;
513 request.set_response_type(PayloadType::COMPRESSABLE);
514 ResponseParameters* response_parameter = request.add_response_parameters();
515 response_parameter->set_size(31415);
516 request.mutable_payload()->set_body(grpc::string(27182, '\0'));
517 StreamingOutputCallResponse response;
518 GPR_ASSERT(stream->Write(request));
519 GPR_ASSERT(stream->Read(&response));
520 GPR_ASSERT(response.payload().has_body());
521 GPR_ASSERT(response.payload().body() == grpc::string(31415, '\0'));
522 gpr_log(GPR_INFO, "Trying to cancel...");
523 context.TryCancel();
524
525 Status s = stream->Finish();
526 gpr_log(GPR_INFO, "Canceling pingpong streaming done.");
527}
528
yang-g69563b92015-07-10 15:32:11 -0700529void InteropClient::DoTimeoutOnSleepingServer() {
530 gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc with a short deadline...");
531 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
532
533 ClientContext context;
534 std::chrono::system_clock::time_point deadline =
535 std::chrono::system_clock::now() + std::chrono::milliseconds(1);
536 context.set_deadline(deadline);
537 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
538 StreamingOutputCallResponse>>
539 stream(stub->FullDuplexCall(&context));
540
541 StreamingOutputCallRequest request;
542 request.mutable_payload()->set_body(grpc::string(27182, '\0'));
543 stream->Write(request);
544
545 Status s = stream->Finish();
546 GPR_ASSERT(s.error_code() == StatusCode::DEADLINE_EXCEEDED);
547 gpr_log(GPR_INFO, "Pingpong streaming timeout done.");
548}
549
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700550void InteropClient::DoStatusWithMessage() {
551 gpr_log(GPR_INFO, "Sending RPC with a request for status code 2 and message");
552 std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
553
554 ClientContext context;
555 SimpleRequest request;
556 SimpleResponse response;
557 EchoStatus *requested_status = request.mutable_response_status();
558 requested_status->set_code(grpc::StatusCode::UNKNOWN);
559 grpc::string test_msg = "This is a test message";
560 requested_status->set_message(test_msg);
561
562 Status s = stub->UnaryCall(&context, request, &response);
563
564 GPR_ASSERT(s.error_code() == grpc::StatusCode::UNKNOWN);
565 GPR_ASSERT(s.error_message() == test_msg);
566 gpr_log(GPR_INFO, "Done testing Status and Message");
567}
568
Yang Gaoa4002072015-04-09 23:25:21 -0700569} // namespace testing
570} // namespace grpc