OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-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/interop/interop_client.h" |
| 35 |
| 36 #include <unistd.h> |
| 37 |
| 38 #include <fstream> |
| 39 #include <memory> |
| 40 |
| 41 #include <grpc++/channel.h> |
| 42 #include <grpc++/client_context.h> |
| 43 #include <grpc++/security/credentials.h> |
| 44 #include <grpc/grpc.h> |
| 45 #include <grpc/support/log.h> |
| 46 #include <grpc/support/string_util.h> |
| 47 #include <grpc/support/useful.h> |
| 48 |
| 49 #include "src/core/transport/byte_stream.h" |
| 50 #include "src/proto/grpc/testing/empty.grpc.pb.h" |
| 51 #include "src/proto/grpc/testing/test.grpc.pb.h" |
| 52 #include "src/proto/grpc/testing/messages.grpc.pb.h" |
| 53 #include "test/cpp/interop/client_helper.h" |
| 54 |
| 55 namespace grpc { |
| 56 namespace testing { |
| 57 |
| 58 static const char* kRandomFile = "test/cpp/interop/rnd.dat"; |
| 59 |
| 60 namespace { |
| 61 // The same value is defined by the Java client. |
| 62 const std::vector<int> request_stream_sizes = {27182, 8, 1828, 45904}; |
| 63 const std::vector<int> response_stream_sizes = {31415, 9, 2653, 58979}; |
| 64 const int kNumResponseMessages = 2000; |
| 65 const int kResponseMessageSize = 1030; |
| 66 const int kReceiveDelayMilliSeconds = 20; |
| 67 const int kLargeRequestSize = 271828; |
| 68 const int kLargeResponseSize = 314159; |
| 69 |
| 70 CompressionType GetInteropCompressionTypeFromCompressionAlgorithm( |
| 71 grpc_compression_algorithm algorithm) { |
| 72 switch (algorithm) { |
| 73 case GRPC_COMPRESS_NONE: |
| 74 return CompressionType::NONE; |
| 75 case GRPC_COMPRESS_GZIP: |
| 76 return CompressionType::GZIP; |
| 77 case GRPC_COMPRESS_DEFLATE: |
| 78 return CompressionType::DEFLATE; |
| 79 default: |
| 80 GPR_ASSERT(false); |
| 81 } |
| 82 } |
| 83 |
| 84 void NoopChecks(const InteropClientContextInspector& inspector, |
| 85 const SimpleRequest* request, const SimpleResponse* response) {} |
| 86 |
| 87 void CompressionChecks(const InteropClientContextInspector& inspector, |
| 88 const SimpleRequest* request, |
| 89 const SimpleResponse* response) { |
| 90 GPR_ASSERT(request->response_compression() == |
| 91 GetInteropCompressionTypeFromCompressionAlgorithm( |
| 92 inspector.GetCallCompressionAlgorithm())); |
| 93 if (request->response_compression() == NONE) { |
| 94 GPR_ASSERT(!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS)); |
| 95 } else if (request->response_type() == PayloadType::COMPRESSABLE) { |
| 96 // requested compression and compressable response => results should always |
| 97 // be compressed. |
| 98 GPR_ASSERT(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS); |
| 99 } |
| 100 } |
| 101 } // namespace |
| 102 |
| 103 InteropClient::ServiceStub::ServiceStub(std::shared_ptr<Channel> channel, |
| 104 bool new_stub_every_call) |
| 105 : channel_(channel), new_stub_every_call_(new_stub_every_call) { |
| 106 // If new_stub_every_call is false, then this is our chance to initialize |
| 107 // stub_. (see Get()) |
| 108 if (!new_stub_every_call) { |
| 109 stub_ = TestService::NewStub(channel); |
| 110 } |
| 111 } |
| 112 |
| 113 TestService::Stub* InteropClient::ServiceStub::Get() { |
| 114 if (new_stub_every_call_) { |
| 115 stub_ = TestService::NewStub(channel_); |
| 116 } |
| 117 |
| 118 return stub_.get(); |
| 119 } |
| 120 |
| 121 void InteropClient::ServiceStub::Reset(std::shared_ptr<Channel> channel) { |
| 122 channel_ = channel; |
| 123 |
| 124 // Update stub_ as well. Note: If new_stub_every_call_ is true, we can reset |
| 125 // the stub_ since the next call to Get() will create a new stub |
| 126 if (new_stub_every_call_) { |
| 127 stub_.reset(); |
| 128 } else { |
| 129 stub_ = TestService::NewStub(channel); |
| 130 } |
| 131 } |
| 132 |
| 133 void InteropClient::Reset(std::shared_ptr<Channel> channel) { |
| 134 serviceStub_.Reset(channel); |
| 135 } |
| 136 |
| 137 InteropClient::InteropClient(std::shared_ptr<Channel> channel) |
| 138 : serviceStub_(channel, true) {} |
| 139 |
| 140 InteropClient::InteropClient(std::shared_ptr<Channel> channel, |
| 141 bool new_stub_every_test_case) |
| 142 : serviceStub_(channel, new_stub_every_test_case) {} |
| 143 |
| 144 void InteropClient::AssertOkOrPrintErrorStatus(const Status& s) { |
| 145 if (s.ok()) { |
| 146 return; |
| 147 } |
| 148 gpr_log(GPR_ERROR, "Error status code: %d, message: %s", s.error_code(), |
| 149 s.error_message().c_str()); |
| 150 GPR_ASSERT(0); |
| 151 } |
| 152 |
| 153 void InteropClient::DoEmpty() { |
| 154 gpr_log(GPR_DEBUG, "Sending an empty rpc..."); |
| 155 |
| 156 Empty request = Empty::default_instance(); |
| 157 Empty response = Empty::default_instance(); |
| 158 ClientContext context; |
| 159 |
| 160 Status s = serviceStub_.Get()->EmptyCall(&context, request, &response); |
| 161 AssertOkOrPrintErrorStatus(s); |
| 162 |
| 163 gpr_log(GPR_DEBUG, "Empty rpc done."); |
| 164 } |
| 165 |
| 166 void InteropClient::PerformLargeUnary(SimpleRequest* request, |
| 167 SimpleResponse* response) { |
| 168 PerformLargeUnary(request, response, NoopChecks); |
| 169 } |
| 170 |
| 171 void InteropClient::PerformLargeUnary(SimpleRequest* request, |
| 172 SimpleResponse* response, |
| 173 CheckerFn custom_checks_fn) { |
| 174 ClientContext context; |
| 175 InteropClientContextInspector inspector(context); |
| 176 // If the request doesn't already specify the response type, default to |
| 177 // COMPRESSABLE. |
| 178 request->set_response_size(kLargeResponseSize); |
| 179 grpc::string payload(kLargeRequestSize, '\0'); |
| 180 request->mutable_payload()->set_body(payload.c_str(), kLargeRequestSize); |
| 181 |
| 182 Status s = serviceStub_.Get()->UnaryCall(&context, *request, response); |
| 183 AssertOkOrPrintErrorStatus(s); |
| 184 |
| 185 custom_checks_fn(inspector, request, response); |
| 186 |
| 187 // Payload related checks. |
| 188 if (request->response_type() != PayloadType::RANDOM) { |
| 189 GPR_ASSERT(response->payload().type() == request->response_type()); |
| 190 } |
| 191 switch (response->payload().type()) { |
| 192 case PayloadType::COMPRESSABLE: |
| 193 GPR_ASSERT(response->payload().body() == |
| 194 grpc::string(kLargeResponseSize, '\0')); |
| 195 break; |
| 196 case PayloadType::UNCOMPRESSABLE: { |
| 197 std::ifstream rnd_file(kRandomFile); |
| 198 GPR_ASSERT(rnd_file.good()); |
| 199 for (int i = 0; i < kLargeResponseSize; i++) { |
| 200 GPR_ASSERT(response->payload().body()[i] == (char)rnd_file.get()); |
| 201 } |
| 202 } break; |
| 203 default: |
| 204 GPR_ASSERT(false); |
| 205 } |
| 206 } |
| 207 |
| 208 void InteropClient::DoComputeEngineCreds( |
| 209 const grpc::string& default_service_account, |
| 210 const grpc::string& oauth_scope) { |
| 211 gpr_log(GPR_DEBUG, |
| 212 "Sending a large unary rpc with compute engine credentials ..."); |
| 213 SimpleRequest request; |
| 214 SimpleResponse response; |
| 215 request.set_fill_username(true); |
| 216 request.set_fill_oauth_scope(true); |
| 217 request.set_response_type(PayloadType::COMPRESSABLE); |
| 218 PerformLargeUnary(&request, &response); |
| 219 gpr_log(GPR_DEBUG, "Got username %s", response.username().c_str()); |
| 220 gpr_log(GPR_DEBUG, "Got oauth_scope %s", response.oauth_scope().c_str()); |
| 221 GPR_ASSERT(!response.username().empty()); |
| 222 GPR_ASSERT(response.username().c_str() == default_service_account); |
| 223 GPR_ASSERT(!response.oauth_scope().empty()); |
| 224 const char* oauth_scope_str = response.oauth_scope().c_str(); |
| 225 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos); |
| 226 gpr_log(GPR_DEBUG, "Large unary with compute engine creds done."); |
| 227 } |
| 228 |
| 229 void InteropClient::DoOauth2AuthToken(const grpc::string& username, |
| 230 const grpc::string& oauth_scope) { |
| 231 gpr_log(GPR_DEBUG, |
| 232 "Sending a unary rpc with raw oauth2 access token credentials ..."); |
| 233 SimpleRequest request; |
| 234 SimpleResponse response; |
| 235 request.set_fill_username(true); |
| 236 request.set_fill_oauth_scope(true); |
| 237 |
| 238 ClientContext context; |
| 239 |
| 240 Status s = serviceStub_.Get()->UnaryCall(&context, request, &response); |
| 241 |
| 242 AssertOkOrPrintErrorStatus(s); |
| 243 GPR_ASSERT(!response.username().empty()); |
| 244 GPR_ASSERT(!response.oauth_scope().empty()); |
| 245 GPR_ASSERT(username == response.username()); |
| 246 const char* oauth_scope_str = response.oauth_scope().c_str(); |
| 247 GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos); |
| 248 gpr_log(GPR_DEBUG, "Unary with oauth2 access token credentials done."); |
| 249 } |
| 250 |
| 251 void InteropClient::DoPerRpcCreds(const grpc::string& json_key) { |
| 252 gpr_log(GPR_DEBUG, "Sending a unary rpc with per-rpc JWT access token ..."); |
| 253 SimpleRequest request; |
| 254 SimpleResponse response; |
| 255 request.set_fill_username(true); |
| 256 |
| 257 ClientContext context; |
| 258 std::chrono::seconds token_lifetime = std::chrono::hours(1); |
| 259 std::shared_ptr<CallCredentials> creds = |
| 260 ServiceAccountJWTAccessCredentials(json_key, token_lifetime.count()); |
| 261 |
| 262 context.set_credentials(creds); |
| 263 |
| 264 Status s = serviceStub_.Get()->UnaryCall(&context, request, &response); |
| 265 |
| 266 AssertOkOrPrintErrorStatus(s); |
| 267 GPR_ASSERT(!response.username().empty()); |
| 268 GPR_ASSERT(json_key.find(response.username()) != grpc::string::npos); |
| 269 gpr_log(GPR_DEBUG, "Unary with per-rpc JWT access token done."); |
| 270 } |
| 271 |
| 272 void InteropClient::DoJwtTokenCreds(const grpc::string& username) { |
| 273 gpr_log(GPR_DEBUG, |
| 274 "Sending a large unary rpc with JWT token credentials ..."); |
| 275 SimpleRequest request; |
| 276 SimpleResponse response; |
| 277 request.set_fill_username(true); |
| 278 request.set_response_type(PayloadType::COMPRESSABLE); |
| 279 PerformLargeUnary(&request, &response); |
| 280 GPR_ASSERT(!response.username().empty()); |
| 281 GPR_ASSERT(username.find(response.username()) != grpc::string::npos); |
| 282 gpr_log(GPR_DEBUG, "Large unary with JWT token creds done."); |
| 283 } |
| 284 |
| 285 void InteropClient::DoLargeUnary() { |
| 286 gpr_log(GPR_DEBUG, "Sending a large unary rpc..."); |
| 287 SimpleRequest request; |
| 288 SimpleResponse response; |
| 289 request.set_response_type(PayloadType::COMPRESSABLE); |
| 290 PerformLargeUnary(&request, &response); |
| 291 gpr_log(GPR_DEBUG, "Large unary done."); |
| 292 } |
| 293 |
| 294 void InteropClient::DoLargeCompressedUnary() { |
| 295 const CompressionType compression_types[] = {NONE, GZIP, DEFLATE}; |
| 296 const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM}; |
| 297 for (size_t i = 0; i < GPR_ARRAY_SIZE(payload_types); i++) { |
| 298 for (size_t j = 0; j < GPR_ARRAY_SIZE(compression_types); j++) { |
| 299 char* log_suffix; |
| 300 gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)", |
| 301 CompressionType_Name(compression_types[j]).c_str(), |
| 302 PayloadType_Name(payload_types[i]).c_str()); |
| 303 |
| 304 gpr_log(GPR_DEBUG, "Sending a large compressed unary rpc %s.", |
| 305 log_suffix); |
| 306 SimpleRequest request; |
| 307 SimpleResponse response; |
| 308 request.set_response_type(payload_types[i]); |
| 309 request.set_response_compression(compression_types[j]); |
| 310 PerformLargeUnary(&request, &response, CompressionChecks); |
| 311 gpr_log(GPR_DEBUG, "Large compressed unary done %s.", log_suffix); |
| 312 gpr_free(log_suffix); |
| 313 } |
| 314 } |
| 315 } |
| 316 |
| 317 void InteropClient::DoRequestStreaming() { |
| 318 gpr_log(GPR_DEBUG, "Sending request steaming rpc ..."); |
| 319 |
| 320 ClientContext context; |
| 321 StreamingInputCallRequest request; |
| 322 StreamingInputCallResponse response; |
| 323 |
| 324 std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream( |
| 325 serviceStub_.Get()->StreamingInputCall(&context, &response)); |
| 326 |
| 327 int aggregated_payload_size = 0; |
| 328 for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) { |
| 329 Payload* payload = request.mutable_payload(); |
| 330 payload->set_body(grpc::string(request_stream_sizes[i], '\0')); |
| 331 GPR_ASSERT(stream->Write(request)); |
| 332 aggregated_payload_size += request_stream_sizes[i]; |
| 333 } |
| 334 stream->WritesDone(); |
| 335 Status s = stream->Finish(); |
| 336 |
| 337 GPR_ASSERT(response.aggregated_payload_size() == aggregated_payload_size); |
| 338 AssertOkOrPrintErrorStatus(s); |
| 339 gpr_log(GPR_DEBUG, "Request streaming done."); |
| 340 } |
| 341 |
| 342 void InteropClient::DoResponseStreaming() { |
| 343 gpr_log(GPR_DEBUG, "Receiving response steaming rpc ..."); |
| 344 |
| 345 ClientContext context; |
| 346 StreamingOutputCallRequest request; |
| 347 for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) { |
| 348 ResponseParameters* response_parameter = request.add_response_parameters(); |
| 349 response_parameter->set_size(response_stream_sizes[i]); |
| 350 } |
| 351 StreamingOutputCallResponse response; |
| 352 std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream( |
| 353 serviceStub_.Get()->StreamingOutputCall(&context, request)); |
| 354 |
| 355 unsigned int i = 0; |
| 356 while (stream->Read(&response)) { |
| 357 GPR_ASSERT(response.payload().body() == |
| 358 grpc::string(response_stream_sizes[i], '\0')); |
| 359 ++i; |
| 360 } |
| 361 GPR_ASSERT(response_stream_sizes.size() == i); |
| 362 Status s = stream->Finish(); |
| 363 AssertOkOrPrintErrorStatus(s); |
| 364 gpr_log(GPR_DEBUG, "Response streaming done."); |
| 365 } |
| 366 |
| 367 void InteropClient::DoResponseCompressedStreaming() { |
| 368 const CompressionType compression_types[] = {NONE, GZIP, DEFLATE}; |
| 369 const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM}; |
| 370 for (size_t i = 0; i < GPR_ARRAY_SIZE(payload_types); i++) { |
| 371 for (size_t j = 0; j < GPR_ARRAY_SIZE(compression_types); j++) { |
| 372 ClientContext context; |
| 373 InteropClientContextInspector inspector(context); |
| 374 StreamingOutputCallRequest request; |
| 375 |
| 376 char* log_suffix; |
| 377 gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)", |
| 378 CompressionType_Name(compression_types[j]).c_str(), |
| 379 PayloadType_Name(payload_types[i]).c_str()); |
| 380 |
| 381 gpr_log(GPR_DEBUG, "Receiving response steaming rpc %s.", log_suffix); |
| 382 |
| 383 request.set_response_type(payload_types[i]); |
| 384 request.set_response_compression(compression_types[j]); |
| 385 |
| 386 for (size_t k = 0; k < response_stream_sizes.size(); ++k) { |
| 387 ResponseParameters* response_parameter = |
| 388 request.add_response_parameters(); |
| 389 response_parameter->set_size(response_stream_sizes[k]); |
| 390 } |
| 391 StreamingOutputCallResponse response; |
| 392 |
| 393 std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream( |
| 394 serviceStub_.Get()->StreamingOutputCall(&context, request)); |
| 395 |
| 396 size_t k = 0; |
| 397 while (stream->Read(&response)) { |
| 398 // Payload related checks. |
| 399 if (request.response_type() != PayloadType::RANDOM) { |
| 400 GPR_ASSERT(response.payload().type() == request.response_type()); |
| 401 } |
| 402 switch (response.payload().type()) { |
| 403 case PayloadType::COMPRESSABLE: |
| 404 GPR_ASSERT(response.payload().body() == |
| 405 grpc::string(response_stream_sizes[k], '\0')); |
| 406 break; |
| 407 case PayloadType::UNCOMPRESSABLE: { |
| 408 std::ifstream rnd_file(kRandomFile); |
| 409 GPR_ASSERT(rnd_file.good()); |
| 410 for (int n = 0; n < response_stream_sizes[k]; n++) { |
| 411 GPR_ASSERT(response.payload().body()[n] == (char)rnd_file.get()); |
| 412 } |
| 413 } break; |
| 414 default: |
| 415 GPR_ASSERT(false); |
| 416 } |
| 417 |
| 418 // Compression related checks. |
| 419 GPR_ASSERT(request.response_compression() == |
| 420 GetInteropCompressionTypeFromCompressionAlgorithm( |
| 421 inspector.GetCallCompressionAlgorithm())); |
| 422 if (request.response_compression() == NONE) { |
| 423 GPR_ASSERT( |
| 424 !(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS)); |
| 425 } else if (request.response_type() == PayloadType::COMPRESSABLE) { |
| 426 // requested compression and compressable response => results should |
| 427 // always be compressed. |
| 428 GPR_ASSERT(inspector.GetMessageFlags() & |
| 429 GRPC_WRITE_INTERNAL_COMPRESS); |
| 430 } |
| 431 |
| 432 ++k; |
| 433 } |
| 434 |
| 435 GPR_ASSERT(response_stream_sizes.size() == k); |
| 436 Status s = stream->Finish(); |
| 437 |
| 438 AssertOkOrPrintErrorStatus(s); |
| 439 gpr_log(GPR_DEBUG, "Response streaming done %s.", log_suffix); |
| 440 gpr_free(log_suffix); |
| 441 } |
| 442 } |
| 443 } |
| 444 |
| 445 void InteropClient::DoResponseStreamingWithSlowConsumer() { |
| 446 gpr_log(GPR_DEBUG, "Receiving response steaming rpc with slow consumer ..."); |
| 447 |
| 448 ClientContext context; |
| 449 StreamingOutputCallRequest request; |
| 450 |
| 451 for (int i = 0; i < kNumResponseMessages; ++i) { |
| 452 ResponseParameters* response_parameter = request.add_response_parameters(); |
| 453 response_parameter->set_size(kResponseMessageSize); |
| 454 } |
| 455 StreamingOutputCallResponse response; |
| 456 std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream( |
| 457 serviceStub_.Get()->StreamingOutputCall(&context, request)); |
| 458 |
| 459 int i = 0; |
| 460 while (stream->Read(&response)) { |
| 461 GPR_ASSERT(response.payload().body() == |
| 462 grpc::string(kResponseMessageSize, '\0')); |
| 463 gpr_log(GPR_DEBUG, "received message %d", i); |
| 464 usleep(kReceiveDelayMilliSeconds * 1000); |
| 465 ++i; |
| 466 } |
| 467 GPR_ASSERT(kNumResponseMessages == i); |
| 468 Status s = stream->Finish(); |
| 469 |
| 470 AssertOkOrPrintErrorStatus(s); |
| 471 gpr_log(GPR_DEBUG, "Response streaming done."); |
| 472 } |
| 473 |
| 474 void InteropClient::DoHalfDuplex() { |
| 475 gpr_log(GPR_DEBUG, "Sending half-duplex streaming rpc ..."); |
| 476 |
| 477 ClientContext context; |
| 478 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 479 StreamingOutputCallResponse>> |
| 480 stream(serviceStub_.Get()->HalfDuplexCall(&context)); |
| 481 |
| 482 StreamingOutputCallRequest request; |
| 483 ResponseParameters* response_parameter = request.add_response_parameters(); |
| 484 for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) { |
| 485 response_parameter->set_size(response_stream_sizes[i]); |
| 486 GPR_ASSERT(stream->Write(request)); |
| 487 } |
| 488 stream->WritesDone(); |
| 489 |
| 490 unsigned int i = 0; |
| 491 StreamingOutputCallResponse response; |
| 492 while (stream->Read(&response)) { |
| 493 GPR_ASSERT(response.payload().body() == |
| 494 grpc::string(response_stream_sizes[i], '\0')); |
| 495 ++i; |
| 496 } |
| 497 GPR_ASSERT(response_stream_sizes.size() == i); |
| 498 Status s = stream->Finish(); |
| 499 AssertOkOrPrintErrorStatus(s); |
| 500 gpr_log(GPR_DEBUG, "Half-duplex streaming rpc done."); |
| 501 } |
| 502 |
| 503 void InteropClient::DoPingPong() { |
| 504 gpr_log(GPR_DEBUG, "Sending Ping Pong streaming rpc ..."); |
| 505 |
| 506 ClientContext context; |
| 507 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 508 StreamingOutputCallResponse>> |
| 509 stream(serviceStub_.Get()->FullDuplexCall(&context)); |
| 510 |
| 511 StreamingOutputCallRequest request; |
| 512 request.set_response_type(PayloadType::COMPRESSABLE); |
| 513 ResponseParameters* response_parameter = request.add_response_parameters(); |
| 514 Payload* payload = request.mutable_payload(); |
| 515 StreamingOutputCallResponse response; |
| 516 for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) { |
| 517 response_parameter->set_size(response_stream_sizes[i]); |
| 518 payload->set_body(grpc::string(request_stream_sizes[i], '\0')); |
| 519 GPR_ASSERT(stream->Write(request)); |
| 520 GPR_ASSERT(stream->Read(&response)); |
| 521 GPR_ASSERT(response.payload().body() == |
| 522 grpc::string(response_stream_sizes[i], '\0')); |
| 523 } |
| 524 |
| 525 stream->WritesDone(); |
| 526 GPR_ASSERT(!stream->Read(&response)); |
| 527 Status s = stream->Finish(); |
| 528 AssertOkOrPrintErrorStatus(s); |
| 529 gpr_log(GPR_DEBUG, "Ping pong streaming done."); |
| 530 } |
| 531 |
| 532 void InteropClient::DoCancelAfterBegin() { |
| 533 gpr_log(GPR_DEBUG, "Sending request steaming rpc ..."); |
| 534 |
| 535 ClientContext context; |
| 536 StreamingInputCallRequest request; |
| 537 StreamingInputCallResponse response; |
| 538 |
| 539 std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream( |
| 540 serviceStub_.Get()->StreamingInputCall(&context, &response)); |
| 541 |
| 542 gpr_log(GPR_DEBUG, "Trying to cancel..."); |
| 543 context.TryCancel(); |
| 544 Status s = stream->Finish(); |
| 545 GPR_ASSERT(s.error_code() == StatusCode::CANCELLED); |
| 546 gpr_log(GPR_DEBUG, "Canceling streaming done."); |
| 547 } |
| 548 |
| 549 void InteropClient::DoCancelAfterFirstResponse() { |
| 550 gpr_log(GPR_DEBUG, "Sending Ping Pong streaming rpc ..."); |
| 551 |
| 552 ClientContext context; |
| 553 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 554 StreamingOutputCallResponse>> |
| 555 stream(serviceStub_.Get()->FullDuplexCall(&context)); |
| 556 |
| 557 StreamingOutputCallRequest request; |
| 558 request.set_response_type(PayloadType::COMPRESSABLE); |
| 559 ResponseParameters* response_parameter = request.add_response_parameters(); |
| 560 response_parameter->set_size(31415); |
| 561 request.mutable_payload()->set_body(grpc::string(27182, '\0')); |
| 562 StreamingOutputCallResponse response; |
| 563 GPR_ASSERT(stream->Write(request)); |
| 564 GPR_ASSERT(stream->Read(&response)); |
| 565 GPR_ASSERT(response.payload().body() == grpc::string(31415, '\0')); |
| 566 gpr_log(GPR_DEBUG, "Trying to cancel..."); |
| 567 context.TryCancel(); |
| 568 |
| 569 Status s = stream->Finish(); |
| 570 gpr_log(GPR_DEBUG, "Canceling pingpong streaming done."); |
| 571 } |
| 572 |
| 573 void InteropClient::DoTimeoutOnSleepingServer() { |
| 574 gpr_log(GPR_DEBUG, |
| 575 "Sending Ping Pong streaming rpc with a short deadline..."); |
| 576 |
| 577 ClientContext context; |
| 578 std::chrono::system_clock::time_point deadline = |
| 579 std::chrono::system_clock::now() + std::chrono::milliseconds(1); |
| 580 context.set_deadline(deadline); |
| 581 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 582 StreamingOutputCallResponse>> |
| 583 stream(serviceStub_.Get()->FullDuplexCall(&context)); |
| 584 |
| 585 StreamingOutputCallRequest request; |
| 586 request.mutable_payload()->set_body(grpc::string(27182, '\0')); |
| 587 stream->Write(request); |
| 588 |
| 589 Status s = stream->Finish(); |
| 590 GPR_ASSERT(s.error_code() == StatusCode::DEADLINE_EXCEEDED); |
| 591 gpr_log(GPR_DEBUG, "Pingpong streaming timeout done."); |
| 592 } |
| 593 |
| 594 void InteropClient::DoEmptyStream() { |
| 595 gpr_log(GPR_DEBUG, "Starting empty_stream."); |
| 596 |
| 597 ClientContext context; |
| 598 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 599 StreamingOutputCallResponse>> |
| 600 stream(serviceStub_.Get()->FullDuplexCall(&context)); |
| 601 stream->WritesDone(); |
| 602 StreamingOutputCallResponse response; |
| 603 GPR_ASSERT(stream->Read(&response) == false); |
| 604 Status s = stream->Finish(); |
| 605 AssertOkOrPrintErrorStatus(s); |
| 606 gpr_log(GPR_DEBUG, "empty_stream done."); |
| 607 } |
| 608 |
| 609 void InteropClient::DoStatusWithMessage() { |
| 610 gpr_log(GPR_DEBUG, |
| 611 "Sending RPC with a request for status code 2 and message"); |
| 612 |
| 613 ClientContext context; |
| 614 SimpleRequest request; |
| 615 SimpleResponse response; |
| 616 EchoStatus* requested_status = request.mutable_response_status(); |
| 617 requested_status->set_code(grpc::StatusCode::UNKNOWN); |
| 618 grpc::string test_msg = "This is a test message"; |
| 619 requested_status->set_message(test_msg); |
| 620 |
| 621 Status s = serviceStub_.Get()->UnaryCall(&context, request, &response); |
| 622 |
| 623 GPR_ASSERT(s.error_code() == grpc::StatusCode::UNKNOWN); |
| 624 GPR_ASSERT(s.error_message() == test_msg); |
| 625 gpr_log(GPR_DEBUG, "Done testing Status and Message"); |
| 626 } |
| 627 |
| 628 void InteropClient::DoCustomMetadata() { |
| 629 const grpc::string kEchoInitialMetadataKey("x-grpc-test-echo-initial"); |
| 630 const grpc::string kInitialMetadataValue("test_initial_metadata_value"); |
| 631 const grpc::string kEchoTrailingBinMetadataKey( |
| 632 "x-grpc-test-echo-trailing-bin"); |
| 633 const grpc::string kTrailingBinValue("\x0a\x0b\x0a\x0b\x0a\x0b"); |
| 634 ; |
| 635 |
| 636 { |
| 637 gpr_log(GPR_DEBUG, "Sending RPC with custom metadata"); |
| 638 ClientContext context; |
| 639 context.AddMetadata(kEchoInitialMetadataKey, kInitialMetadataValue); |
| 640 context.AddMetadata(kEchoTrailingBinMetadataKey, kTrailingBinValue); |
| 641 SimpleRequest request; |
| 642 SimpleResponse response; |
| 643 request.set_response_size(kLargeResponseSize); |
| 644 grpc::string payload(kLargeRequestSize, '\0'); |
| 645 request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize); |
| 646 |
| 647 Status s = serviceStub_.Get()->UnaryCall(&context, request, &response); |
| 648 AssertOkOrPrintErrorStatus(s); |
| 649 const auto& server_initial_metadata = context.GetServerInitialMetadata(); |
| 650 auto iter = server_initial_metadata.find(kEchoInitialMetadataKey); |
| 651 GPR_ASSERT(iter != server_initial_metadata.end()); |
| 652 GPR_ASSERT(iter->second.data() == kInitialMetadataValue); |
| 653 const auto& server_trailing_metadata = context.GetServerTrailingMetadata(); |
| 654 iter = server_trailing_metadata.find(kEchoTrailingBinMetadataKey); |
| 655 GPR_ASSERT(iter != server_trailing_metadata.end()); |
| 656 GPR_ASSERT(grpc::string(iter->second.begin(), iter->second.end()) == |
| 657 kTrailingBinValue); |
| 658 |
| 659 gpr_log(GPR_DEBUG, "Done testing RPC with custom metadata"); |
| 660 } |
| 661 |
| 662 { |
| 663 gpr_log(GPR_DEBUG, "Sending stream with custom metadata"); |
| 664 ClientContext context; |
| 665 context.AddMetadata(kEchoInitialMetadataKey, kInitialMetadataValue); |
| 666 context.AddMetadata(kEchoTrailingBinMetadataKey, kTrailingBinValue); |
| 667 std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 668 StreamingOutputCallResponse>> |
| 669 stream(serviceStub_.Get()->FullDuplexCall(&context)); |
| 670 |
| 671 StreamingOutputCallRequest request; |
| 672 request.set_response_type(PayloadType::COMPRESSABLE); |
| 673 ResponseParameters* response_parameter = request.add_response_parameters(); |
| 674 response_parameter->set_size(kLargeResponseSize); |
| 675 grpc::string payload(kLargeRequestSize, '\0'); |
| 676 request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize); |
| 677 StreamingOutputCallResponse response; |
| 678 GPR_ASSERT(stream->Write(request)); |
| 679 stream->WritesDone(); |
| 680 GPR_ASSERT(stream->Read(&response)); |
| 681 GPR_ASSERT(response.payload().body() == |
| 682 grpc::string(kLargeResponseSize, '\0')); |
| 683 GPR_ASSERT(!stream->Read(&response)); |
| 684 Status s = stream->Finish(); |
| 685 AssertOkOrPrintErrorStatus(s); |
| 686 const auto& server_initial_metadata = context.GetServerInitialMetadata(); |
| 687 auto iter = server_initial_metadata.find(kEchoInitialMetadataKey); |
| 688 GPR_ASSERT(iter != server_initial_metadata.end()); |
| 689 GPR_ASSERT(iter->second.data() == kInitialMetadataValue); |
| 690 const auto& server_trailing_metadata = context.GetServerTrailingMetadata(); |
| 691 iter = server_trailing_metadata.find(kEchoTrailingBinMetadataKey); |
| 692 GPR_ASSERT(iter != server_trailing_metadata.end()); |
| 693 GPR_ASSERT(grpc::string(iter->second.begin(), iter->second.end()) == |
| 694 kTrailingBinValue); |
| 695 |
| 696 gpr_log(GPR_DEBUG, "Done testing stream with custom metadata"); |
| 697 } |
| 698 } |
| 699 |
| 700 } // namespace testing |
| 701 } // namespace grpc |
OLD | NEW |