Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Yang Gao | 69fe075 | 2015-06-01 14:32:38 -0700 | [diff] [blame] | 34 | #include <mutex> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | #include <thread> |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 36 | |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 37 | #include "src/core/security/credentials.h" |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 38 | #include "test/core/end2end/data/ssl_test_data.h" |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 39 | #include "test/core/util/port.h" |
Craig Tiller | 14e60e9 | 2015-01-13 17:26:46 -0800 | [diff] [blame] | 40 | #include "test/core/util/test_config.h" |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 41 | #include "test/cpp/util/echo_duplicate.grpc.pb.h" |
| 42 | #include "test/cpp/util/echo.grpc.pb.h" |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 43 | #include <grpc++/channel_arguments.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include <grpc++/channel_interface.h> |
| 45 | #include <grpc++/client_context.h> |
| 46 | #include <grpc++/create_channel.h> |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 47 | #include <grpc++/credentials.h> |
Vijay Pai | 1f3e6c1 | 2015-07-23 16:18:21 -0700 | [diff] [blame] | 48 | #include <grpc++/dynamic_thread_pool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | #include <grpc++/server.h> |
| 50 | #include <grpc++/server_builder.h> |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 51 | #include <grpc++/server_context.h> |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 52 | #include <grpc++/server_credentials.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | #include <grpc++/status.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 54 | #include <grpc++/stream.h> |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 55 | #include <grpc++/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | #include <gtest/gtest.h> |
| 57 | |
| 58 | #include <grpc/grpc.h> |
| 59 | #include <grpc/support/thd.h> |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 60 | #include <grpc/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | |
| 62 | using grpc::cpp::test::util::EchoRequest; |
| 63 | using grpc::cpp::test::util::EchoResponse; |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 64 | using std::chrono::system_clock; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | |
| 66 | namespace grpc { |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 67 | namespace testing { |
| 68 | |
| 69 | namespace { |
| 70 | |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 71 | const char* kServerCancelAfterReads = "cancel_after_reads"; |
| 72 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 73 | // When echo_deadline is requested, deadline seen in the ServerContext is set in |
| 74 | // the response in seconds. |
| 75 | void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request, |
| 76 | EchoResponse* response) { |
| 77 | if (request->has_param() && request->param().echo_deadline()) { |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 78 | gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 79 | if (context->deadline() != system_clock::time_point::max()) { |
| 80 | Timepoint2Timespec(context->deadline(), &deadline); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 81 | } |
| 82 | response->mutable_param()->set_request_deadline(deadline.tv_sec); |
| 83 | } |
| 84 | } |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 85 | |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 86 | void CheckServerAuthContext(const ServerContext* context) { |
yang-g | 85c04f9 | 2015-07-07 17:47:31 -0700 | [diff] [blame] | 87 | std::shared_ptr<const AuthContext> auth_ctx = context->auth_context(); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 88 | std::vector<grpc::string> ssl = |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 89 | auth_ctx->FindPropertyValues("transport_security_type"); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 90 | EXPECT_EQ(1u, ssl.size()); |
| 91 | EXPECT_EQ("ssl", ssl[0]); |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty()); |
| 93 | EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty()); |
| 94 | } |
| 95 | |
yang-g | d7ead69 | 2015-07-30 10:57:45 -0700 | [diff] [blame] | 96 | bool CheckIsLocalhost(const grpc::string& addr) { |
| 97 | const grpc::string kIpv6("ipv6:[::1]:"); |
| 98 | const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:"); |
| 99 | const grpc::string kIpv4("ipv4:127.0.0.1:"); |
| 100 | return addr.substr(0, kIpv4.size()) == kIpv4 || |
| 101 | addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 || |
| 102 | addr.substr(0, kIpv6.size()) == kIpv6; |
| 103 | } |
| 104 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 105 | } // namespace |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 107 | class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 108 | public: |
vjpai | df55161 | 2015-07-14 13:38:44 -0700 | [diff] [blame] | 109 | TestServiceImpl() : signal_client_(false), host_() {} |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 110 | explicit TestServiceImpl(const grpc::string& host) |
| 111 | : signal_client_(false), host_(new grpc::string(host)) {} |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 112 | |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 113 | Status Echo(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 114 | EchoResponse* response) GRPC_OVERRIDE { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 115 | response->set_message(request->message()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 116 | MaybeEchoDeadline(context, request, response); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 117 | if (host_) { |
| 118 | response->mutable_param()->set_host(*host_); |
| 119 | } |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 120 | if (request->has_param() && request->param().client_cancel_after_us()) { |
| 121 | { |
| 122 | std::unique_lock<std::mutex> lock(mu_); |
| 123 | signal_client_ = true; |
| 124 | } |
| 125 | while (!context->IsCancelled()) { |
David Garcia Quintas | feb67f6 | 2015-05-20 19:23:25 -0700 | [diff] [blame] | 126 | gpr_sleep_until(gpr_time_add( |
Craig Tiller | 20b5fe9 | 2015-07-06 10:43:50 -0700 | [diff] [blame] | 127 | gpr_now(GPR_CLOCK_REALTIME), |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 128 | gpr_time_from_micros(request->param().client_cancel_after_us(), |
| 129 | GPR_TIMESPAN))); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 130 | } |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 131 | return Status::CANCELLED; |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 132 | } else if (request->has_param() && |
| 133 | request->param().server_cancel_after_us()) { |
David Garcia Quintas | feb67f6 | 2015-05-20 19:23:25 -0700 | [diff] [blame] | 134 | gpr_sleep_until(gpr_time_add( |
Craig Tiller | 20b5fe9 | 2015-07-06 10:43:50 -0700 | [diff] [blame] | 135 | gpr_now(GPR_CLOCK_REALTIME), |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 136 | gpr_time_from_micros(request->param().server_cancel_after_us(), |
| 137 | GPR_TIMESPAN))); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 138 | return Status::CANCELLED; |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 139 | } else { |
| 140 | EXPECT_FALSE(context->IsCancelled()); |
| 141 | } |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 142 | |
| 143 | if (request->has_param() && request->param().echo_metadata()) { |
| 144 | const std::multimap<grpc::string, grpc::string>& client_metadata = |
| 145 | context->client_metadata(); |
| 146 | for (std::multimap<grpc::string, grpc::string>::const_iterator iter = |
| 147 | client_metadata.begin(); |
| 148 | iter != client_metadata.end(); ++iter) { |
| 149 | context->AddTrailingMetadata((*iter).first, (*iter).second); |
| 150 | } |
| 151 | } |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 152 | if (request->has_param() && request->param().check_auth_context()) { |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 153 | CheckServerAuthContext(context); |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 154 | } |
yang-g | 6f30dec | 2015-07-22 23:11:56 -0700 | [diff] [blame] | 155 | if (request->has_param() && |
| 156 | request->param().response_message_length() > 0) { |
| 157 | response->set_message( |
| 158 | grpc::string(request->param().response_message_length(), '\0')); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 159 | } |
yang-g | d7ead69 | 2015-07-30 10:57:45 -0700 | [diff] [blame] | 160 | if (request->has_param() && request->param().echo_peer()) { |
| 161 | response->mutable_param()->set_peer(context->peer()); |
| 162 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 163 | return Status::OK; |
| 164 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 165 | |
| 166 | // Unimplemented is left unimplemented to test the returned error. |
| 167 | |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 168 | Status RequestStream(ServerContext* context, |
| 169 | ServerReader<EchoRequest>* reader, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 170 | EchoResponse* response) GRPC_OVERRIDE { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 171 | EchoRequest request; |
| 172 | response->set_message(""); |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 173 | int cancel_after_reads = 0; |
| 174 | const std::multimap<grpc::string, grpc::string> client_initial_metadata = |
| 175 | context->client_metadata(); |
| 176 | if (client_initial_metadata.find(kServerCancelAfterReads) != |
| 177 | client_initial_metadata.end()) { |
| 178 | std::istringstream iss( |
| 179 | client_initial_metadata.find(kServerCancelAfterReads)->second); |
| 180 | iss >> cancel_after_reads; |
| 181 | gpr_log(GPR_INFO, "cancel_after_reads %d", cancel_after_reads); |
| 182 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 183 | while (reader->Read(&request)) { |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 184 | if (cancel_after_reads == 1) { |
| 185 | gpr_log(GPR_INFO, "return cancel status"); |
| 186 | return Status::CANCELLED; |
| 187 | } else if (cancel_after_reads > 0) { |
| 188 | cancel_after_reads--; |
| 189 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 190 | response->mutable_message()->append(request.message()); |
| 191 | } |
| 192 | return Status::OK; |
| 193 | } |
| 194 | |
| 195 | // Return 3 messages. |
| 196 | // TODO(yangg) make it generic by adding a parameter into EchoRequest |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 197 | Status ResponseStream(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 198 | ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 199 | EchoResponse response; |
| 200 | response.set_message(request->message() + "0"); |
| 201 | writer->Write(response); |
| 202 | response.set_message(request->message() + "1"); |
| 203 | writer->Write(response); |
| 204 | response.set_message(request->message() + "2"); |
| 205 | writer->Write(response); |
| 206 | |
| 207 | return Status::OK; |
| 208 | } |
| 209 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 210 | Status BidiStream(ServerContext* context, |
| 211 | ServerReaderWriter<EchoResponse, EchoRequest>* stream) |
| 212 | GRPC_OVERRIDE { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 213 | EchoRequest request; |
| 214 | EchoResponse response; |
| 215 | while (stream->Read(&request)) { |
| 216 | gpr_log(GPR_INFO, "recv msg %s", request.message().c_str()); |
| 217 | response.set_message(request.message()); |
| 218 | stream->Write(response); |
| 219 | } |
| 220 | return Status::OK; |
| 221 | } |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 222 | |
| 223 | bool signal_client() { |
| 224 | std::unique_lock<std::mutex> lock(mu_); |
| 225 | return signal_client_; |
| 226 | } |
| 227 | |
| 228 | private: |
| 229 | bool signal_client_; |
| 230 | std::mutex mu_; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 231 | std::unique_ptr<grpc::string> host_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 232 | }; |
| 233 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 234 | class TestServiceImplDupPkg |
| 235 | : public ::grpc::cpp::test::util::duplicate::TestService::Service { |
| 236 | public: |
| 237 | Status Echo(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 238 | EchoResponse* response) GRPC_OVERRIDE { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 239 | response->set_message("no package"); |
| 240 | return Status::OK; |
| 241 | } |
| 242 | }; |
| 243 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 244 | class End2endTest : public ::testing::Test { |
| 245 | protected: |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 246 | End2endTest() |
| 247 | : kMaxMessageSize_(8192), special_service_("special"), thread_pool_(2) {} |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 248 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 249 | void SetUp() GRPC_OVERRIDE { |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 250 | int port = grpc_pick_unused_port_or_die(); |
yang-g | d7ead69 | 2015-07-30 10:57:45 -0700 | [diff] [blame] | 251 | server_address_ << "127.0.0.1:" << port; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 252 | // Setup server |
| 253 | ServerBuilder builder; |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 254 | SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key, |
| 255 | test_server1_cert}; |
| 256 | SslServerCredentialsOptions ssl_opts; |
| 257 | ssl_opts.pem_root_certs = ""; |
| 258 | ssl_opts.pem_key_cert_pairs.push_back(pkcp); |
Yang Gao | b57f72d | 2015-05-17 21:54:54 -0700 | [diff] [blame] | 259 | builder.AddListeningPort(server_address_.str(), |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 260 | SslServerCredentials(ssl_opts)); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 261 | builder.RegisterService(&service_); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 262 | builder.RegisterService("foo.test.youtube.com", &special_service_); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 263 | builder.SetMaxMessageSize( |
| 264 | kMaxMessageSize_); // For testing max message size. |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 265 | builder.RegisterService(&dup_pkg_service_); |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 266 | builder.SetThreadPool(&thread_pool_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 267 | server_ = builder.BuildAndStart(); |
| 268 | } |
| 269 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 270 | void TearDown() GRPC_OVERRIDE { server_->Shutdown(); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 271 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 272 | void ResetStub() { |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 273 | SslCredentialsOptions ssl_opts = {test_root_cert, "", ""}; |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 274 | ChannelArguments args; |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 275 | args.SetSslTargetNameOverride("foo.test.google.fr"); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 276 | args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test"); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 277 | channel_ = CreateChannel(server_address_.str(), SslCredentials(ssl_opts), |
| 278 | args); |
| 279 | stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel_)); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 280 | } |
| 281 | |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 282 | std::shared_ptr<ChannelInterface> channel_; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 283 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 284 | std::unique_ptr<Server> server_; |
| 285 | std::ostringstream server_address_; |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 286 | const int kMaxMessageSize_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 287 | TestServiceImpl service_; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 288 | TestServiceImpl special_service_; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 289 | TestServiceImplDupPkg dup_pkg_service_; |
Vijay Pai | 1f3e6c1 | 2015-07-23 16:18:21 -0700 | [diff] [blame] | 290 | DynamicThreadPool thread_pool_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 291 | }; |
| 292 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 293 | static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub, |
| 294 | int num_rpcs) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 295 | EchoRequest request; |
| 296 | EchoResponse response; |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 297 | request.set_message("Hello hello hello hello"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 298 | |
| 299 | for (int i = 0; i < num_rpcs; ++i) { |
| 300 | ClientContext context; |
Craig Tiller | bf6abee | 2015-07-19 22:31:04 -0700 | [diff] [blame] | 301 | context.set_compression_algorithm(GRPC_COMPRESS_GZIP); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 302 | Status s = stub->Echo(&context, request, &response); |
| 303 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 304 | EXPECT_TRUE(s.ok()); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 305 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 308 | TEST_F(End2endTest, SimpleRpcWithHost) { |
| 309 | ResetStub(); |
| 310 | |
| 311 | EchoRequest request; |
| 312 | EchoResponse response; |
| 313 | request.set_message("Hello"); |
| 314 | |
| 315 | ClientContext context; |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 316 | context.set_authority("foo.test.youtube.com"); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 317 | Status s = stub_->Echo(&context, request, &response); |
| 318 | EXPECT_EQ(response.message(), request.message()); |
| 319 | EXPECT_TRUE(response.has_param()); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 320 | EXPECT_EQ("special", response.param().host()); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 321 | EXPECT_TRUE(s.ok()); |
| 322 | } |
| 323 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 324 | TEST_F(End2endTest, SimpleRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 325 | ResetStub(); |
| 326 | SendRpc(stub_.get(), 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | TEST_F(End2endTest, MultipleRpcs) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 330 | ResetStub(); |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 331 | std::vector<std::thread*> threads; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 332 | for (int i = 0; i < 10; ++i) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 333 | threads.push_back(new std::thread(SendRpc, stub_.get(), 10)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 334 | } |
| 335 | for (int i = 0; i < 10; ++i) { |
| 336 | threads[i]->join(); |
| 337 | delete threads[i]; |
| 338 | } |
| 339 | } |
| 340 | |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 341 | // Set a 10us deadline and make sure proper error is returned. |
| 342 | TEST_F(End2endTest, RpcDeadlineExpires) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 343 | ResetStub(); |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 344 | EchoRequest request; |
| 345 | EchoResponse response; |
| 346 | request.set_message("Hello"); |
| 347 | |
| 348 | ClientContext context; |
| 349 | std::chrono::system_clock::time_point deadline = |
| 350 | std::chrono::system_clock::now() + std::chrono::microseconds(10); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 351 | context.set_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 352 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 353 | EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code()); |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 354 | } |
| 355 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 356 | // Set a long but finite deadline. |
| 357 | TEST_F(End2endTest, RpcLongDeadline) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 358 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 359 | EchoRequest request; |
| 360 | EchoResponse response; |
| 361 | request.set_message("Hello"); |
| 362 | |
| 363 | ClientContext context; |
| 364 | std::chrono::system_clock::time_point deadline = |
| 365 | std::chrono::system_clock::now() + std::chrono::hours(1); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 366 | context.set_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 367 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 368 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 369 | EXPECT_TRUE(s.ok()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // Ask server to echo back the deadline it sees. |
| 373 | TEST_F(End2endTest, EchoDeadline) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 374 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 375 | EchoRequest request; |
| 376 | EchoResponse response; |
| 377 | request.set_message("Hello"); |
| 378 | request.mutable_param()->set_echo_deadline(true); |
| 379 | |
| 380 | ClientContext context; |
| 381 | std::chrono::system_clock::time_point deadline = |
| 382 | std::chrono::system_clock::now() + std::chrono::seconds(100); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 383 | context.set_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 384 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 385 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 386 | EXPECT_TRUE(s.ok()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 387 | gpr_timespec sent_deadline; |
| 388 | Timepoint2Timespec(deadline, &sent_deadline); |
| 389 | // Allow 1 second error. |
| 390 | EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1); |
| 391 | EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // Ask server to echo back the deadline it sees. The rpc has no deadline. |
| 395 | TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 396 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 397 | EchoRequest request; |
| 398 | EchoResponse response; |
| 399 | request.set_message("Hello"); |
| 400 | request.mutable_param()->set_echo_deadline(true); |
| 401 | |
| 402 | ClientContext context; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 403 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 404 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 405 | EXPECT_TRUE(s.ok()); |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 406 | EXPECT_EQ(response.param().request_deadline(), |
| 407 | gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 408 | } |
| 409 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 410 | TEST_F(End2endTest, UnimplementedRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 411 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 412 | EchoRequest request; |
| 413 | EchoResponse response; |
| 414 | request.set_message("Hello"); |
| 415 | |
| 416 | ClientContext context; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 417 | Status s = stub_->Unimplemented(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 418 | EXPECT_FALSE(s.ok()); |
| 419 | EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED); |
| 420 | EXPECT_EQ(s.error_message(), ""); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 421 | EXPECT_EQ(response.message(), ""); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TEST_F(End2endTest, RequestStreamOneRequest) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 425 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 426 | EchoRequest request; |
| 427 | EchoResponse response; |
| 428 | ClientContext context; |
| 429 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 430 | auto stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 431 | request.set_message("hello"); |
| 432 | EXPECT_TRUE(stream->Write(request)); |
| 433 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 434 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 435 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 436 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | TEST_F(End2endTest, RequestStreamTwoRequests) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 440 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 441 | EchoRequest request; |
| 442 | EchoResponse response; |
| 443 | ClientContext context; |
| 444 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 445 | auto stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 446 | request.set_message("hello"); |
| 447 | EXPECT_TRUE(stream->Write(request)); |
| 448 | EXPECT_TRUE(stream->Write(request)); |
| 449 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 450 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 451 | EXPECT_EQ(response.message(), "hellohello"); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 452 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | TEST_F(End2endTest, ResponseStream) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 456 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 457 | EchoRequest request; |
| 458 | EchoResponse response; |
| 459 | ClientContext context; |
| 460 | request.set_message("hello"); |
| 461 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 462 | auto stream = stub_->ResponseStream(&context, request); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 463 | EXPECT_TRUE(stream->Read(&response)); |
| 464 | EXPECT_EQ(response.message(), request.message() + "0"); |
| 465 | EXPECT_TRUE(stream->Read(&response)); |
| 466 | EXPECT_EQ(response.message(), request.message() + "1"); |
| 467 | EXPECT_TRUE(stream->Read(&response)); |
| 468 | EXPECT_EQ(response.message(), request.message() + "2"); |
| 469 | EXPECT_FALSE(stream->Read(&response)); |
| 470 | |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 471 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 472 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | TEST_F(End2endTest, BidiStream) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 476 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 477 | EchoRequest request; |
| 478 | EchoResponse response; |
| 479 | ClientContext context; |
| 480 | grpc::string msg("hello"); |
| 481 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 482 | auto stream = stub_->BidiStream(&context); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 483 | |
| 484 | request.set_message(msg + "0"); |
| 485 | EXPECT_TRUE(stream->Write(request)); |
| 486 | EXPECT_TRUE(stream->Read(&response)); |
| 487 | EXPECT_EQ(response.message(), request.message()); |
| 488 | |
| 489 | request.set_message(msg + "1"); |
| 490 | EXPECT_TRUE(stream->Write(request)); |
| 491 | EXPECT_TRUE(stream->Read(&response)); |
| 492 | EXPECT_EQ(response.message(), request.message()); |
| 493 | |
| 494 | request.set_message(msg + "2"); |
| 495 | EXPECT_TRUE(stream->Write(request)); |
| 496 | EXPECT_TRUE(stream->Read(&response)); |
| 497 | EXPECT_EQ(response.message(), request.message()); |
| 498 | |
| 499 | stream->WritesDone(); |
| 500 | EXPECT_FALSE(stream->Read(&response)); |
| 501 | |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 502 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 503 | EXPECT_TRUE(s.ok()); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // Talk to the two services with the same name but different package names. |
| 507 | // The two stubs are created on the same channel. |
| 508 | TEST_F(End2endTest, DiffPackageServices) { |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 509 | ResetStub(); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 510 | EchoRequest request; |
| 511 | EchoResponse response; |
| 512 | request.set_message("Hello"); |
| 513 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 514 | ClientContext context; |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 515 | Status s = stub_->Echo(&context, request, &response); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 516 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 517 | EXPECT_TRUE(s.ok()); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 518 | |
| 519 | std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub> |
| 520 | dup_pkg_stub( |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 521 | grpc::cpp::test::util::duplicate::TestService::NewStub(channel_)); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 522 | ClientContext context2; |
| 523 | s = dup_pkg_stub->Echo(&context2, request, &response); |
| 524 | EXPECT_EQ("no package", response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 525 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 526 | } |
| 527 | |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 528 | // rpc and stream should fail on bad credentials. |
| 529 | TEST_F(End2endTest, BadCredentials) { |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 530 | std::shared_ptr<Credentials> bad_creds = ServiceAccountCredentials("", "", 1); |
Craig Tiller | b256faa | 2015-07-23 11:28:16 -0700 | [diff] [blame] | 531 | EXPECT_EQ(static_cast<Credentials*>(nullptr), bad_creds.get()); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 532 | std::shared_ptr<ChannelInterface> channel = |
| 533 | CreateChannel(server_address_.str(), bad_creds, ChannelArguments()); |
| 534 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub( |
| 535 | grpc::cpp::test::util::TestService::NewStub(channel)); |
| 536 | EchoRequest request; |
| 537 | EchoResponse response; |
| 538 | ClientContext context; |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 539 | request.set_message("Hello"); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 540 | |
| 541 | Status s = stub->Echo(&context, request, &response); |
| 542 | EXPECT_EQ("", response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 543 | EXPECT_FALSE(s.ok()); |
| 544 | EXPECT_EQ(StatusCode::UNKNOWN, s.error_code()); |
| 545 | EXPECT_EQ("Rpc sent on a lame channel.", s.error_message()); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 546 | |
| 547 | ClientContext context2; |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 548 | auto stream = stub->BidiStream(&context2); |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 549 | s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 550 | EXPECT_FALSE(s.ok()); |
| 551 | EXPECT_EQ(StatusCode::UNKNOWN, s.error_code()); |
| 552 | EXPECT_EQ("Rpc sent on a lame channel.", s.error_message()); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 555 | void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) { |
Craig Tiller | 20b5fe9 | 2015-07-06 10:43:50 -0700 | [diff] [blame] | 556 | gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 557 | gpr_time_from_micros(delay_us, GPR_TIMESPAN))); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 558 | while (!service->signal_client()) { |
| 559 | } |
| 560 | context->TryCancel(); |
| 561 | } |
| 562 | |
| 563 | // Client cancels rpc after 10ms |
| 564 | TEST_F(End2endTest, ClientCancelsRpc) { |
| 565 | ResetStub(); |
| 566 | EchoRequest request; |
| 567 | EchoResponse response; |
| 568 | request.set_message("Hello"); |
| 569 | const int kCancelDelayUs = 10 * 1000; |
| 570 | request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs); |
| 571 | |
| 572 | ClientContext context; |
| 573 | std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_); |
| 574 | Status s = stub_->Echo(&context, request, &response); |
| 575 | cancel_thread.join(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 576 | EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 577 | EXPECT_EQ(s.error_message(), "Cancelled"); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // Server cancels rpc after 1ms |
| 581 | TEST_F(End2endTest, ServerCancelsRpc) { |
| 582 | ResetStub(); |
| 583 | EchoRequest request; |
| 584 | EchoResponse response; |
| 585 | request.set_message("Hello"); |
| 586 | request.mutable_param()->set_server_cancel_after_us(1000); |
| 587 | |
| 588 | ClientContext context; |
| 589 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 590 | EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 591 | EXPECT_TRUE(s.error_message().empty()); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 594 | // Client cancels request stream after sending two messages |
| 595 | TEST_F(End2endTest, ClientCancelsRequestStream) { |
| 596 | ResetStub(); |
| 597 | EchoRequest request; |
| 598 | EchoResponse response; |
| 599 | ClientContext context; |
| 600 | request.set_message("hello"); |
| 601 | |
| 602 | auto stream = stub_->RequestStream(&context, &response); |
| 603 | EXPECT_TRUE(stream->Write(request)); |
| 604 | EXPECT_TRUE(stream->Write(request)); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 605 | |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 606 | context.TryCancel(); |
| 607 | |
| 608 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 609 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 610 | |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 611 | EXPECT_EQ(response.message(), ""); |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 614 | // Client cancels server stream after sending some messages |
Abhishek Kumar | a1d3a72 | 2015-04-23 10:24:04 -0700 | [diff] [blame] | 615 | TEST_F(End2endTest, ClientCancelsResponseStream) { |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 616 | ResetStub(); |
| 617 | EchoRequest request; |
| 618 | EchoResponse response; |
| 619 | ClientContext context; |
| 620 | request.set_message("hello"); |
| 621 | |
| 622 | auto stream = stub_->ResponseStream(&context, request); |
| 623 | |
| 624 | EXPECT_TRUE(stream->Read(&response)); |
| 625 | EXPECT_EQ(response.message(), request.message() + "0"); |
| 626 | EXPECT_TRUE(stream->Read(&response)); |
| 627 | EXPECT_EQ(response.message(), request.message() + "1"); |
| 628 | |
| 629 | context.TryCancel(); |
| 630 | |
| 631 | // The cancellation races with responses, so there might be zero or |
| 632 | // one responses pending, read till failure |
| 633 | |
| 634 | if (stream->Read(&response)) { |
| 635 | EXPECT_EQ(response.message(), request.message() + "2"); |
| 636 | // Since we have cancelled, we expect the next attempt to read to fail |
| 637 | EXPECT_FALSE(stream->Read(&response)); |
| 638 | } |
| 639 | |
| 640 | Status s = stream->Finish(); |
Abhishek Kumar | 18298a7 | 2015-04-17 15:00:25 -0700 | [diff] [blame] | 641 | // The final status could be either of CANCELLED or OK depending on |
| 642 | // who won the race. |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 643 | EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code()); |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 646 | // Client cancels bidi stream after sending some messages |
| 647 | TEST_F(End2endTest, ClientCancelsBidi) { |
| 648 | ResetStub(); |
| 649 | EchoRequest request; |
| 650 | EchoResponse response; |
| 651 | ClientContext context; |
| 652 | grpc::string msg("hello"); |
| 653 | |
| 654 | auto stream = stub_->BidiStream(&context); |
| 655 | |
| 656 | request.set_message(msg + "0"); |
| 657 | EXPECT_TRUE(stream->Write(request)); |
| 658 | EXPECT_TRUE(stream->Read(&response)); |
| 659 | EXPECT_EQ(response.message(), request.message()); |
| 660 | |
| 661 | request.set_message(msg + "1"); |
| 662 | EXPECT_TRUE(stream->Write(request)); |
| 663 | |
| 664 | context.TryCancel(); |
| 665 | |
| 666 | // The cancellation races with responses, so there might be zero or |
| 667 | // one responses pending, read till failure |
| 668 | |
| 669 | if (stream->Read(&response)) { |
| 670 | EXPECT_EQ(response.message(), request.message()); |
| 671 | // Since we have cancelled, we expect the next attempt to read to fail |
| 672 | EXPECT_FALSE(stream->Read(&response)); |
| 673 | } |
| 674 | |
| 675 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 676 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 679 | TEST_F(End2endTest, RpcMaxMessageSize) { |
| 680 | ResetStub(); |
| 681 | EchoRequest request; |
| 682 | EchoResponse response; |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 683 | request.set_message(string(kMaxMessageSize_ * 2, 'a')); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 684 | |
| 685 | ClientContext context; |
| 686 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 687 | EXPECT_FALSE(s.ok()); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 688 | } |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 689 | |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 690 | bool MetadataContains(const std::multimap<grpc::string, grpc::string>& metadata, |
| 691 | const grpc::string& key, const grpc::string& value) { |
| 692 | int count = 0; |
| 693 | |
| 694 | for (std::multimap<grpc::string, grpc::string>::const_iterator iter = |
| 695 | metadata.begin(); |
| 696 | iter != metadata.end(); ++iter) { |
| 697 | if ((*iter).first == key && (*iter).second == value) { |
| 698 | count++; |
| 699 | } |
| 700 | } |
| 701 | return count == 1; |
| 702 | } |
| 703 | |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 704 | TEST_F(End2endTest, SetPerCallCredentials) { |
| 705 | ResetStub(); |
| 706 | EchoRequest request; |
| 707 | EchoResponse response; |
| 708 | ClientContext context; |
| 709 | std::shared_ptr<Credentials> creds = |
| 710 | IAMCredentials("fake_token", "fake_selector"); |
| 711 | context.set_credentials(creds); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 712 | request.set_message("Hello"); |
| 713 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 714 | |
| 715 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 716 | EXPECT_EQ(request.message(), response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 717 | EXPECT_TRUE(s.ok()); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 718 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 719 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 720 | "fake_token")); |
| 721 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 722 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 723 | "fake_selector")); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | TEST_F(End2endTest, InsecurePerCallCredentials) { |
| 727 | ResetStub(); |
| 728 | EchoRequest request; |
| 729 | EchoResponse response; |
| 730 | ClientContext context; |
| 731 | std::shared_ptr<Credentials> creds = InsecureCredentials(); |
| 732 | context.set_credentials(creds); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 733 | request.set_message("Hello"); |
| 734 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 735 | |
| 736 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 737 | EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 738 | EXPECT_EQ("Failed to set credentials to rpc.", s.error_message()); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | TEST_F(End2endTest, OverridePerCallCredentials) { |
| 742 | ResetStub(); |
| 743 | EchoRequest request; |
| 744 | EchoResponse response; |
| 745 | ClientContext context; |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 746 | std::shared_ptr<Credentials> creds1 = |
| 747 | IAMCredentials("fake_token1", "fake_selector1"); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 748 | context.set_credentials(creds1); |
| 749 | std::shared_ptr<Credentials> creds2 = |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 750 | IAMCredentials("fake_token2", "fake_selector2"); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 751 | context.set_credentials(creds2); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 752 | request.set_message("Hello"); |
| 753 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 754 | |
| 755 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 756 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 757 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 758 | "fake_token2")); |
| 759 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 760 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 761 | "fake_selector2")); |
Yang Gao | b57f72d | 2015-05-17 21:54:54 -0700 | [diff] [blame] | 762 | EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 763 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 764 | "fake_token1")); |
| 765 | EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 766 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 767 | "fake_selector1")); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 768 | EXPECT_EQ(request.message(), response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 769 | EXPECT_TRUE(s.ok()); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 770 | } |
| 771 | |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 772 | // Client sends 20 requests and the server returns CANCELLED status after |
| 773 | // reading 10 requests. |
| 774 | TEST_F(End2endTest, RequestStreamServerEarlyCancelTest) { |
| 775 | ResetStub(); |
| 776 | EchoRequest request; |
| 777 | EchoResponse response; |
| 778 | ClientContext context; |
| 779 | |
| 780 | context.AddMetadata(kServerCancelAfterReads, "10"); |
| 781 | auto stream = stub_->RequestStream(&context, &response); |
| 782 | request.set_message("hello"); |
| 783 | int send_messages = 20; |
| 784 | while (send_messages > 0) { |
| 785 | EXPECT_TRUE(stream->Write(request)); |
| 786 | send_messages--; |
| 787 | } |
| 788 | stream->WritesDone(); |
| 789 | Status s = stream->Finish(); |
| 790 | EXPECT_EQ(s.error_code(), StatusCode::CANCELLED); |
| 791 | } |
| 792 | |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 793 | TEST_F(End2endTest, ClientAuthContext) { |
| 794 | ResetStub(); |
| 795 | EchoRequest request; |
| 796 | EchoResponse response; |
| 797 | request.set_message("Hello"); |
| 798 | request.mutable_param()->set_check_auth_context(true); |
| 799 | |
| 800 | ClientContext context; |
| 801 | Status s = stub_->Echo(&context, request, &response); |
| 802 | EXPECT_EQ(response.message(), request.message()); |
| 803 | EXPECT_TRUE(s.ok()); |
| 804 | |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 805 | std::shared_ptr<const AuthContext> auth_ctx = context.auth_context(); |
| 806 | std::vector<grpc::string> ssl = |
| 807 | auth_ctx->FindPropertyValues("transport_security_type"); |
| 808 | EXPECT_EQ(1u, ssl.size()); |
| 809 | EXPECT_EQ("ssl", ssl[0]); |
| 810 | EXPECT_EQ("x509_subject_alternative_name", |
| 811 | auth_ctx->GetPeerIdentityPropertyName()); |
| 812 | EXPECT_EQ(3u, auth_ctx->GetPeerIdentity().size()); |
| 813 | EXPECT_EQ("*.test.google.fr", auth_ctx->GetPeerIdentity()[0]); |
| 814 | EXPECT_EQ("waterzooi.test.google.be", auth_ctx->GetPeerIdentity()[1]); |
| 815 | EXPECT_EQ("*.test.youtube.com", auth_ctx->GetPeerIdentity()[2]); |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 816 | } |
| 817 | |
yang-g | 6f30dec | 2015-07-22 23:11:56 -0700 | [diff] [blame] | 818 | // Make the response larger than the flow control window. |
| 819 | TEST_F(End2endTest, HugeResponse) { |
| 820 | ResetStub(); |
| 821 | EchoRequest request; |
| 822 | EchoResponse response; |
| 823 | request.set_message("huge response"); |
vjpai | 51d2275 | 2015-07-24 14:11:04 -0700 | [diff] [blame] | 824 | const size_t kResponseSize = 1024 * (1024 + 10); |
yang-g | 6f30dec | 2015-07-22 23:11:56 -0700 | [diff] [blame] | 825 | request.mutable_param()->set_response_message_length(kResponseSize); |
| 826 | |
| 827 | ClientContext context; |
| 828 | Status s = stub_->Echo(&context, request, &response); |
| 829 | EXPECT_EQ(kResponseSize, response.message().size()); |
| 830 | EXPECT_TRUE(s.ok()); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 831 | } |
| 832 | |
yang-g | 8ab3836 | 2015-07-31 14:05:33 -0700 | [diff] [blame] | 833 | namespace { |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 834 | void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream, |
| 835 | gpr_event *ev) { |
yang-g | 8ab3836 | 2015-07-31 14:05:33 -0700 | [diff] [blame] | 836 | EchoResponse resp; |
Craig Tiller | 7c1be05 | 2015-07-31 15:38:37 -0700 | [diff] [blame] | 837 | gpr_event_set(ev, (void*)1); |
yang-g | 8ab3836 | 2015-07-31 14:05:33 -0700 | [diff] [blame] | 838 | while (stream->Read(&resp)) { |
| 839 | gpr_log(GPR_INFO, "Read message"); |
| 840 | } |
| 841 | } |
| 842 | } // namespace |
| 843 | |
| 844 | // Run a Read and a WritesDone simultaneously. |
Craig Tiller | fb21ae6 | 2015-08-03 10:16:11 -0700 | [diff] [blame] | 845 | TEST_F(End2endTest, SimultaneousReadWritesDone) { |
yang-g | 8ab3836 | 2015-07-31 14:05:33 -0700 | [diff] [blame] | 846 | ResetStub(); |
| 847 | ClientContext context; |
Craig Tiller | 7c1be05 | 2015-07-31 15:38:37 -0700 | [diff] [blame] | 848 | gpr_event ev; |
| 849 | gpr_event_init(&ev); |
yang-g | 8ab3836 | 2015-07-31 14:05:33 -0700 | [diff] [blame] | 850 | auto stream = stub_->BidiStream(&context); |
Craig Tiller | 7c1be05 | 2015-07-31 15:38:37 -0700 | [diff] [blame] | 851 | std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev); |
| 852 | gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME)); |
yang-g | 8ab3836 | 2015-07-31 14:05:33 -0700 | [diff] [blame] | 853 | stream->WritesDone(); |
| 854 | Status s = stream->Finish(); |
| 855 | EXPECT_TRUE(s.ok()); |
| 856 | reader_thread.join(); |
| 857 | } |
| 858 | |
yang-g | d7ead69 | 2015-07-30 10:57:45 -0700 | [diff] [blame] | 859 | TEST_F(End2endTest, Peer) { |
| 860 | ResetStub(); |
| 861 | EchoRequest request; |
| 862 | EchoResponse response; |
| 863 | request.set_message("hello"); |
| 864 | request.mutable_param()->set_echo_peer(true); |
| 865 | |
| 866 | ClientContext context; |
| 867 | Status s = stub_->Echo(&context, request, &response); |
| 868 | EXPECT_EQ(response.message(), request.message()); |
| 869 | EXPECT_TRUE(s.ok()); |
| 870 | EXPECT_TRUE(CheckIsLocalhost(response.param().peer())); |
| 871 | EXPECT_TRUE(CheckIsLocalhost(context.peer())); |
| 872 | } |
| 873 | |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 874 | TEST_F(End2endTest, ChannelState) { |
| 875 | ResetStub(); |
| 876 | // Start IDLE |
| 877 | EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false)); |
| 878 | |
yang-g | 8708dd7 | 2015-08-05 15:57:14 -0700 | [diff] [blame^] | 879 | // Did not ask to connect, no state change. |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 880 | CompletionQueue cq; |
| 881 | std::chrono::system_clock::time_point deadline = |
| 882 | std::chrono::system_clock::now() + std::chrono::milliseconds(10); |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 883 | channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL); |
| 884 | void* tag; |
| 885 | bool ok = true; |
| 886 | cq.Next(&tag, &ok); |
| 887 | EXPECT_FALSE(ok); |
| 888 | |
| 889 | EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true)); |
| 890 | EXPECT_TRUE(channel_->WaitForStateChange( |
| 891 | GRPC_CHANNEL_IDLE, gpr_inf_future(GPR_CLOCK_REALTIME))); |
| 892 | EXPECT_EQ(GRPC_CHANNEL_CONNECTING, channel_->GetState(false)); |
yang-g | 8708dd7 | 2015-08-05 15:57:14 -0700 | [diff] [blame^] | 893 | EXPECT_TRUE(channel_->WaitForState(GRPC_CHANNEL_READY, |
| 894 | gpr_inf_future(GPR_CLOCK_REALTIME))); |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 897 | } // namespace testing |
| 898 | } // namespace grpc |
| 899 | |
| 900 | int main(int argc, char** argv) { |
| 901 | grpc_test_init(argc, argv); |
| 902 | ::testing::InitGoogleTest(&argc, argv); |
| 903 | return RUN_ALL_TESTS(); |
| 904 | } |