Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 36 | #include <unistd.h> |
| 37 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 38 | #include <fstream> |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 39 | #include <memory> |
| 40 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 41 | #include <grpc/grpc.h> |
| 42 | #include <grpc/support/log.h> |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 43 | #include <grpc/support/string_util.h> |
David Garcia Quintas | 2a6427f | 2015-08-18 11:11:40 -0700 | [diff] [blame] | 44 | #include <grpc/support/useful.h> |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 45 | #include <grpc++/channel.h> |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 46 | #include <grpc++/client_context.h> |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 47 | #include <grpc++/security/credentials.h> |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 48 | |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 49 | #include "src/core/transport/stream_op.h" |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 50 | #include "test/cpp/interop/client_helper.h" |
Abhishek Kumar | 1b3e3cd | 2015-04-16 20:10:29 -0700 | [diff] [blame] | 51 | #include "test/proto/test.grpc.pb.h" |
Abhishek Kumar | 60572d4 | 2015-04-16 20:45:25 -0700 | [diff] [blame] | 52 | #include "test/proto/empty.grpc.pb.h" |
| 53 | #include "test/proto/messages.grpc.pb.h" |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 54 | |
| 55 | namespace grpc { |
| 56 | namespace testing { |
| 57 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 58 | static const char* kRandomFile = "test/cpp/interop/rnd.dat"; |
| 59 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 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; |
Xudong Ma | a5861f3 | 2015-05-26 15:24:11 -0700 | [diff] [blame] | 67 | const int kLargeRequestSize = 271828; |
| 68 | const int kLargeResponseSize = 314159; |
David Garcia Quintas | 81491b6 | 2015-08-18 16:46:53 -0700 | [diff] [blame] | 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 | } |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 83 | } // namespace |
| 84 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 85 | InteropClient::InteropClient(std::shared_ptr<Channel> channel) |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 86 | : channel_(channel), stub_(TestService::NewStub(channel)) {} |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 87 | |
| 88 | void InteropClient::AssertOkOrPrintErrorStatus(const Status& s) { |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 89 | if (s.ok()) { |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 90 | return; |
| 91 | } |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 92 | gpr_log(GPR_INFO, "Error status code: %d, message: %s", s.error_code(), |
| 93 | s.error_message().c_str()); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 94 | GPR_ASSERT(0); |
| 95 | } |
| 96 | |
| 97 | void InteropClient::DoEmpty() { |
| 98 | gpr_log(GPR_INFO, "Sending an empty rpc..."); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 99 | |
| 100 | Empty request = Empty::default_instance(); |
| 101 | Empty response = Empty::default_instance(); |
| 102 | ClientContext context; |
| 103 | |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 104 | Status s = stub_->EmptyCall(&context, request, &response); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 105 | AssertOkOrPrintErrorStatus(s); |
| 106 | |
| 107 | gpr_log(GPR_INFO, "Empty rpc done."); |
| 108 | } |
| 109 | |
| 110 | // Shared code to set large payload, make rpc and check response payload. |
| 111 | void InteropClient::PerformLargeUnary(SimpleRequest* request, |
| 112 | SimpleResponse* response) { |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 113 | |
| 114 | ClientContext context; |
David Garcia Quintas | cd37d58 | 2015-08-09 15:50:21 -0700 | [diff] [blame] | 115 | InteropClientContextInspector inspector(context); |
David Garcia Quintas | 93dfab9 | 2015-08-13 11:29:50 -0700 | [diff] [blame] | 116 | // If the request doesn't already specify the response type, default to |
| 117 | // COMPRESSABLE. |
David Garcia Quintas | cd37d58 | 2015-08-09 15:50:21 -0700 | [diff] [blame] | 118 | request->set_response_size(kLargeResponseSize); |
| 119 | grpc::string payload(kLargeRequestSize, '\0'); |
| 120 | request->mutable_payload()->set_body(payload.c_str(), kLargeRequestSize); |
| 121 | |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 122 | Status s = stub_->UnaryCall(&context, *request, response); |
David Garcia Quintas | cd37d58 | 2015-08-09 15:50:21 -0700 | [diff] [blame] | 123 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 124 | // Compression related checks. |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 125 | GPR_ASSERT(request->response_compression() == |
| 126 | GetInteropCompressionTypeFromCompressionAlgorithm( |
| 127 | inspector.GetCallCompressionAlgorithm())); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 128 | if (request->response_compression() == NONE) { |
| 129 | GPR_ASSERT(!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS)); |
| 130 | } else if (request->response_type() == PayloadType::COMPRESSABLE) { |
| 131 | // requested compression and compressable response => results should always |
| 132 | // be compressed. |
| 133 | GPR_ASSERT(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS); |
| 134 | } |
| 135 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 136 | AssertOkOrPrintErrorStatus(s); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 137 | |
| 138 | // Payload related checks. |
| 139 | if (request->response_type() != PayloadType::RANDOM) { |
| 140 | GPR_ASSERT(response->payload().type() == request->response_type()); |
| 141 | } |
| 142 | switch (response->payload().type()) { |
| 143 | case PayloadType::COMPRESSABLE: |
| 144 | GPR_ASSERT(response->payload().body() == |
| 145 | grpc::string(kLargeResponseSize, '\0')); |
| 146 | break; |
| 147 | case PayloadType::UNCOMPRESSABLE: { |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 148 | std::ifstream rnd_file(kRandomFile); |
| 149 | GPR_ASSERT(rnd_file.good()); |
| 150 | for (int i = 0; i < kLargeResponseSize; i++) { |
| 151 | GPR_ASSERT(response->payload().body()[i] == (char)rnd_file.get()); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 152 | } |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 153 | } break; |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 154 | default: |
| 155 | GPR_ASSERT(false); |
| 156 | } |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void InteropClient::DoComputeEngineCreds( |
| 160 | const grpc::string& default_service_account, |
| 161 | const grpc::string& oauth_scope) { |
| 162 | gpr_log(GPR_INFO, |
| 163 | "Sending a large unary rpc with compute engine credentials ..."); |
| 164 | SimpleRequest request; |
| 165 | SimpleResponse response; |
| 166 | request.set_fill_username(true); |
| 167 | request.set_fill_oauth_scope(true); |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 168 | request.set_response_type(PayloadType::COMPRESSABLE); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 169 | PerformLargeUnary(&request, &response); |
| 170 | gpr_log(GPR_INFO, "Got username %s", response.username().c_str()); |
| 171 | gpr_log(GPR_INFO, "Got oauth_scope %s", response.oauth_scope().c_str()); |
| 172 | GPR_ASSERT(!response.username().empty()); |
| 173 | GPR_ASSERT(response.username().c_str() == default_service_account); |
| 174 | GPR_ASSERT(!response.oauth_scope().empty()); |
| 175 | const char* oauth_scope_str = response.oauth_scope().c_str(); |
| 176 | GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos); |
| 177 | gpr_log(GPR_INFO, "Large unary with compute engine creds done."); |
| 178 | } |
| 179 | |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 180 | void InteropClient::DoOauth2AuthToken(const grpc::string& username, |
| 181 | const grpc::string& oauth_scope) { |
| 182 | gpr_log(GPR_INFO, |
yang-g | 463cde7 | 2015-07-17 15:21:39 -0700 | [diff] [blame] | 183 | "Sending a unary rpc with raw oauth2 access token credentials ..."); |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 184 | SimpleRequest request; |
| 185 | SimpleResponse response; |
| 186 | request.set_fill_username(true); |
| 187 | request.set_fill_oauth_scope(true); |
yang-g | 463cde7 | 2015-07-17 15:21:39 -0700 | [diff] [blame] | 188 | |
| 189 | ClientContext context; |
| 190 | |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 191 | Status s = stub_->UnaryCall(&context, request, &response); |
yang-g | 463cde7 | 2015-07-17 15:21:39 -0700 | [diff] [blame] | 192 | |
| 193 | AssertOkOrPrintErrorStatus(s); |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 194 | GPR_ASSERT(!response.username().empty()); |
| 195 | GPR_ASSERT(!response.oauth_scope().empty()); |
yang-g | 867d0c1 | 2015-09-02 14:33:15 -0700 | [diff] [blame] | 196 | GPR_ASSERT(username == response.username()); |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 197 | const char* oauth_scope_str = response.oauth_scope().c_str(); |
| 198 | GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos); |
yang-g | 463cde7 | 2015-07-17 15:21:39 -0700 | [diff] [blame] | 199 | gpr_log(GPR_INFO, "Unary with oauth2 access token credentials done."); |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 200 | } |
| 201 | |
yang-g | 867d0c1 | 2015-09-02 14:33:15 -0700 | [diff] [blame] | 202 | void InteropClient::DoPerRpcCreds(const grpc::string& json_key) { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 203 | gpr_log(GPR_INFO, "Sending a unary rpc with per-rpc JWT access token ..."); |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 204 | SimpleRequest request; |
| 205 | SimpleResponse response; |
| 206 | request.set_fill_username(true); |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 207 | |
| 208 | ClientContext context; |
yang-g | 867d0c1 | 2015-09-02 14:33:15 -0700 | [diff] [blame] | 209 | std::chrono::seconds token_lifetime = std::chrono::hours(1); |
| 210 | std::shared_ptr<Credentials> creds = |
| 211 | ServiceAccountJWTAccessCredentials(json_key, token_lifetime.count()); |
| 212 | |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 213 | context.set_credentials(creds); |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 214 | |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 215 | Status s = stub_->UnaryCall(&context, request, &response); |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 216 | |
| 217 | AssertOkOrPrintErrorStatus(s); |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 218 | GPR_ASSERT(!response.username().empty()); |
yang-g | 867d0c1 | 2015-09-02 14:33:15 -0700 | [diff] [blame] | 219 | GPR_ASSERT(json_key.find(response.username()) != grpc::string::npos); |
yang-g | 201ef59 | 2015-09-02 15:55:10 -0700 | [diff] [blame] | 220 | gpr_log(GPR_INFO, "Unary with per-rpc JWT access token done."); |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 223 | void InteropClient::DoJwtTokenCreds(const grpc::string& username) { |
| 224 | gpr_log(GPR_INFO, "Sending a large unary rpc with JWT token credentials ..."); |
| 225 | SimpleRequest request; |
| 226 | SimpleResponse response; |
| 227 | request.set_fill_username(true); |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 228 | request.set_response_type(PayloadType::COMPRESSABLE); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 229 | PerformLargeUnary(&request, &response); |
| 230 | GPR_ASSERT(!response.username().empty()); |
| 231 | GPR_ASSERT(username.find(response.username()) != grpc::string::npos); |
| 232 | gpr_log(GPR_INFO, "Large unary with JWT token creds done."); |
| 233 | } |
| 234 | |
| 235 | void InteropClient::DoLargeUnary() { |
David Garcia Quintas | cd37d58 | 2015-08-09 15:50:21 -0700 | [diff] [blame] | 236 | gpr_log(GPR_INFO, "Sending a large unary rpc..."); |
| 237 | SimpleRequest request; |
| 238 | SimpleResponse response; |
David Garcia Quintas | 93dfab9 | 2015-08-13 11:29:50 -0700 | [diff] [blame] | 239 | request.set_response_type(PayloadType::COMPRESSABLE); |
David Garcia Quintas | cd37d58 | 2015-08-09 15:50:21 -0700 | [diff] [blame] | 240 | PerformLargeUnary(&request, &response); |
| 241 | gpr_log(GPR_INFO, "Large unary done."); |
| 242 | } |
| 243 | |
| 244 | void InteropClient::DoLargeCompressedUnary() { |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 245 | const CompressionType compression_types[] = {NONE, GZIP, DEFLATE}; |
| 246 | const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM}; |
David Garcia Quintas | 2a6427f | 2015-08-18 11:11:40 -0700 | [diff] [blame] | 247 | for (size_t i = 0; i < GPR_ARRAY_SIZE(payload_types); i++) { |
| 248 | for (size_t j = 0; j < GPR_ARRAY_SIZE(compression_types); j++) { |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 249 | char* log_suffix; |
| 250 | gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)", |
David Garcia Quintas | 2a6427f | 2015-08-18 11:11:40 -0700 | [diff] [blame] | 251 | CompressionType_Name(compression_types[j]).c_str(), |
| 252 | PayloadType_Name(payload_types[i]).c_str()); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 253 | |
David Garcia Quintas | 616b375 | 2015-08-11 15:21:02 -0700 | [diff] [blame] | 254 | gpr_log(GPR_INFO, "Sending a large compressed unary rpc %s.", log_suffix); |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 255 | SimpleRequest request; |
| 256 | SimpleResponse response; |
David Garcia Quintas | 2a6427f | 2015-08-18 11:11:40 -0700 | [diff] [blame] | 257 | request.set_response_type(payload_types[i]); |
| 258 | request.set_response_compression(compression_types[j]); |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 259 | PerformLargeUnary(&request, &response); |
David Garcia Quintas | 616b375 | 2015-08-11 15:21:02 -0700 | [diff] [blame] | 260 | gpr_log(GPR_INFO, "Large compressed unary done %s.", log_suffix); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 261 | gpr_free(log_suffix); |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void InteropClient::DoRequestStreaming() { |
| 267 | gpr_log(GPR_INFO, "Sending request steaming rpc ..."); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 268 | |
| 269 | ClientContext context; |
| 270 | StreamingInputCallRequest request; |
| 271 | StreamingInputCallResponse response; |
| 272 | |
| 273 | std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream( |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 274 | stub_->StreamingInputCall(&context, &response)); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 275 | |
| 276 | int aggregated_payload_size = 0; |
| 277 | for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) { |
| 278 | Payload* payload = request.mutable_payload(); |
| 279 | payload->set_body(grpc::string(request_stream_sizes[i], '\0')); |
| 280 | GPR_ASSERT(stream->Write(request)); |
| 281 | aggregated_payload_size += request_stream_sizes[i]; |
| 282 | } |
| 283 | stream->WritesDone(); |
| 284 | Status s = stream->Finish(); |
| 285 | |
| 286 | GPR_ASSERT(response.aggregated_payload_size() == aggregated_payload_size); |
| 287 | AssertOkOrPrintErrorStatus(s); |
| 288 | gpr_log(GPR_INFO, "Request streaming done."); |
| 289 | } |
| 290 | |
| 291 | void InteropClient::DoResponseStreaming() { |
David Garcia Quintas | cd37d58 | 2015-08-09 15:50:21 -0700 | [diff] [blame] | 292 | gpr_log(GPR_INFO, "Receiving response steaming rpc ..."); |
David Garcia Quintas | cd37d58 | 2015-08-09 15:50:21 -0700 | [diff] [blame] | 293 | |
| 294 | ClientContext context; |
| 295 | StreamingOutputCallRequest request; |
| 296 | for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) { |
| 297 | ResponseParameters* response_parameter = request.add_response_parameters(); |
| 298 | response_parameter->set_size(response_stream_sizes[i]); |
| 299 | } |
| 300 | StreamingOutputCallResponse response; |
| 301 | std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream( |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 302 | stub_->StreamingOutputCall(&context, request)); |
David Garcia Quintas | cd37d58 | 2015-08-09 15:50:21 -0700 | [diff] [blame] | 303 | |
| 304 | unsigned int i = 0; |
| 305 | while (stream->Read(&response)) { |
| 306 | GPR_ASSERT(response.payload().body() == |
| 307 | grpc::string(response_stream_sizes[i], '\0')); |
| 308 | ++i; |
| 309 | } |
| 310 | GPR_ASSERT(response_stream_sizes.size() == i); |
| 311 | Status s = stream->Finish(); |
| 312 | AssertOkOrPrintErrorStatus(s); |
| 313 | gpr_log(GPR_INFO, "Response streaming done."); |
| 314 | } |
| 315 | |
| 316 | void InteropClient::DoResponseCompressedStreaming() { |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 317 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 318 | const CompressionType compression_types[] = {NONE, GZIP, DEFLATE}; |
| 319 | const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM}; |
David Garcia Quintas | 2a6427f | 2015-08-18 11:11:40 -0700 | [diff] [blame] | 320 | for (size_t i = 0; i < GPR_ARRAY_SIZE(payload_types); i++) { |
| 321 | for (size_t j = 0; j < GPR_ARRAY_SIZE(compression_types); j++) { |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 322 | ClientContext context; |
| 323 | InteropClientContextInspector inspector(context); |
| 324 | StreamingOutputCallRequest request; |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 325 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 326 | char* log_suffix; |
| 327 | gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)", |
David Garcia Quintas | 2a6427f | 2015-08-18 11:11:40 -0700 | [diff] [blame] | 328 | CompressionType_Name(compression_types[j]).c_str(), |
| 329 | PayloadType_Name(payload_types[i]).c_str()); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 330 | |
| 331 | gpr_log(GPR_INFO, "Receiving response steaming rpc %s.", log_suffix); |
| 332 | |
David Garcia Quintas | 2a6427f | 2015-08-18 11:11:40 -0700 | [diff] [blame] | 333 | request.set_response_type(payload_types[i]); |
| 334 | request.set_response_compression(compression_types[j]); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 335 | |
David Garcia Quintas | 04ecfa1 | 2015-08-25 14:19:48 -0700 | [diff] [blame] | 336 | for (size_t k = 0; k < response_stream_sizes.size(); ++k) { |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 337 | ResponseParameters* response_parameter = |
| 338 | request.add_response_parameters(); |
David Garcia Quintas | 04ecfa1 | 2015-08-25 14:19:48 -0700 | [diff] [blame] | 339 | response_parameter->set_size(response_stream_sizes[k]); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 340 | } |
| 341 | StreamingOutputCallResponse response; |
| 342 | |
| 343 | std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream( |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 344 | stub_->StreamingOutputCall(&context, request)); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 345 | |
David Garcia Quintas | 04ecfa1 | 2015-08-25 14:19:48 -0700 | [diff] [blame] | 346 | size_t k = 0; |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 347 | while (stream->Read(&response)) { |
David Garcia Quintas | 04ecfa1 | 2015-08-25 14:19:48 -0700 | [diff] [blame] | 348 | // Payload related checks. |
| 349 | if (request.response_type() != PayloadType::RANDOM) { |
| 350 | GPR_ASSERT(response.payload().type() == request.response_type()); |
| 351 | } |
| 352 | switch (response.payload().type()) { |
| 353 | case PayloadType::COMPRESSABLE: |
| 354 | GPR_ASSERT(response.payload().body() == |
| 355 | grpc::string(response_stream_sizes[k], '\0')); |
| 356 | break; |
| 357 | case PayloadType::UNCOMPRESSABLE: { |
| 358 | std::ifstream rnd_file(kRandomFile); |
| 359 | GPR_ASSERT(rnd_file.good()); |
| 360 | for (int n = 0; n < response_stream_sizes[k]; n++) { |
| 361 | GPR_ASSERT(response.payload().body()[n] == (char)rnd_file.get()); |
| 362 | } |
| 363 | } break; |
| 364 | default: |
| 365 | GPR_ASSERT(false); |
| 366 | } |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 367 | |
| 368 | // Compression related checks. |
| 369 | GPR_ASSERT(request.response_compression() == |
| 370 | GetInteropCompressionTypeFromCompressionAlgorithm( |
| 371 | inspector.GetCallCompressionAlgorithm())); |
| 372 | if (request.response_compression() == NONE) { |
| 373 | GPR_ASSERT( |
| 374 | !(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS)); |
| 375 | } else if (request.response_type() == PayloadType::COMPRESSABLE) { |
| 376 | // requested compression and compressable response => results should |
| 377 | // always be compressed. |
| 378 | GPR_ASSERT(inspector.GetMessageFlags() & |
| 379 | GRPC_WRITE_INTERNAL_COMPRESS); |
| 380 | } |
| 381 | |
David Garcia Quintas | 04ecfa1 | 2015-08-25 14:19:48 -0700 | [diff] [blame] | 382 | ++k; |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 383 | } |
| 384 | |
David Garcia Quintas | 04ecfa1 | 2015-08-25 14:19:48 -0700 | [diff] [blame] | 385 | GPR_ASSERT(response_stream_sizes.size() == k); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 386 | Status s = stream->Finish(); |
| 387 | |
| 388 | AssertOkOrPrintErrorStatus(s); |
| 389 | gpr_log(GPR_INFO, "Response streaming done %s.", log_suffix); |
| 390 | gpr_free(log_suffix); |
| 391 | } |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 392 | } |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void InteropClient::DoResponseStreamingWithSlowConsumer() { |
| 396 | gpr_log(GPR_INFO, "Receiving response steaming rpc with slow consumer ..."); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 397 | |
| 398 | ClientContext context; |
| 399 | StreamingOutputCallRequest request; |
| 400 | |
| 401 | for (int i = 0; i < kNumResponseMessages; ++i) { |
| 402 | ResponseParameters* response_parameter = request.add_response_parameters(); |
| 403 | response_parameter->set_size(kResponseMessageSize); |
| 404 | } |
| 405 | StreamingOutputCallResponse response; |
| 406 | std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream( |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 407 | stub_->StreamingOutputCall(&context, request)); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 408 | |
| 409 | int i = 0; |
| 410 | while (stream->Read(&response)) { |
| 411 | GPR_ASSERT(response.payload().body() == |
| 412 | grpc::string(kResponseMessageSize, '\0')); |
| 413 | gpr_log(GPR_INFO, "received message %d", i); |
| 414 | usleep(kReceiveDelayMilliSeconds * 1000); |
| 415 | ++i; |
| 416 | } |
| 417 | GPR_ASSERT(kNumResponseMessages == i); |
| 418 | Status s = stream->Finish(); |
| 419 | |
| 420 | AssertOkOrPrintErrorStatus(s); |
| 421 | gpr_log(GPR_INFO, "Response streaming done."); |
| 422 | } |
| 423 | |
| 424 | void InteropClient::DoHalfDuplex() { |
| 425 | gpr_log(GPR_INFO, "Sending half-duplex streaming rpc ..."); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 426 | |
| 427 | ClientContext context; |
| 428 | std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 429 | StreamingOutputCallResponse>> |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 430 | stream(stub_->HalfDuplexCall(&context)); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 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)) { |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 443 | GPR_ASSERT(response.payload().body() == |
| 444 | grpc::string(response_stream_sizes[i], '\0')); |
| 445 | ++i; |
| 446 | } |
| 447 | GPR_ASSERT(response_stream_sizes.size() == i); |
| 448 | Status s = stream->Finish(); |
| 449 | AssertOkOrPrintErrorStatus(s); |
| 450 | gpr_log(GPR_INFO, "Half-duplex streaming rpc done."); |
| 451 | } |
| 452 | |
| 453 | void InteropClient::DoPingPong() { |
| 454 | gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ..."); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 455 | |
| 456 | ClientContext context; |
| 457 | std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 458 | StreamingOutputCallResponse>> |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 459 | stream(stub_->FullDuplexCall(&context)); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 460 | |
| 461 | StreamingOutputCallRequest request; |
| 462 | request.set_response_type(PayloadType::COMPRESSABLE); |
| 463 | ResponseParameters* response_parameter = request.add_response_parameters(); |
| 464 | Payload* payload = request.mutable_payload(); |
| 465 | StreamingOutputCallResponse response; |
| 466 | for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) { |
| 467 | response_parameter->set_size(response_stream_sizes[i]); |
| 468 | payload->set_body(grpc::string(request_stream_sizes[i], '\0')); |
| 469 | GPR_ASSERT(stream->Write(request)); |
| 470 | GPR_ASSERT(stream->Read(&response)); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 471 | GPR_ASSERT(response.payload().body() == |
| 472 | grpc::string(response_stream_sizes[i], '\0')); |
| 473 | } |
| 474 | |
| 475 | stream->WritesDone(); |
| 476 | GPR_ASSERT(!stream->Read(&response)); |
| 477 | Status s = stream->Finish(); |
| 478 | AssertOkOrPrintErrorStatus(s); |
| 479 | gpr_log(GPR_INFO, "Ping pong streaming done."); |
| 480 | } |
| 481 | |
Yang Gao | 68d6157 | 2015-04-24 14:42:42 -0700 | [diff] [blame] | 482 | void InteropClient::DoCancelAfterBegin() { |
| 483 | gpr_log(GPR_INFO, "Sending request steaming rpc ..."); |
Yang Gao | 68d6157 | 2015-04-24 14:42:42 -0700 | [diff] [blame] | 484 | |
| 485 | ClientContext context; |
| 486 | StreamingInputCallRequest request; |
| 487 | StreamingInputCallResponse response; |
| 488 | |
| 489 | std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream( |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 490 | stub_->StreamingInputCall(&context, &response)); |
Yang Gao | 68d6157 | 2015-04-24 14:42:42 -0700 | [diff] [blame] | 491 | |
| 492 | gpr_log(GPR_INFO, "Trying to cancel..."); |
| 493 | context.TryCancel(); |
| 494 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 495 | GPR_ASSERT(s.error_code() == StatusCode::CANCELLED); |
Yang Gao | 68d6157 | 2015-04-24 14:42:42 -0700 | [diff] [blame] | 496 | gpr_log(GPR_INFO, "Canceling streaming done."); |
| 497 | } |
| 498 | |
| 499 | void InteropClient::DoCancelAfterFirstResponse() { |
| 500 | gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ..."); |
Yang Gao | 68d6157 | 2015-04-24 14:42:42 -0700 | [diff] [blame] | 501 | |
| 502 | ClientContext context; |
| 503 | std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 504 | StreamingOutputCallResponse>> |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 505 | stream(stub_->FullDuplexCall(&context)); |
Yang Gao | 68d6157 | 2015-04-24 14:42:42 -0700 | [diff] [blame] | 506 | |
| 507 | StreamingOutputCallRequest request; |
| 508 | request.set_response_type(PayloadType::COMPRESSABLE); |
| 509 | ResponseParameters* response_parameter = request.add_response_parameters(); |
| 510 | response_parameter->set_size(31415); |
| 511 | request.mutable_payload()->set_body(grpc::string(27182, '\0')); |
| 512 | StreamingOutputCallResponse response; |
| 513 | GPR_ASSERT(stream->Write(request)); |
| 514 | GPR_ASSERT(stream->Read(&response)); |
Yang Gao | 68d6157 | 2015-04-24 14:42:42 -0700 | [diff] [blame] | 515 | GPR_ASSERT(response.payload().body() == grpc::string(31415, '\0')); |
| 516 | gpr_log(GPR_INFO, "Trying to cancel..."); |
| 517 | context.TryCancel(); |
| 518 | |
| 519 | Status s = stream->Finish(); |
| 520 | gpr_log(GPR_INFO, "Canceling pingpong streaming done."); |
| 521 | } |
| 522 | |
yang-g | 69563b9 | 2015-07-10 15:32:11 -0700 | [diff] [blame] | 523 | void InteropClient::DoTimeoutOnSleepingServer() { |
| 524 | gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc with a short deadline..."); |
yang-g | 69563b9 | 2015-07-10 15:32:11 -0700 | [diff] [blame] | 525 | |
| 526 | ClientContext context; |
| 527 | std::chrono::system_clock::time_point deadline = |
| 528 | std::chrono::system_clock::now() + std::chrono::milliseconds(1); |
| 529 | context.set_deadline(deadline); |
| 530 | std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 531 | StreamingOutputCallResponse>> |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 532 | stream(stub_->FullDuplexCall(&context)); |
yang-g | 69563b9 | 2015-07-10 15:32:11 -0700 | [diff] [blame] | 533 | |
| 534 | StreamingOutputCallRequest request; |
| 535 | request.mutable_payload()->set_body(grpc::string(27182, '\0')); |
| 536 | stream->Write(request); |
| 537 | |
| 538 | Status s = stream->Finish(); |
| 539 | GPR_ASSERT(s.error_code() == StatusCode::DEADLINE_EXCEEDED); |
| 540 | gpr_log(GPR_INFO, "Pingpong streaming timeout done."); |
| 541 | } |
| 542 | |
yang-g | 78bddc6 | 2015-09-21 10:32:17 -0700 | [diff] [blame] | 543 | void InteropClient::DoEmptyStream() { |
| 544 | gpr_log(GPR_INFO, "Starting empty_stream."); |
yang-g | 78bddc6 | 2015-09-21 10:32:17 -0700 | [diff] [blame] | 545 | |
| 546 | ClientContext context; |
| 547 | std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest, |
| 548 | StreamingOutputCallResponse>> |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 549 | stream(stub_->FullDuplexCall(&context)); |
yang-g | 78bddc6 | 2015-09-21 10:32:17 -0700 | [diff] [blame] | 550 | stream->WritesDone(); |
| 551 | StreamingOutputCallResponse response; |
| 552 | GPR_ASSERT(stream->Read(&response) == false); |
| 553 | Status s = stream->Finish(); |
| 554 | AssertOkOrPrintErrorStatus(s); |
| 555 | gpr_log(GPR_INFO, "empty_stream done."); |
| 556 | } |
| 557 | |
Abhishek Kumar | e1c867d | 2015-08-05 11:04:45 -0700 | [diff] [blame] | 558 | void InteropClient::DoStatusWithMessage() { |
| 559 | gpr_log(GPR_INFO, "Sending RPC with a request for status code 2 and message"); |
Abhishek Kumar | e1c867d | 2015-08-05 11:04:45 -0700 | [diff] [blame] | 560 | |
| 561 | ClientContext context; |
| 562 | SimpleRequest request; |
| 563 | SimpleResponse response; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 564 | EchoStatus* requested_status = request.mutable_response_status(); |
Abhishek Kumar | e1c867d | 2015-08-05 11:04:45 -0700 | [diff] [blame] | 565 | requested_status->set_code(grpc::StatusCode::UNKNOWN); |
| 566 | grpc::string test_msg = "This is a test message"; |
| 567 | requested_status->set_message(test_msg); |
| 568 | |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame^] | 569 | Status s = stub_->UnaryCall(&context, request, &response); |
Abhishek Kumar | e1c867d | 2015-08-05 11:04:45 -0700 | [diff] [blame] | 570 | |
| 571 | GPR_ASSERT(s.error_code() == grpc::StatusCode::UNKNOWN); |
| 572 | GPR_ASSERT(s.error_message() == test_msg); |
| 573 | gpr_log(GPR_INFO, "Done testing Status and Message"); |
| 574 | } |
| 575 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 576 | } // namespace testing |
| 577 | } // namespace grpc |