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 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 36 | #include <fstream> |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 37 | #include <memory> |
| 38 | |
| 39 | #include <unistd.h> |
| 40 | |
| 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> |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 44 | #include <grpc++/channel_interface.h> |
| 45 | #include <grpc++/client_context.h> |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 46 | #include <grpc++/credentials.h> |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 47 | #include <grpc++/status.h> |
| 48 | #include <grpc++/stream.h> |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 49 | |
| 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" |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 54 | #include "src/core/transport/stream_op.h" |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 55 | |
| 56 | namespace grpc { |
| 57 | namespace testing { |
| 58 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 59 | static const char* kRandomFile = "test/cpp/interop/rnd.dat"; |
| 60 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 61 | namespace { |
| 62 | // The same value is defined by the Java client. |
| 63 | const std::vector<int> request_stream_sizes = {27182, 8, 1828, 45904}; |
| 64 | const std::vector<int> response_stream_sizes = {31415, 9, 2653, 58979}; |
| 65 | const int kNumResponseMessages = 2000; |
| 66 | const int kResponseMessageSize = 1030; |
| 67 | const int kReceiveDelayMilliSeconds = 20; |
Xudong Ma | a5861f3 | 2015-05-26 15:24:11 -0700 | [diff] [blame] | 68 | const int kLargeRequestSize = 271828; |
| 69 | const int kLargeResponseSize = 314159; |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 70 | } // namespace |
| 71 | |
| 72 | InteropClient::InteropClient(std::shared_ptr<ChannelInterface> channel) |
| 73 | : channel_(channel) {} |
| 74 | |
| 75 | void InteropClient::AssertOkOrPrintErrorStatus(const Status& s) { |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 76 | if (s.ok()) { |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 77 | return; |
| 78 | } |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 79 | gpr_log(GPR_INFO, "Error status code: %d, message: %s", s.error_code(), |
| 80 | s.error_message().c_str()); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 81 | GPR_ASSERT(0); |
| 82 | } |
| 83 | |
| 84 | void 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. |
| 99 | void InteropClient::PerformLargeUnary(SimpleRequest* request, |
| 100 | SimpleResponse* response) { |
| 101 | std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_)); |
| 102 | |
| 103 | ClientContext context; |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 104 | InteropClientContextInspector inspector(context); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 105 | 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 Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 111 | // Compression related checks. |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 112 | GPR_ASSERT(request->response_compression() == |
| 113 | GetInteropCompressionTypeFromCompressionAlgorithm( |
| 114 | inspector.GetCallCompressionAlgorithm())); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 115 | 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 Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 123 | AssertOkOrPrintErrorStatus(s); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 124 | |
| 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 Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void 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 Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 156 | request.set_response_type(PayloadType::COMPRESSABLE); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 157 | 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 | |
| 168 | void 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 Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 176 | request.set_response_type(PayloadType::COMPRESSABLE); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 177 | 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-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 186 | void InteropClient::DoOauth2AuthToken(const grpc::string& username, |
| 187 | const grpc::string& oauth_scope) { |
| 188 | gpr_log(GPR_INFO, |
yang-g | 463cde7 | 2015-07-17 15:21:39 -0700 | [diff] [blame] | 189 | "Sending a unary rpc with raw oauth2 access token credentials ..."); |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 190 | SimpleRequest request; |
| 191 | SimpleResponse response; |
| 192 | request.set_fill_username(true); |
| 193 | request.set_fill_oauth_scope(true); |
yang-g | 463cde7 | 2015-07-17 15:21:39 -0700 | [diff] [blame] | 194 | 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-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 201 | 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-g | 463cde7 | 2015-07-17 15:21:39 -0700 | [diff] [blame] | 206 | gpr_log(GPR_INFO, "Unary with oauth2 access token credentials done."); |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 207 | } |
| 208 | |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 209 | void InteropClient::DoPerRpcCreds(const grpc::string& username, |
| 210 | const grpc::string& oauth_scope) { |
| 211 | gpr_log(GPR_INFO, |
yang-g | 8c31ee2 | 2015-07-15 13:36:27 -0700 | [diff] [blame] | 212 | "Sending a unary rpc with per-rpc raw oauth2 access token ..."); |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 213 | 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-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 223 | |
| 224 | Status s = stub->UnaryCall(&context, request, &response); |
| 225 | |
| 226 | AssertOkOrPrintErrorStatus(s); |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 227 | 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-g | 8c31ee2 | 2015-07-15 13:36:27 -0700 | [diff] [blame] | 232 | gpr_log(GPR_INFO, "Unary with per-rpc oauth2 access token done."); |
yang-g | 5bf510b | 2015-07-14 10:54:29 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 235 | void 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 Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 240 | request.set_response_type(PayloadType::COMPRESSABLE); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 241 | 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 | |
| 247 | void InteropClient::DoLargeUnary() { |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 248 | 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 Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 252 | 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 Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 258 | 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 Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 263 | gpr_log(GPR_INFO, "Large unary done %s.", log_suffix); |
| 264 | gpr_free(log_suffix); |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void 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 | |
| 295 | void InteropClient::DoResponseStreaming() { |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 296 | std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_)); |
| 297 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 298 | 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 Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 305 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 306 | 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 Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 355 | } |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void 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 | |
| 388 | void 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 | |
| 419 | void 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 Gao | 68d6157 | 2015-04-24 14:42:42 -0700 | [diff] [blame] | 450 | void 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 Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 464 | GPR_ASSERT(s.error_code() == StatusCode::CANCELLED); |
Yang Gao | 68d6157 | 2015-04-24 14:42:42 -0700 | [diff] [blame] | 465 | gpr_log(GPR_INFO, "Canceling streaming done."); |
| 466 | } |
| 467 | |
| 468 | void 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-g | 69563b9 | 2015-07-10 15:32:11 -0700 | [diff] [blame] | 494 | void 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 Kumar | e1c867d | 2015-08-05 11:04:45 -0700 | [diff] [blame] | 515 | void 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 Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 534 | } // namespace testing |
| 535 | } // namespace grpc |