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