Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 34 | #include <chrono> |
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 | |
Craig Tiller | 14e60e9 | 2015-01-13 17:26:46 -0800 | [diff] [blame] | 37 | #include "test/core/util/test_config.h" |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 38 | #include "test/cpp/util/echo_duplicate.pb.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include "test/cpp/util/echo.pb.h" |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 40 | #include "src/cpp/util/time.h" |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 41 | #include <grpc++/channel_arguments.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <grpc++/channel_interface.h> |
| 43 | #include <grpc++/client_context.h> |
| 44 | #include <grpc++/create_channel.h> |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 45 | #include <grpc++/credentials.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <grpc++/server.h> |
| 47 | #include <grpc++/server_builder.h> |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 48 | #include <grpc++/server_context.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | #include <grpc++/status.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 50 | #include <grpc++/stream.h> |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 51 | #include "test/core/util/port.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | #include <gtest/gtest.h> |
| 53 | |
| 54 | #include <grpc/grpc.h> |
| 55 | #include <grpc/support/thd.h> |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 56 | #include <grpc/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | |
| 58 | using grpc::cpp::test::util::EchoRequest; |
| 59 | using grpc::cpp::test::util::EchoResponse; |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 60 | using std::chrono::system_clock; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | |
| 62 | namespace grpc { |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 63 | namespace testing { |
| 64 | |
| 65 | namespace { |
| 66 | |
| 67 | // When echo_deadline is requested, deadline seen in the ServerContext is set in |
| 68 | // the response in seconds. |
| 69 | void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request, |
| 70 | EchoResponse* response) { |
| 71 | if (request->has_param() && request->param().echo_deadline()) { |
| 72 | gpr_timespec deadline = gpr_inf_future; |
| 73 | if (context->absolute_deadline() != system_clock::time_point::max()) { |
| 74 | Timepoint2Timespec(context->absolute_deadline(), &deadline); |
| 75 | } |
| 76 | response->mutable_param()->set_request_deadline(deadline.tv_sec); |
| 77 | } |
| 78 | } |
| 79 | } // namespace |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 81 | class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | public: |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 83 | Status Echo(ServerContext* context, const EchoRequest* request, |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 84 | EchoResponse* response) override { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 85 | response->set_message(request->message()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 86 | MaybeEchoDeadline(context, request, response); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 87 | return Status::OK; |
| 88 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 89 | |
| 90 | // Unimplemented is left unimplemented to test the returned error. |
| 91 | |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 92 | Status RequestStream(ServerContext* context, |
| 93 | ServerReader<EchoRequest>* reader, |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 94 | EchoResponse* response) override { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 95 | EchoRequest request; |
| 96 | response->set_message(""); |
| 97 | while (reader->Read(&request)) { |
| 98 | response->mutable_message()->append(request.message()); |
| 99 | } |
| 100 | return Status::OK; |
| 101 | } |
| 102 | |
| 103 | // Return 3 messages. |
| 104 | // TODO(yangg) make it generic by adding a parameter into EchoRequest |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 105 | Status ResponseStream(ServerContext* context, const EchoRequest* request, |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 106 | ServerWriter<EchoResponse>* writer) override { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 107 | EchoResponse response; |
| 108 | response.set_message(request->message() + "0"); |
| 109 | writer->Write(response); |
| 110 | response.set_message(request->message() + "1"); |
| 111 | writer->Write(response); |
| 112 | response.set_message(request->message() + "2"); |
| 113 | writer->Write(response); |
| 114 | |
| 115 | return Status::OK; |
| 116 | } |
| 117 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 118 | Status BidiStream( |
| 119 | ServerContext* context, |
| 120 | ServerReaderWriter<EchoResponse, EchoRequest>* stream) override { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 121 | EchoRequest request; |
| 122 | EchoResponse response; |
| 123 | while (stream->Read(&request)) { |
| 124 | gpr_log(GPR_INFO, "recv msg %s", request.message().c_str()); |
| 125 | response.set_message(request.message()); |
| 126 | stream->Write(response); |
| 127 | } |
| 128 | return Status::OK; |
| 129 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 132 | class TestServiceImplDupPkg |
| 133 | : public ::grpc::cpp::test::util::duplicate::TestService::Service { |
| 134 | public: |
| 135 | Status Echo(ServerContext* context, const EchoRequest* request, |
| 136 | EchoResponse* response) override { |
| 137 | response->set_message("no package"); |
| 138 | return Status::OK; |
| 139 | } |
| 140 | }; |
| 141 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | class End2endTest : public ::testing::Test { |
| 143 | protected: |
| 144 | void SetUp() override { |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 145 | int port = grpc_pick_unused_port_or_die(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 146 | server_address_ << "localhost:" << port; |
| 147 | // Setup server |
| 148 | ServerBuilder builder; |
| 149 | builder.AddPort(server_address_.str()); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame^] | 150 | builder.RegisterService(&service_); |
| 151 | builder.RegisterService(&dup_pkg_service_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 152 | server_ = builder.BuildAndStart(); |
| 153 | } |
| 154 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 155 | void TearDown() override { server_->Shutdown(); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 157 | void ResetStub() { |
| 158 | std::shared_ptr<ChannelInterface> channel = |
| 159 | CreateChannel(server_address_.str(), ChannelArguments()); |
| 160 | stub_.reset(grpc::cpp::test::util::TestService::NewStub(channel)); |
| 161 | } |
| 162 | |
| 163 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 164 | std::unique_ptr<Server> server_; |
| 165 | std::ostringstream server_address_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 166 | TestServiceImpl service_; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 167 | TestServiceImplDupPkg dup_pkg_service_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 170 | static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub, |
| 171 | int num_rpcs) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 172 | EchoRequest request; |
| 173 | EchoResponse response; |
| 174 | request.set_message("Hello"); |
| 175 | |
| 176 | for (int i = 0; i < num_rpcs; ++i) { |
| 177 | ClientContext context; |
| 178 | Status s = stub->Echo(&context, request, &response); |
| 179 | EXPECT_EQ(response.message(), request.message()); |
| 180 | EXPECT_TRUE(s.IsOk()); |
| 181 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_F(End2endTest, SimpleRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 185 | ResetStub(); |
| 186 | SendRpc(stub_.get(), 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | TEST_F(End2endTest, MultipleRpcs) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 190 | ResetStub(); |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 191 | std::vector<std::thread*> threads; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 192 | for (int i = 0; i < 10; ++i) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 193 | threads.push_back(new std::thread(SendRpc, stub_.get(), 10)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | } |
| 195 | for (int i = 0; i < 10; ++i) { |
| 196 | threads[i]->join(); |
| 197 | delete threads[i]; |
| 198 | } |
| 199 | } |
| 200 | |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 201 | // Set a 10us deadline and make sure proper error is returned. |
| 202 | TEST_F(End2endTest, RpcDeadlineExpires) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 203 | ResetStub(); |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 204 | EchoRequest request; |
| 205 | EchoResponse response; |
| 206 | request.set_message("Hello"); |
| 207 | |
| 208 | ClientContext context; |
| 209 | std::chrono::system_clock::time_point deadline = |
| 210 | std::chrono::system_clock::now() + std::chrono::microseconds(10); |
| 211 | context.set_absolute_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 212 | Status s = stub_->Echo(&context, request, &response); |
Craig Tiller | 7b01878 | 2015-01-16 09:53:39 -0800 | [diff] [blame] | 213 | EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.code()); |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 214 | } |
| 215 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 216 | // Set a long but finite deadline. |
| 217 | TEST_F(End2endTest, RpcLongDeadline) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 218 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 219 | EchoRequest request; |
| 220 | EchoResponse response; |
| 221 | request.set_message("Hello"); |
| 222 | |
| 223 | ClientContext context; |
| 224 | std::chrono::system_clock::time_point deadline = |
| 225 | std::chrono::system_clock::now() + std::chrono::hours(1); |
| 226 | context.set_absolute_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 227 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 228 | EXPECT_EQ(response.message(), request.message()); |
| 229 | EXPECT_TRUE(s.IsOk()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Ask server to echo back the deadline it sees. |
| 233 | TEST_F(End2endTest, EchoDeadline) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 234 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 235 | EchoRequest request; |
| 236 | EchoResponse response; |
| 237 | request.set_message("Hello"); |
| 238 | request.mutable_param()->set_echo_deadline(true); |
| 239 | |
| 240 | ClientContext context; |
| 241 | std::chrono::system_clock::time_point deadline = |
| 242 | std::chrono::system_clock::now() + std::chrono::seconds(100); |
| 243 | context.set_absolute_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 244 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 245 | EXPECT_EQ(response.message(), request.message()); |
| 246 | EXPECT_TRUE(s.IsOk()); |
| 247 | gpr_timespec sent_deadline; |
| 248 | Timepoint2Timespec(deadline, &sent_deadline); |
| 249 | // Allow 1 second error. |
| 250 | EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1); |
| 251 | EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // Ask server to echo back the deadline it sees. The rpc has no deadline. |
| 255 | TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 256 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 257 | EchoRequest request; |
| 258 | EchoResponse response; |
| 259 | request.set_message("Hello"); |
| 260 | request.mutable_param()->set_echo_deadline(true); |
| 261 | |
| 262 | ClientContext context; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 263 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 264 | EXPECT_EQ(response.message(), request.message()); |
| 265 | EXPECT_TRUE(s.IsOk()); |
| 266 | EXPECT_EQ(response.param().request_deadline(), gpr_inf_future.tv_sec); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 267 | } |
| 268 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 269 | TEST_F(End2endTest, UnimplementedRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 270 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 271 | EchoRequest request; |
| 272 | EchoResponse response; |
| 273 | request.set_message("Hello"); |
| 274 | |
| 275 | ClientContext context; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 276 | Status s = stub_->Unimplemented(&context, request, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 277 | EXPECT_FALSE(s.IsOk()); |
| 278 | EXPECT_EQ(s.code(), grpc::StatusCode::UNIMPLEMENTED); |
| 279 | EXPECT_EQ(s.details(), ""); |
| 280 | EXPECT_EQ(response.message(), ""); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | TEST_F(End2endTest, RequestStreamOneRequest) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 284 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 285 | EchoRequest request; |
| 286 | EchoResponse response; |
| 287 | ClientContext context; |
| 288 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 289 | ClientWriter<EchoRequest>* stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 290 | request.set_message("hello"); |
| 291 | EXPECT_TRUE(stream->Write(request)); |
| 292 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame^] | 293 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 294 | EXPECT_EQ(response.message(), request.message()); |
| 295 | EXPECT_TRUE(s.IsOk()); |
| 296 | |
| 297 | delete stream; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | TEST_F(End2endTest, RequestStreamTwoRequests) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 301 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 302 | EchoRequest request; |
| 303 | EchoResponse response; |
| 304 | ClientContext context; |
| 305 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 306 | ClientWriter<EchoRequest>* stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 307 | request.set_message("hello"); |
| 308 | EXPECT_TRUE(stream->Write(request)); |
| 309 | EXPECT_TRUE(stream->Write(request)); |
| 310 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame^] | 311 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 312 | EXPECT_EQ(response.message(), "hellohello"); |
| 313 | EXPECT_TRUE(s.IsOk()); |
| 314 | |
| 315 | delete stream; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | TEST_F(End2endTest, ResponseStream) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 319 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 320 | EchoRequest request; |
| 321 | EchoResponse response; |
| 322 | ClientContext context; |
| 323 | request.set_message("hello"); |
| 324 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 325 | ClientReader<EchoResponse>* stream = |
| 326 | stub_->ResponseStream(&context, &request); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 327 | EXPECT_TRUE(stream->Read(&response)); |
| 328 | EXPECT_EQ(response.message(), request.message() + "0"); |
| 329 | EXPECT_TRUE(stream->Read(&response)); |
| 330 | EXPECT_EQ(response.message(), request.message() + "1"); |
| 331 | EXPECT_TRUE(stream->Read(&response)); |
| 332 | EXPECT_EQ(response.message(), request.message() + "2"); |
| 333 | EXPECT_FALSE(stream->Read(&response)); |
| 334 | |
| 335 | Status s = stream->Wait(); |
| 336 | EXPECT_TRUE(s.IsOk()); |
| 337 | |
| 338 | delete stream; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | TEST_F(End2endTest, BidiStream) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 342 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 343 | EchoRequest request; |
| 344 | EchoResponse response; |
| 345 | ClientContext context; |
| 346 | grpc::string msg("hello"); |
| 347 | |
| 348 | ClientReaderWriter<EchoRequest, EchoResponse>* stream = |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 349 | stub_->BidiStream(&context); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 350 | |
| 351 | request.set_message(msg + "0"); |
| 352 | EXPECT_TRUE(stream->Write(request)); |
| 353 | EXPECT_TRUE(stream->Read(&response)); |
| 354 | EXPECT_EQ(response.message(), request.message()); |
| 355 | |
| 356 | request.set_message(msg + "1"); |
| 357 | EXPECT_TRUE(stream->Write(request)); |
| 358 | EXPECT_TRUE(stream->Read(&response)); |
| 359 | EXPECT_EQ(response.message(), request.message()); |
| 360 | |
| 361 | request.set_message(msg + "2"); |
| 362 | EXPECT_TRUE(stream->Write(request)); |
| 363 | EXPECT_TRUE(stream->Read(&response)); |
| 364 | EXPECT_EQ(response.message(), request.message()); |
| 365 | |
| 366 | stream->WritesDone(); |
| 367 | EXPECT_FALSE(stream->Read(&response)); |
| 368 | |
| 369 | Status s = stream->Wait(); |
| 370 | EXPECT_TRUE(s.IsOk()); |
| 371 | |
| 372 | delete stream; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // Talk to the two services with the same name but different package names. |
| 376 | // The two stubs are created on the same channel. |
| 377 | TEST_F(End2endTest, DiffPackageServices) { |
| 378 | std::shared_ptr<ChannelInterface> channel = |
| 379 | CreateChannel(server_address_.str(), ChannelArguments()); |
| 380 | |
| 381 | EchoRequest request; |
| 382 | EchoResponse response; |
| 383 | request.set_message("Hello"); |
| 384 | |
| 385 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub( |
| 386 | grpc::cpp::test::util::TestService::NewStub(channel)); |
| 387 | ClientContext context; |
| 388 | Status s = stub->Echo(&context, request, &response); |
| 389 | EXPECT_EQ(response.message(), request.message()); |
| 390 | EXPECT_TRUE(s.IsOk()); |
| 391 | |
| 392 | std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub> |
| 393 | dup_pkg_stub( |
| 394 | grpc::cpp::test::util::duplicate::TestService::NewStub(channel)); |
| 395 | ClientContext context2; |
| 396 | s = dup_pkg_stub->Echo(&context2, request, &response); |
| 397 | EXPECT_EQ("no package", response.message()); |
| 398 | EXPECT_TRUE(s.IsOk()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 399 | } |
| 400 | |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 401 | // rpc and stream should fail on bad credentials. |
| 402 | TEST_F(End2endTest, BadCredentials) { |
| 403 | std::unique_ptr<Credentials> bad_creds = |
| 404 | CredentialsFactory::ServiceAccountCredentials("", "", |
| 405 | std::chrono::seconds(1)); |
| 406 | EXPECT_EQ(nullptr, bad_creds.get()); |
| 407 | std::shared_ptr<ChannelInterface> channel = |
| 408 | CreateChannel(server_address_.str(), bad_creds, ChannelArguments()); |
| 409 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub( |
| 410 | grpc::cpp::test::util::TestService::NewStub(channel)); |
| 411 | EchoRequest request; |
| 412 | EchoResponse response; |
| 413 | ClientContext context; |
| 414 | grpc::string msg("hello"); |
| 415 | |
| 416 | Status s = stub->Echo(&context, request, &response); |
| 417 | EXPECT_EQ("", response.message()); |
| 418 | EXPECT_FALSE(s.IsOk()); |
| 419 | EXPECT_EQ(StatusCode::UNKNOWN, s.code()); |
| 420 | EXPECT_EQ("Rpc sent on a lame channel.", s.details()); |
| 421 | |
| 422 | ClientContext context2; |
| 423 | ClientReaderWriter<EchoRequest, EchoResponse>* stream = |
| 424 | stub->BidiStream(&context2); |
| 425 | s = stream->Wait(); |
| 426 | EXPECT_FALSE(s.IsOk()); |
| 427 | EXPECT_EQ(StatusCode::UNKNOWN, s.code()); |
| 428 | EXPECT_EQ("Rpc sent on a lame channel.", s.details()); |
| 429 | |
| 430 | delete stream; |
| 431 | } |
| 432 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 433 | } // namespace testing |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 434 | } // namespace grpc |
| 435 | |
| 436 | int main(int argc, char** argv) { |
Craig Tiller | 14e60e9 | 2015-01-13 17:26:46 -0800 | [diff] [blame] | 437 | grpc_test_init(argc, argv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 438 | grpc_init(); |
| 439 | ::testing::InitGoogleTest(&argc, argv); |
| 440 | int result = RUN_ALL_TESTS(); |
| 441 | grpc_shutdown(); |
| 442 | return result; |
| 443 | } |