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