blob: 76271c3e1dd934879efb8270db3c172f7812692c [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <thread>
yangged5e7e02015-01-06 10:16:15 -080035
Yang Gao26a49122015-05-15 17:02:56 -070036#include "src/core/security/credentials.h"
37#include "src/cpp/server/thread_pool.h"
Nicolas Noble89219162015-04-07 18:01:18 -070038#include "test/core/util/port.h"
Craig Tiller14e60e92015-01-13 17:26:46 -080039#include "test/core/util/test_config.h"
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020040#include "test/cpp/util/echo_duplicate.grpc.pb.h"
41#include "test/cpp/util/echo.grpc.pb.h"
Yang Gao26a49122015-05-15 17:02:56 -070042#include "test/cpp/util/fake_credentials.h"
yangg59dfc902014-12-19 14:00:14 -080043#include <grpc++/channel_arguments.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044#include <grpc++/channel_interface.h>
45#include <grpc++/client_context.h>
46#include <grpc++/create_channel.h>
yangg4105e2b2015-01-09 14:19:44 -080047#include <grpc++/credentials.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048#include <grpc++/server.h>
49#include <grpc++/server_builder.h>
yangga4b6f5d2014-12-17 15:53:12 -080050#include <grpc++/server_context.h>
Craig Tiller42bc87c2015-02-23 08:50:19 -080051#include <grpc++/server_credentials.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052#include <grpc++/status.h>
nnoble0c475f02014-12-05 15:37:39 -080053#include <grpc++/stream.h>
Nicolas Noble89219162015-04-07 18:01:18 -070054#include <grpc++/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055#include <gtest/gtest.h>
56
57#include <grpc/grpc.h>
58#include <grpc/support/thd.h>
yangged5e7e02015-01-06 10:16:15 -080059#include <grpc/support/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060
61using grpc::cpp::test::util::EchoRequest;
62using grpc::cpp::test::util::EchoResponse;
yangged5e7e02015-01-06 10:16:15 -080063using std::chrono::system_clock;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064
65namespace grpc {
yangged5e7e02015-01-06 10:16:15 -080066namespace testing {
67
68namespace {
69
70// When echo_deadline is requested, deadline seen in the ServerContext is set in
71// the response in seconds.
72void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request,
73 EchoResponse* response) {
74 if (request->has_param() && request->param().echo_deadline()) {
75 gpr_timespec deadline = gpr_inf_future;
Nicolas Noble89219162015-04-07 18:01:18 -070076 if (context->deadline() != system_clock::time_point::max()) {
77 Timepoint2Timespec(context->deadline(), &deadline);
yangged5e7e02015-01-06 10:16:15 -080078 }
79 response->mutable_param()->set_request_deadline(deadline.tv_sec);
80 }
81}
Craig Tiller7418d012015-02-11 15:25:03 -080082
yangged5e7e02015-01-06 10:16:15 -080083} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084
yangg1456d152015-01-08 15:39:58 -080085class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086 public:
Yang Gao0c4b0dd2015-03-30 13:08:34 -070087 TestServiceImpl() : signal_client_(false) {}
88
yangga4b6f5d2014-12-17 15:53:12 -080089 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -080090 EchoResponse* response) GRPC_OVERRIDE {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091 response->set_message(request->message());
yangged5e7e02015-01-06 10:16:15 -080092 MaybeEchoDeadline(context, request, response);
Yang Gao0c4b0dd2015-03-30 13:08:34 -070093 if (request->has_param() && request->param().client_cancel_after_us()) {
94 {
95 std::unique_lock<std::mutex> lock(mu_);
96 signal_client_ = true;
97 }
98 while (!context->IsCancelled()) {
99 std::this_thread::sleep_for(std::chrono::microseconds(
100 request->param().client_cancel_after_us()));
101 }
102 return Status::Cancelled;
103 } else if (request->has_param() &&
104 request->param().server_cancel_after_us()) {
105 std::this_thread::sleep_for(
106 std::chrono::microseconds(request->param().server_cancel_after_us()));
107 return Status::Cancelled;
108 } else {
109 EXPECT_FALSE(context->IsCancelled());
110 }
Yang Gao26a49122015-05-15 17:02:56 -0700111
112 if (request->has_param() && request->param().echo_metadata()) {
113 const std::multimap<grpc::string, grpc::string>& client_metadata =
114 context->client_metadata();
115 for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
116 client_metadata.begin();
117 iter != client_metadata.end(); ++iter) {
118 context->AddTrailingMetadata((*iter).first, (*iter).second);
119 }
120 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121 return Status::OK;
122 }
nnoble0c475f02014-12-05 15:37:39 -0800123
124 // Unimplemented is left unimplemented to test the returned error.
125
yangga4b6f5d2014-12-17 15:53:12 -0800126 Status RequestStream(ServerContext* context,
127 ServerReader<EchoRequest>* reader,
Craig Tillercf133f42015-02-26 14:05:56 -0800128 EchoResponse* response) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800129 EchoRequest request;
130 response->set_message("");
131 while (reader->Read(&request)) {
132 response->mutable_message()->append(request.message());
133 }
134 return Status::OK;
135 }
136
137 // Return 3 messages.
138 // TODO(yangg) make it generic by adding a parameter into EchoRequest
yangga4b6f5d2014-12-17 15:53:12 -0800139 Status ResponseStream(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800140 ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800141 EchoResponse response;
142 response.set_message(request->message() + "0");
143 writer->Write(response);
144 response.set_message(request->message() + "1");
145 writer->Write(response);
146 response.set_message(request->message() + "2");
147 writer->Write(response);
148
149 return Status::OK;
150 }
151
Craig Tillercf133f42015-02-26 14:05:56 -0800152 Status BidiStream(ServerContext* context,
153 ServerReaderWriter<EchoResponse, EchoRequest>* stream)
154 GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800155 EchoRequest request;
156 EchoResponse response;
157 while (stream->Read(&request)) {
158 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
159 response.set_message(request.message());
160 stream->Write(response);
161 }
162 return Status::OK;
163 }
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700164
165 bool signal_client() {
166 std::unique_lock<std::mutex> lock(mu_);
167 return signal_client_;
168 }
169
170 private:
171 bool signal_client_;
172 std::mutex mu_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800173};
174
yangg1456d152015-01-08 15:39:58 -0800175class TestServiceImplDupPkg
176 : public ::grpc::cpp::test::util::duplicate::TestService::Service {
177 public:
178 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800179 EchoResponse* response) GRPC_OVERRIDE {
yangg1456d152015-01-08 15:39:58 -0800180 response->set_message("no package");
181 return Status::OK;
182 }
183};
184
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185class End2endTest : public ::testing::Test {
186 protected:
Yang Gao3921c562015-04-30 16:07:06 -0700187 End2endTest() : kMaxMessageSize_(8192), thread_pool_(2) {}
Craig Tiller7418d012015-02-11 15:25:03 -0800188
Craig Tillercf133f42015-02-26 14:05:56 -0800189 void SetUp() GRPC_OVERRIDE {
Craig Tiller35e39712015-01-12 16:41:24 -0800190 int port = grpc_pick_unused_port_or_die();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800191 server_address_ << "localhost:" << port;
192 // Setup server
193 ServerBuilder builder;
Yang Gaob57f72d2015-05-17 21:54:54 -0700194 builder.AddListeningPort(server_address_.str(),
195 FakeTransportSecurityServerCredentials());
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800196 builder.RegisterService(&service_);
Yang Gaoc71a9d22015-05-04 00:22:12 -0700197 builder.SetMaxMessageSize(
198 kMaxMessageSize_); // For testing max message size.
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800199 builder.RegisterService(&dup_pkg_service_);
Craig Tiller7418d012015-02-11 15:25:03 -0800200 builder.SetThreadPool(&thread_pool_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201 server_ = builder.BuildAndStart();
202 }
203
Craig Tillercf133f42015-02-26 14:05:56 -0800204 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205
yangg1456d152015-01-08 15:39:58 -0800206 void ResetStub() {
Yang Gaob57f72d2015-05-17 21:54:54 -0700207 std::shared_ptr<ChannelInterface> channel =
208 CreateChannel(server_address_.str(), FakeTransportSecurityCredentials(),
209 ChannelArguments());
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800210 stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel));
yangg1456d152015-01-08 15:39:58 -0800211 }
212
213 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800214 std::unique_ptr<Server> server_;
215 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700216 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800217 TestServiceImpl service_;
yangg1456d152015-01-08 15:39:58 -0800218 TestServiceImplDupPkg dup_pkg_service_;
Craig Tiller7418d012015-02-11 15:25:03 -0800219 ThreadPool thread_pool_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800220};
221
yangg1456d152015-01-08 15:39:58 -0800222static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub,
223 int num_rpcs) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224 EchoRequest request;
225 EchoResponse response;
226 request.set_message("Hello");
227
228 for (int i = 0; i < num_rpcs; ++i) {
229 ClientContext context;
230 Status s = stub->Echo(&context, request, &response);
231 EXPECT_EQ(response.message(), request.message());
232 EXPECT_TRUE(s.IsOk());
233 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800234}
235
236TEST_F(End2endTest, SimpleRpc) {
yangg1456d152015-01-08 15:39:58 -0800237 ResetStub();
238 SendRpc(stub_.get(), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239}
240
241TEST_F(End2endTest, MultipleRpcs) {
yangg1456d152015-01-08 15:39:58 -0800242 ResetStub();
Craig Tiller35e39712015-01-12 16:41:24 -0800243 std::vector<std::thread*> threads;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800244 for (int i = 0; i < 10; ++i) {
yangg1456d152015-01-08 15:39:58 -0800245 threads.push_back(new std::thread(SendRpc, stub_.get(), 10));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246 }
247 for (int i = 0; i < 10; ++i) {
248 threads[i]->join();
249 delete threads[i];
250 }
251}
252
yanggda029e32014-12-18 10:24:04 -0800253// Set a 10us deadline and make sure proper error is returned.
254TEST_F(End2endTest, RpcDeadlineExpires) {
yangg1456d152015-01-08 15:39:58 -0800255 ResetStub();
yanggda029e32014-12-18 10:24:04 -0800256 EchoRequest request;
257 EchoResponse response;
258 request.set_message("Hello");
259
260 ClientContext context;
261 std::chrono::system_clock::time_point deadline =
262 std::chrono::system_clock::now() + std::chrono::microseconds(10);
Nicolas Noble89219162015-04-07 18:01:18 -0700263 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800264 Status s = stub_->Echo(&context, request, &response);
Craig Tiller7b018782015-01-16 09:53:39 -0800265 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.code());
yanggda029e32014-12-18 10:24:04 -0800266}
267
yangged5e7e02015-01-06 10:16:15 -0800268// Set a long but finite deadline.
269TEST_F(End2endTest, RpcLongDeadline) {
yangg1456d152015-01-08 15:39:58 -0800270 ResetStub();
yangged5e7e02015-01-06 10:16:15 -0800271 EchoRequest request;
272 EchoResponse response;
273 request.set_message("Hello");
274
275 ClientContext context;
276 std::chrono::system_clock::time_point deadline =
277 std::chrono::system_clock::now() + std::chrono::hours(1);
Nicolas Noble89219162015-04-07 18:01:18 -0700278 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800279 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800280 EXPECT_EQ(response.message(), request.message());
281 EXPECT_TRUE(s.IsOk());
yangged5e7e02015-01-06 10:16:15 -0800282}
283
284// Ask server to echo back the deadline it sees.
285TEST_F(End2endTest, EchoDeadline) {
yangg1456d152015-01-08 15:39:58 -0800286 ResetStub();
yangged5e7e02015-01-06 10:16:15 -0800287 EchoRequest request;
288 EchoResponse response;
289 request.set_message("Hello");
290 request.mutable_param()->set_echo_deadline(true);
291
292 ClientContext context;
293 std::chrono::system_clock::time_point deadline =
294 std::chrono::system_clock::now() + std::chrono::seconds(100);
Nicolas Noble89219162015-04-07 18:01:18 -0700295 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800296 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800297 EXPECT_EQ(response.message(), request.message());
298 EXPECT_TRUE(s.IsOk());
299 gpr_timespec sent_deadline;
300 Timepoint2Timespec(deadline, &sent_deadline);
301 // Allow 1 second error.
302 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
303 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
yangged5e7e02015-01-06 10:16:15 -0800304}
305
306// Ask server to echo back the deadline it sees. The rpc has no deadline.
307TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) {
yangg1456d152015-01-08 15:39:58 -0800308 ResetStub();
yangged5e7e02015-01-06 10:16:15 -0800309 EchoRequest request;
310 EchoResponse response;
311 request.set_message("Hello");
312 request.mutable_param()->set_echo_deadline(true);
313
314 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800315 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800316 EXPECT_EQ(response.message(), request.message());
317 EXPECT_TRUE(s.IsOk());
318 EXPECT_EQ(response.param().request_deadline(), gpr_inf_future.tv_sec);
yangged5e7e02015-01-06 10:16:15 -0800319}
320
nnoble0c475f02014-12-05 15:37:39 -0800321TEST_F(End2endTest, UnimplementedRpc) {
yangg1456d152015-01-08 15:39:58 -0800322 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800323 EchoRequest request;
324 EchoResponse response;
325 request.set_message("Hello");
326
327 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800328 Status s = stub_->Unimplemented(&context, request, &response);
nnoble0c475f02014-12-05 15:37:39 -0800329 EXPECT_FALSE(s.IsOk());
330 EXPECT_EQ(s.code(), grpc::StatusCode::UNIMPLEMENTED);
331 EXPECT_EQ(s.details(), "");
332 EXPECT_EQ(response.message(), "");
nnoble0c475f02014-12-05 15:37:39 -0800333}
334
335TEST_F(End2endTest, RequestStreamOneRequest) {
yangg1456d152015-01-08 15:39:58 -0800336 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800337 EchoRequest request;
338 EchoResponse response;
339 ClientContext context;
340
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800341 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800342 request.set_message("hello");
343 EXPECT_TRUE(stream->Write(request));
344 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800345 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800346 EXPECT_EQ(response.message(), request.message());
347 EXPECT_TRUE(s.IsOk());
nnoble0c475f02014-12-05 15:37:39 -0800348}
349
350TEST_F(End2endTest, RequestStreamTwoRequests) {
yangg1456d152015-01-08 15:39:58 -0800351 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800352 EchoRequest request;
353 EchoResponse response;
354 ClientContext context;
355
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800356 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800357 request.set_message("hello");
358 EXPECT_TRUE(stream->Write(request));
359 EXPECT_TRUE(stream->Write(request));
360 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800361 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800362 EXPECT_EQ(response.message(), "hellohello");
363 EXPECT_TRUE(s.IsOk());
nnoble0c475f02014-12-05 15:37:39 -0800364}
365
366TEST_F(End2endTest, ResponseStream) {
yangg1456d152015-01-08 15:39:58 -0800367 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800368 EchoRequest request;
369 EchoResponse response;
370 ClientContext context;
371 request.set_message("hello");
372
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800373 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800374 EXPECT_TRUE(stream->Read(&response));
375 EXPECT_EQ(response.message(), request.message() + "0");
376 EXPECT_TRUE(stream->Read(&response));
377 EXPECT_EQ(response.message(), request.message() + "1");
378 EXPECT_TRUE(stream->Read(&response));
379 EXPECT_EQ(response.message(), request.message() + "2");
380 EXPECT_FALSE(stream->Read(&response));
381
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800382 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800383 EXPECT_TRUE(s.IsOk());
nnoble0c475f02014-12-05 15:37:39 -0800384}
385
386TEST_F(End2endTest, BidiStream) {
yangg1456d152015-01-08 15:39:58 -0800387 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800388 EchoRequest request;
389 EchoResponse response;
390 ClientContext context;
391 grpc::string msg("hello");
392
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800393 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800394
395 request.set_message(msg + "0");
396 EXPECT_TRUE(stream->Write(request));
397 EXPECT_TRUE(stream->Read(&response));
398 EXPECT_EQ(response.message(), request.message());
399
400 request.set_message(msg + "1");
401 EXPECT_TRUE(stream->Write(request));
402 EXPECT_TRUE(stream->Read(&response));
403 EXPECT_EQ(response.message(), request.message());
404
405 request.set_message(msg + "2");
406 EXPECT_TRUE(stream->Write(request));
407 EXPECT_TRUE(stream->Read(&response));
408 EXPECT_EQ(response.message(), request.message());
409
410 stream->WritesDone();
411 EXPECT_FALSE(stream->Read(&response));
412
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800413 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800414 EXPECT_TRUE(s.IsOk());
yangg1456d152015-01-08 15:39:58 -0800415}
416
417// Talk to the two services with the same name but different package names.
418// The two stubs are created on the same channel.
419TEST_F(End2endTest, DiffPackageServices) {
Yang Gaob57f72d2015-05-17 21:54:54 -0700420 std::shared_ptr<ChannelInterface> channel =
421 CreateChannel(server_address_.str(), FakeTransportSecurityCredentials(),
422 ChannelArguments());
yangg1456d152015-01-08 15:39:58 -0800423
424 EchoRequest request;
425 EchoResponse response;
426 request.set_message("Hello");
427
428 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub(
429 grpc::cpp::test::util::TestService::NewStub(channel));
430 ClientContext context;
431 Status s = stub->Echo(&context, request, &response);
432 EXPECT_EQ(response.message(), request.message());
433 EXPECT_TRUE(s.IsOk());
434
435 std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub>
436 dup_pkg_stub(
437 grpc::cpp::test::util::duplicate::TestService::NewStub(channel));
438 ClientContext context2;
439 s = dup_pkg_stub->Echo(&context2, request, &response);
440 EXPECT_EQ("no package", response.message());
441 EXPECT_TRUE(s.IsOk());
nnoble0c475f02014-12-05 15:37:39 -0800442}
443
yangg4105e2b2015-01-09 14:19:44 -0800444// rpc and stream should fail on bad credentials.
445TEST_F(End2endTest, BadCredentials) {
Yang Gaoa8938922015-05-14 11:51:07 -0700446 std::shared_ptr<Credentials> bad_creds = ServiceAccountCredentials("", "", 1);
yangg4105e2b2015-01-09 14:19:44 -0800447 EXPECT_EQ(nullptr, bad_creds.get());
448 std::shared_ptr<ChannelInterface> channel =
449 CreateChannel(server_address_.str(), bad_creds, ChannelArguments());
450 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub(
451 grpc::cpp::test::util::TestService::NewStub(channel));
452 EchoRequest request;
453 EchoResponse response;
454 ClientContext context;
Yang Gao26a49122015-05-15 17:02:56 -0700455 request.set_message("Hello");
yangg4105e2b2015-01-09 14:19:44 -0800456
457 Status s = stub->Echo(&context, request, &response);
458 EXPECT_EQ("", response.message());
459 EXPECT_FALSE(s.IsOk());
460 EXPECT_EQ(StatusCode::UNKNOWN, s.code());
461 EXPECT_EQ("Rpc sent on a lame channel.", s.details());
462
463 ClientContext context2;
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800464 auto stream = stub->BidiStream(&context2);
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800465 s = stream->Finish();
yangg4105e2b2015-01-09 14:19:44 -0800466 EXPECT_FALSE(s.IsOk());
467 EXPECT_EQ(StatusCode::UNKNOWN, s.code());
468 EXPECT_EQ("Rpc sent on a lame channel.", s.details());
yangg4105e2b2015-01-09 14:19:44 -0800469}
470
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700471void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
472 std::this_thread::sleep_for(std::chrono::microseconds(delay_us));
473 while (!service->signal_client()) {
474 }
475 context->TryCancel();
476}
477
478// Client cancels rpc after 10ms
479TEST_F(End2endTest, ClientCancelsRpc) {
480 ResetStub();
481 EchoRequest request;
482 EchoResponse response;
483 request.set_message("Hello");
484 const int kCancelDelayUs = 10 * 1000;
485 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
486
487 ClientContext context;
488 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
489 Status s = stub_->Echo(&context, request, &response);
490 cancel_thread.join();
491 EXPECT_EQ(StatusCode::CANCELLED, s.code());
Craig Tiller12088b22015-04-20 09:51:27 -0700492 EXPECT_EQ(s.details(), "Cancelled");
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700493}
494
495// Server cancels rpc after 1ms
496TEST_F(End2endTest, ServerCancelsRpc) {
497 ResetStub();
498 EchoRequest request;
499 EchoResponse response;
500 request.set_message("Hello");
501 request.mutable_param()->set_server_cancel_after_us(1000);
502
503 ClientContext context;
504 Status s = stub_->Echo(&context, request, &response);
505 EXPECT_EQ(StatusCode::CANCELLED, s.code());
506 EXPECT_TRUE(s.details().empty());
507}
508
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700509// Client cancels request stream after sending two messages
510TEST_F(End2endTest, ClientCancelsRequestStream) {
511 ResetStub();
512 EchoRequest request;
513 EchoResponse response;
514 ClientContext context;
515 request.set_message("hello");
516
517 auto stream = stub_->RequestStream(&context, &response);
518 EXPECT_TRUE(stream->Write(request));
519 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700520
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700521 context.TryCancel();
522
523 Status s = stream->Finish();
524 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700525
Yang Gaoc71a9d22015-05-04 00:22:12 -0700526 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700527}
528
Abhishek Kumare41d0402015-04-17 14:12:33 -0700529// Client cancels server stream after sending some messages
Abhishek Kumara1d3a722015-04-23 10:24:04 -0700530TEST_F(End2endTest, ClientCancelsResponseStream) {
Abhishek Kumare41d0402015-04-17 14:12:33 -0700531 ResetStub();
532 EchoRequest request;
533 EchoResponse response;
534 ClientContext context;
535 request.set_message("hello");
536
537 auto stream = stub_->ResponseStream(&context, request);
538
539 EXPECT_TRUE(stream->Read(&response));
540 EXPECT_EQ(response.message(), request.message() + "0");
541 EXPECT_TRUE(stream->Read(&response));
542 EXPECT_EQ(response.message(), request.message() + "1");
543
544 context.TryCancel();
545
546 // The cancellation races with responses, so there might be zero or
547 // one responses pending, read till failure
548
549 if (stream->Read(&response)) {
550 EXPECT_EQ(response.message(), request.message() + "2");
551 // Since we have cancelled, we expect the next attempt to read to fail
552 EXPECT_FALSE(stream->Read(&response));
553 }
554
555 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700556 // The final status could be either of CANCELLED or OK depending on
557 // who won the race.
558 EXPECT_GE(grpc::StatusCode::CANCELLED, s.code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700559}
560
Abhishek Kumar82a83312015-04-17 13:30:51 -0700561// Client cancels bidi stream after sending some messages
562TEST_F(End2endTest, ClientCancelsBidi) {
563 ResetStub();
564 EchoRequest request;
565 EchoResponse response;
566 ClientContext context;
567 grpc::string msg("hello");
568
569 auto stream = stub_->BidiStream(&context);
570
571 request.set_message(msg + "0");
572 EXPECT_TRUE(stream->Write(request));
573 EXPECT_TRUE(stream->Read(&response));
574 EXPECT_EQ(response.message(), request.message());
575
576 request.set_message(msg + "1");
577 EXPECT_TRUE(stream->Write(request));
578
579 context.TryCancel();
580
581 // The cancellation races with responses, so there might be zero or
582 // one responses pending, read till failure
583
584 if (stream->Read(&response)) {
585 EXPECT_EQ(response.message(), request.message());
586 // Since we have cancelled, we expect the next attempt to read to fail
587 EXPECT_FALSE(stream->Read(&response));
588 }
589
590 Status s = stream->Finish();
591 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.code());
592}
593
Yang Gao3921c562015-04-30 16:07:06 -0700594TEST_F(End2endTest, RpcMaxMessageSize) {
595 ResetStub();
596 EchoRequest request;
597 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700598 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700599
600 ClientContext context;
601 Status s = stub_->Echo(&context, request, &response);
602 EXPECT_FALSE(s.IsOk());
603}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700604
Yang Gao26a49122015-05-15 17:02:56 -0700605bool MetadataContains(const std::multimap<grpc::string, grpc::string>& metadata,
606 const grpc::string& key, const grpc::string& value) {
607 int count = 0;
608
609 for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
610 metadata.begin();
611 iter != metadata.end(); ++iter) {
612 if ((*iter).first == key && (*iter).second == value) {
613 count++;
614 }
615 }
616 return count == 1;
617}
618
Yang Gaoa8938922015-05-14 11:51:07 -0700619TEST_F(End2endTest, SetPerCallCredentials) {
620 ResetStub();
621 EchoRequest request;
622 EchoResponse response;
623 ClientContext context;
624 std::shared_ptr<Credentials> creds =
625 IAMCredentials("fake_token", "fake_selector");
626 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -0700627 request.set_message("Hello");
628 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700629
630 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -0700631 EXPECT_EQ(request.message(), response.message());
632 EXPECT_TRUE(s.IsOk());
Yang Gao26a49122015-05-15 17:02:56 -0700633 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
634 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
635 "fake_token"));
636 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
637 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
638 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -0700639}
640
641TEST_F(End2endTest, InsecurePerCallCredentials) {
642 ResetStub();
643 EchoRequest request;
644 EchoResponse response;
645 ClientContext context;
646 std::shared_ptr<Credentials> creds = InsecureCredentials();
647 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -0700648 request.set_message("Hello");
649 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700650
651 Status s = stub_->Echo(&context, request, &response);
Yang Gao280ca172015-05-15 10:25:10 -0700652 EXPECT_EQ(StatusCode::CANCELLED, s.code());
653 EXPECT_EQ("Failed to set credentials to rpc.", s.details());
Yang Gaoa8938922015-05-14 11:51:07 -0700654}
655
656TEST_F(End2endTest, OverridePerCallCredentials) {
657 ResetStub();
658 EchoRequest request;
659 EchoResponse response;
660 ClientContext context;
Yang Gao26a49122015-05-15 17:02:56 -0700661 std::shared_ptr<Credentials> creds1 =
662 IAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -0700663 context.set_credentials(creds1);
664 std::shared_ptr<Credentials> creds2 =
Yang Gao26a49122015-05-15 17:02:56 -0700665 IAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -0700666 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -0700667 request.set_message("Hello");
668 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700669
670 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -0700671 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
672 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
673 "fake_token2"));
674 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
675 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
676 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -0700677 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
678 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
679 "fake_token1"));
680 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
681 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
682 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -0700683 EXPECT_EQ(request.message(), response.message());
684 EXPECT_TRUE(s.IsOk());
685}
686
yangged5e7e02015-01-06 10:16:15 -0800687} // namespace testing
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800688} // namespace grpc
689
690int main(int argc, char** argv) {
Craig Tiller14e60e92015-01-13 17:26:46 -0800691 grpc_test_init(argc, argv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800692 ::testing::InitGoogleTest(&argc, argv);
Yang Gaoc4b6ffb2015-04-23 16:35:24 -0700693 return RUN_ALL_TESTS();
Craig Tiller190d3602015-02-18 09:23:38 -0800694}