blob: f63854ccf315e0ae96fae5c1bf54fde366bcedef [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
yangged5e7e02015-01-06 10:16:15 -080034#include <chrono>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035#include <thread>
yangged5e7e02015-01-06 10:16:15 -080036
Craig Tiller14e60e92015-01-13 17:26:46 -080037#include "test/core/util/test_config.h"
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020038#include "test/cpp/util/echo_duplicate.grpc.pb.h"
39#include "test/cpp/util/echo.grpc.pb.h"
yangged5e7e02015-01-06 10:16:15 -080040#include "src/cpp/util/time.h"
Craig Tiller7418d012015-02-11 15:25:03 -080041#include "src/cpp/server/thread_pool.h"
yangg59dfc902014-12-19 14:00:14 -080042#include <grpc++/channel_arguments.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#include <grpc++/channel_interface.h>
44#include <grpc++/client_context.h>
45#include <grpc++/create_channel.h>
yangg4105e2b2015-01-09 14:19:44 -080046#include <grpc++/credentials.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047#include <grpc++/server.h>
48#include <grpc++/server_builder.h>
yangga4b6f5d2014-12-17 15:53:12 -080049#include <grpc++/server_context.h>
Craig Tiller42bc87c2015-02-23 08:50:19 -080050#include <grpc++/server_credentials.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051#include <grpc++/status.h>
nnoble0c475f02014-12-05 15:37:39 -080052#include <grpc++/stream.h>
Craig Tiller35e39712015-01-12 16:41:24 -080053#include "test/core/util/port.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054#include <gtest/gtest.h>
55
56#include <grpc/grpc.h>
57#include <grpc/support/thd.h>
yangged5e7e02015-01-06 10:16:15 -080058#include <grpc/support/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059
60using grpc::cpp::test::util::EchoRequest;
61using grpc::cpp::test::util::EchoResponse;
yangged5e7e02015-01-06 10:16:15 -080062using std::chrono::system_clock;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063
64namespace grpc {
yangged5e7e02015-01-06 10:16:15 -080065namespace testing {
66
67namespace {
68
69// When echo_deadline is requested, deadline seen in the ServerContext is set in
70// the response in seconds.
71void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request,
72 EchoResponse* response) {
73 if (request->has_param() && request->param().echo_deadline()) {
74 gpr_timespec deadline = gpr_inf_future;
75 if (context->absolute_deadline() != system_clock::time_point::max()) {
76 Timepoint2Timespec(context->absolute_deadline(), &deadline);
77 }
78 response->mutable_param()->set_request_deadline(deadline.tv_sec);
79 }
80}
Craig Tiller7418d012015-02-11 15:25:03 -080081
yangged5e7e02015-01-06 10:16:15 -080082} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083
yangg1456d152015-01-08 15:39:58 -080084class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085 public:
Yang Gao0c4b0dd2015-03-30 13:08:34 -070086 TestServiceImpl() : signal_client_(false) {}
87
yangga4b6f5d2014-12-17 15:53:12 -080088 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -080089 EchoResponse* response) GRPC_OVERRIDE {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090 response->set_message(request->message());
yangged5e7e02015-01-06 10:16:15 -080091 MaybeEchoDeadline(context, request, response);
Yang Gao0c4b0dd2015-03-30 13:08:34 -070092 if (request->has_param() && request->param().client_cancel_after_us()) {
93 {
94 std::unique_lock<std::mutex> lock(mu_);
95 signal_client_ = true;
96 }
97 while (!context->IsCancelled()) {
98 std::this_thread::sleep_for(std::chrono::microseconds(
99 request->param().client_cancel_after_us()));
100 }
101 return Status::Cancelled;
102 } else if (request->has_param() &&
103 request->param().server_cancel_after_us()) {
104 std::this_thread::sleep_for(
105 std::chrono::microseconds(request->param().server_cancel_after_us()));
106 return Status::Cancelled;
107 } else {
108 EXPECT_FALSE(context->IsCancelled());
109 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 return Status::OK;
111 }
nnoble0c475f02014-12-05 15:37:39 -0800112
113 // Unimplemented is left unimplemented to test the returned error.
114
yangga4b6f5d2014-12-17 15:53:12 -0800115 Status RequestStream(ServerContext* context,
116 ServerReader<EchoRequest>* reader,
Craig Tillercf133f42015-02-26 14:05:56 -0800117 EchoResponse* response) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800118 EchoRequest request;
119 response->set_message("");
120 while (reader->Read(&request)) {
121 response->mutable_message()->append(request.message());
122 }
123 return Status::OK;
124 }
125
126 // Return 3 messages.
127 // TODO(yangg) make it generic by adding a parameter into EchoRequest
yangga4b6f5d2014-12-17 15:53:12 -0800128 Status ResponseStream(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800129 ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800130 EchoResponse response;
131 response.set_message(request->message() + "0");
132 writer->Write(response);
133 response.set_message(request->message() + "1");
134 writer->Write(response);
135 response.set_message(request->message() + "2");
136 writer->Write(response);
137
138 return Status::OK;
139 }
140
Craig Tillercf133f42015-02-26 14:05:56 -0800141 Status BidiStream(ServerContext* context,
142 ServerReaderWriter<EchoResponse, EchoRequest>* stream)
143 GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800144 EchoRequest request;
145 EchoResponse response;
146 while (stream->Read(&request)) {
147 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
148 response.set_message(request.message());
149 stream->Write(response);
150 }
151 return Status::OK;
152 }
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700153
154 bool signal_client() {
155 std::unique_lock<std::mutex> lock(mu_);
156 return signal_client_;
157 }
158
159 private:
160 bool signal_client_;
161 std::mutex mu_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800162};
163
yangg1456d152015-01-08 15:39:58 -0800164class TestServiceImplDupPkg
165 : public ::grpc::cpp::test::util::duplicate::TestService::Service {
166 public:
167 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800168 EchoResponse* response) GRPC_OVERRIDE {
yangg1456d152015-01-08 15:39:58 -0800169 response->set_message("no package");
170 return Status::OK;
171 }
172};
173
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174class End2endTest : public ::testing::Test {
175 protected:
Craig Tiller7418d012015-02-11 15:25:03 -0800176 End2endTest() : thread_pool_(2) {}
177
Craig Tillercf133f42015-02-26 14:05:56 -0800178 void SetUp() GRPC_OVERRIDE {
Craig Tiller35e39712015-01-12 16:41:24 -0800179 int port = grpc_pick_unused_port_or_die();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180 server_address_ << "localhost:" << port;
181 // Setup server
182 ServerBuilder builder;
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700183 builder.AddListeningPort(server_address_.str(),
184 InsecureServerCredentials());
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800185 builder.RegisterService(&service_);
186 builder.RegisterService(&dup_pkg_service_);
Craig Tiller7418d012015-02-11 15:25:03 -0800187 builder.SetThreadPool(&thread_pool_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188 server_ = builder.BuildAndStart();
189 }
190
Craig Tillercf133f42015-02-26 14:05:56 -0800191 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192
yangg1456d152015-01-08 15:39:58 -0800193 void ResetStub() {
Craig Tiller47c83fd2015-02-21 22:45:35 -0800194 std::shared_ptr<ChannelInterface> channel = CreateChannel(
195 server_address_.str(), InsecureCredentials(), ChannelArguments());
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800196 stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel));
yangg1456d152015-01-08 15:39:58 -0800197 }
198
199 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200 std::unique_ptr<Server> server_;
201 std::ostringstream server_address_;
nnoble0c475f02014-12-05 15:37:39 -0800202 TestServiceImpl service_;
yangg1456d152015-01-08 15:39:58 -0800203 TestServiceImplDupPkg dup_pkg_service_;
Craig Tiller7418d012015-02-11 15:25:03 -0800204 ThreadPool thread_pool_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205};
206
yangg1456d152015-01-08 15:39:58 -0800207static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub,
208 int num_rpcs) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 EchoRequest request;
210 EchoResponse response;
211 request.set_message("Hello");
212
213 for (int i = 0; i < num_rpcs; ++i) {
214 ClientContext context;
215 Status s = stub->Echo(&context, request, &response);
216 EXPECT_EQ(response.message(), request.message());
217 EXPECT_TRUE(s.IsOk());
218 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219}
220
221TEST_F(End2endTest, SimpleRpc) {
yangg1456d152015-01-08 15:39:58 -0800222 ResetStub();
223 SendRpc(stub_.get(), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224}
225
226TEST_F(End2endTest, MultipleRpcs) {
yangg1456d152015-01-08 15:39:58 -0800227 ResetStub();
Craig Tiller35e39712015-01-12 16:41:24 -0800228 std::vector<std::thread*> threads;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800229 for (int i = 0; i < 10; ++i) {
yangg1456d152015-01-08 15:39:58 -0800230 threads.push_back(new std::thread(SendRpc, stub_.get(), 10));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800231 }
232 for (int i = 0; i < 10; ++i) {
233 threads[i]->join();
234 delete threads[i];
235 }
236}
237
yanggda029e32014-12-18 10:24:04 -0800238// Set a 10us deadline and make sure proper error is returned.
239TEST_F(End2endTest, RpcDeadlineExpires) {
yangg1456d152015-01-08 15:39:58 -0800240 ResetStub();
yanggda029e32014-12-18 10:24:04 -0800241 EchoRequest request;
242 EchoResponse response;
243 request.set_message("Hello");
244
245 ClientContext context;
246 std::chrono::system_clock::time_point deadline =
247 std::chrono::system_clock::now() + std::chrono::microseconds(10);
248 context.set_absolute_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800249 Status s = stub_->Echo(&context, request, &response);
Craig Tiller7b018782015-01-16 09:53:39 -0800250 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.code());
yanggda029e32014-12-18 10:24:04 -0800251}
252
yangged5e7e02015-01-06 10:16:15 -0800253// Set a long but finite deadline.
254TEST_F(End2endTest, RpcLongDeadline) {
yangg1456d152015-01-08 15:39:58 -0800255 ResetStub();
yangged5e7e02015-01-06 10:16:15 -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::hours(1);
263 context.set_absolute_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800264 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800265 EXPECT_EQ(response.message(), request.message());
266 EXPECT_TRUE(s.IsOk());
yangged5e7e02015-01-06 10:16:15 -0800267}
268
269// Ask server to echo back the deadline it sees.
270TEST_F(End2endTest, EchoDeadline) {
yangg1456d152015-01-08 15:39:58 -0800271 ResetStub();
yangged5e7e02015-01-06 10:16:15 -0800272 EchoRequest request;
273 EchoResponse response;
274 request.set_message("Hello");
275 request.mutable_param()->set_echo_deadline(true);
276
277 ClientContext context;
278 std::chrono::system_clock::time_point deadline =
279 std::chrono::system_clock::now() + std::chrono::seconds(100);
280 context.set_absolute_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800281 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800282 EXPECT_EQ(response.message(), request.message());
283 EXPECT_TRUE(s.IsOk());
284 gpr_timespec sent_deadline;
285 Timepoint2Timespec(deadline, &sent_deadline);
286 // Allow 1 second error.
287 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
288 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
yangged5e7e02015-01-06 10:16:15 -0800289}
290
291// Ask server to echo back the deadline it sees. The rpc has no deadline.
292TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) {
yangg1456d152015-01-08 15:39:58 -0800293 ResetStub();
yangged5e7e02015-01-06 10:16:15 -0800294 EchoRequest request;
295 EchoResponse response;
296 request.set_message("Hello");
297 request.mutable_param()->set_echo_deadline(true);
298
299 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800300 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800301 EXPECT_EQ(response.message(), request.message());
302 EXPECT_TRUE(s.IsOk());
303 EXPECT_EQ(response.param().request_deadline(), gpr_inf_future.tv_sec);
yangged5e7e02015-01-06 10:16:15 -0800304}
305
nnoble0c475f02014-12-05 15:37:39 -0800306TEST_F(End2endTest, UnimplementedRpc) {
yangg1456d152015-01-08 15:39:58 -0800307 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800308 EchoRequest request;
309 EchoResponse response;
310 request.set_message("Hello");
311
312 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800313 Status s = stub_->Unimplemented(&context, request, &response);
nnoble0c475f02014-12-05 15:37:39 -0800314 EXPECT_FALSE(s.IsOk());
315 EXPECT_EQ(s.code(), grpc::StatusCode::UNIMPLEMENTED);
316 EXPECT_EQ(s.details(), "");
317 EXPECT_EQ(response.message(), "");
nnoble0c475f02014-12-05 15:37:39 -0800318}
319
320TEST_F(End2endTest, RequestStreamOneRequest) {
yangg1456d152015-01-08 15:39:58 -0800321 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800322 EchoRequest request;
323 EchoResponse response;
324 ClientContext context;
325
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800326 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800327 request.set_message("hello");
328 EXPECT_TRUE(stream->Write(request));
329 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800330 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800331 EXPECT_EQ(response.message(), request.message());
332 EXPECT_TRUE(s.IsOk());
nnoble0c475f02014-12-05 15:37:39 -0800333}
334
335TEST_F(End2endTest, RequestStreamTwoRequests) {
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 EXPECT_TRUE(stream->Write(request));
345 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800346 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800347 EXPECT_EQ(response.message(), "hellohello");
348 EXPECT_TRUE(s.IsOk());
nnoble0c475f02014-12-05 15:37:39 -0800349}
350
351TEST_F(End2endTest, ResponseStream) {
yangg1456d152015-01-08 15:39:58 -0800352 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800353 EchoRequest request;
354 EchoResponse response;
355 ClientContext context;
356 request.set_message("hello");
357
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800358 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800359 EXPECT_TRUE(stream->Read(&response));
360 EXPECT_EQ(response.message(), request.message() + "0");
361 EXPECT_TRUE(stream->Read(&response));
362 EXPECT_EQ(response.message(), request.message() + "1");
363 EXPECT_TRUE(stream->Read(&response));
364 EXPECT_EQ(response.message(), request.message() + "2");
365 EXPECT_FALSE(stream->Read(&response));
366
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800367 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800368 EXPECT_TRUE(s.IsOk());
nnoble0c475f02014-12-05 15:37:39 -0800369}
370
371TEST_F(End2endTest, BidiStream) {
yangg1456d152015-01-08 15:39:58 -0800372 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800373 EchoRequest request;
374 EchoResponse response;
375 ClientContext context;
376 grpc::string msg("hello");
377
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800378 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800379
380 request.set_message(msg + "0");
381 EXPECT_TRUE(stream->Write(request));
382 EXPECT_TRUE(stream->Read(&response));
383 EXPECT_EQ(response.message(), request.message());
384
385 request.set_message(msg + "1");
386 EXPECT_TRUE(stream->Write(request));
387 EXPECT_TRUE(stream->Read(&response));
388 EXPECT_EQ(response.message(), request.message());
389
390 request.set_message(msg + "2");
391 EXPECT_TRUE(stream->Write(request));
392 EXPECT_TRUE(stream->Read(&response));
393 EXPECT_EQ(response.message(), request.message());
394
395 stream->WritesDone();
396 EXPECT_FALSE(stream->Read(&response));
397
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800398 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800399 EXPECT_TRUE(s.IsOk());
yangg1456d152015-01-08 15:39:58 -0800400}
401
402// Talk to the two services with the same name but different package names.
403// The two stubs are created on the same channel.
404TEST_F(End2endTest, DiffPackageServices) {
Craig Tiller47c83fd2015-02-21 22:45:35 -0800405 std::shared_ptr<ChannelInterface> channel = CreateChannel(
406 server_address_.str(), InsecureCredentials(), ChannelArguments());
yangg1456d152015-01-08 15:39:58 -0800407
408 EchoRequest request;
409 EchoResponse response;
410 request.set_message("Hello");
411
412 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub(
413 grpc::cpp::test::util::TestService::NewStub(channel));
414 ClientContext context;
415 Status s = stub->Echo(&context, request, &response);
416 EXPECT_EQ(response.message(), request.message());
417 EXPECT_TRUE(s.IsOk());
418
419 std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub>
420 dup_pkg_stub(
421 grpc::cpp::test::util::duplicate::TestService::NewStub(channel));
422 ClientContext context2;
423 s = dup_pkg_stub->Echo(&context2, request, &response);
424 EXPECT_EQ("no package", response.message());
425 EXPECT_TRUE(s.IsOk());
nnoble0c475f02014-12-05 15:37:39 -0800426}
427
yangg4105e2b2015-01-09 14:19:44 -0800428// rpc and stream should fail on bad credentials.
429TEST_F(End2endTest, BadCredentials) {
430 std::unique_ptr<Credentials> bad_creds =
Craig Tiller47c83fd2015-02-21 22:45:35 -0800431 ServiceAccountCredentials("", "", std::chrono::seconds(1));
yangg4105e2b2015-01-09 14:19:44 -0800432 EXPECT_EQ(nullptr, bad_creds.get());
433 std::shared_ptr<ChannelInterface> channel =
434 CreateChannel(server_address_.str(), bad_creds, ChannelArguments());
435 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub(
436 grpc::cpp::test::util::TestService::NewStub(channel));
437 EchoRequest request;
438 EchoResponse response;
439 ClientContext context;
440 grpc::string msg("hello");
441
442 Status s = stub->Echo(&context, request, &response);
443 EXPECT_EQ("", response.message());
444 EXPECT_FALSE(s.IsOk());
445 EXPECT_EQ(StatusCode::UNKNOWN, s.code());
446 EXPECT_EQ("Rpc sent on a lame channel.", s.details());
447
448 ClientContext context2;
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800449 auto stream = stub->BidiStream(&context2);
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800450 s = stream->Finish();
yangg4105e2b2015-01-09 14:19:44 -0800451 EXPECT_FALSE(s.IsOk());
452 EXPECT_EQ(StatusCode::UNKNOWN, s.code());
453 EXPECT_EQ("Rpc sent on a lame channel.", s.details());
yangg4105e2b2015-01-09 14:19:44 -0800454}
455
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700456void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
457 std::this_thread::sleep_for(std::chrono::microseconds(delay_us));
458 while (!service->signal_client()) {
459 }
460 context->TryCancel();
461}
462
463// Client cancels rpc after 10ms
464TEST_F(End2endTest, ClientCancelsRpc) {
465 ResetStub();
466 EchoRequest request;
467 EchoResponse response;
468 request.set_message("Hello");
469 const int kCancelDelayUs = 10 * 1000;
470 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
471
472 ClientContext context;
473 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
474 Status s = stub_->Echo(&context, request, &response);
475 cancel_thread.join();
476 EXPECT_EQ(StatusCode::CANCELLED, s.code());
Craig Tiller12088b22015-04-20 09:51:27 -0700477 EXPECT_EQ(s.details(), "Cancelled");
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700478}
479
480// Server cancels rpc after 1ms
481TEST_F(End2endTest, ServerCancelsRpc) {
482 ResetStub();
483 EchoRequest request;
484 EchoResponse response;
485 request.set_message("Hello");
486 request.mutable_param()->set_server_cancel_after_us(1000);
487
488 ClientContext context;
489 Status s = stub_->Echo(&context, request, &response);
490 EXPECT_EQ(StatusCode::CANCELLED, s.code());
491 EXPECT_TRUE(s.details().empty());
492}
493
Abhishek Kumare41d0402015-04-17 14:12:33 -0700494// Client cancels server stream after sending some messages
Abhishek Kumara1d3a722015-04-23 10:24:04 -0700495TEST_F(End2endTest, ClientCancelsResponseStream) {
Abhishek Kumare41d0402015-04-17 14:12:33 -0700496 ResetStub();
497 EchoRequest request;
498 EchoResponse response;
499 ClientContext context;
500 request.set_message("hello");
501
502 auto stream = stub_->ResponseStream(&context, request);
503
504 EXPECT_TRUE(stream->Read(&response));
505 EXPECT_EQ(response.message(), request.message() + "0");
506 EXPECT_TRUE(stream->Read(&response));
507 EXPECT_EQ(response.message(), request.message() + "1");
508
509 context.TryCancel();
510
511 // The cancellation races with responses, so there might be zero or
512 // one responses pending, read till failure
513
514 if (stream->Read(&response)) {
515 EXPECT_EQ(response.message(), request.message() + "2");
516 // Since we have cancelled, we expect the next attempt to read to fail
517 EXPECT_FALSE(stream->Read(&response));
518 }
519
520 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700521 // The final status could be either of CANCELLED or OK depending on
522 // who won the race.
523 EXPECT_GE(grpc::StatusCode::CANCELLED, s.code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700524}
525
Abhishek Kumar82a83312015-04-17 13:30:51 -0700526// Client cancels bidi stream after sending some messages
527TEST_F(End2endTest, ClientCancelsBidi) {
528 ResetStub();
529 EchoRequest request;
530 EchoResponse response;
531 ClientContext context;
532 grpc::string msg("hello");
533
534 auto stream = stub_->BidiStream(&context);
535
536 request.set_message(msg + "0");
537 EXPECT_TRUE(stream->Write(request));
538 EXPECT_TRUE(stream->Read(&response));
539 EXPECT_EQ(response.message(), request.message());
540
541 request.set_message(msg + "1");
542 EXPECT_TRUE(stream->Write(request));
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());
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();
556 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.code());
557}
558
559
yangged5e7e02015-01-06 10:16:15 -0800560} // namespace testing
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800561} // namespace grpc
562
563int main(int argc, char** argv) {
Craig Tiller14e60e92015-01-13 17:26:46 -0800564 grpc_test_init(argc, argv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800565 ::testing::InitGoogleTest(&argc, argv);
Yang Gaoc4b6ffb2015-04-23 16:35:24 -0700566 return RUN_ALL_TESTS();
Craig Tiller190d3602015-02-18 09:23:38 -0800567}