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