blob: bd68e851d3c1e5f636f1344a577ed8880efbe5fb [file] [log] [blame]
Craig Tillere50e5cb2015-08-18 12:44:57 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tillere50e5cb2015-08-18 12:44:57 -07004 * 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
Craig Tillere50e5cb2015-08-18 12:44:57 -070034#include <thread>
35
David Garcia Quintasc79b0652016-07-27 21:11:58 -070036#include <gtest/gtest.h>
37
yang-gd392fa02015-08-20 13:55:29 -070038#include <grpc++/channel.h>
Craig Tillere50e5cb2015-08-18 12:44:57 -070039#include <grpc++/client_context.h>
40#include <grpc++/create_channel.h>
Craig Tillere50e5cb2015-08-18 12:44:57 -070041#include <grpc++/server.h>
42#include <grpc++/server_builder.h>
43#include <grpc++/server_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080044#include <grpc/grpc.h>
David Garcia Quintasc79b0652016-07-27 21:11:58 -070045#include <grpc/support/log.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080046#include <grpc/support/sync.h>
yang-g9e2f90c2015-08-21 15:35:03 -070047
Craig Tiller9533d042016-03-25 17:11:06 -070048#include "src/core/lib/support/env.h"
Craig Tiller1b4e3302015-12-17 16:35:00 -080049#include "src/proto/grpc/testing/echo.grpc.pb.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080050#include "test/core/util/port.h"
51#include "test/core/util/test_config.h"
Dan Zhang340bb832017-01-20 14:49:30 -050052#include "test/cpp/util/test_credentials_provider.h"
Craig Tillere50e5cb2015-08-18 12:44:57 -070053
Craig Tiller1b4e3302015-12-17 16:35:00 -080054using grpc::testing::EchoRequest;
55using grpc::testing::EchoResponse;
Craig Tillere50e5cb2015-08-18 12:44:57 -070056
57namespace grpc {
58namespace testing {
59
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080060class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
Craig Tillere50e5cb2015-08-18 12:44:57 -070061 public:
62 explicit TestServiceImpl(gpr_event* ev) : ev_(ev) {}
63
64 Status Echo(ServerContext* context, const EchoRequest* request,
Vijay Paic0b2acb2016-11-01 16:31:56 -070065 EchoResponse* response) override {
Craig Tillere50e5cb2015-08-18 12:44:57 -070066 gpr_event_set(ev_, (void*)1);
67 while (!context->IsCancelled()) {
68 }
69 return Status::OK;
70 }
71
72 private:
73 gpr_event* ev_;
74};
75
Dan Zhang340bb832017-01-20 14:49:30 -050076class ShutdownTest : public ::testing::TestWithParam<string> {
Craig Tillere50e5cb2015-08-18 12:44:57 -070077 public:
78 ShutdownTest() : shutdown_(false), service_(&ev_) { gpr_event_init(&ev_); }
79
Vijay Paic0b2acb2016-11-01 16:31:56 -070080 void SetUp() override {
Craig Tillere50e5cb2015-08-18 12:44:57 -070081 port_ = grpc_pick_unused_port_or_die();
82 server_ = SetUpServer(port_);
83 }
84
85 std::unique_ptr<Server> SetUpServer(const int port) {
86 grpc::string server_address = "localhost:" + to_string(port);
87
88 ServerBuilder builder;
Dan Zhang340bb832017-01-20 14:49:30 -050089 auto server_creds =
90 GetCredentialsProvider()->GetServerCredentials(GetParam());
91 builder.AddListeningPort(server_address, server_creds);
Craig Tillere50e5cb2015-08-18 12:44:57 -070092 builder.RegisterService(&service_);
93 std::unique_ptr<Server> server = builder.BuildAndStart();
94 return server;
95 }
96
Vijay Paic0b2acb2016-11-01 16:31:56 -070097 void TearDown() override { GPR_ASSERT(shutdown_); }
Craig Tillere50e5cb2015-08-18 12:44:57 -070098
99 void ResetStub() {
100 string target = "dns:localhost:" + to_string(port_);
Dan Zhang340bb832017-01-20 14:49:30 -0500101 ChannelArguments args;
102 auto channel_creds =
103 GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args);
104 channel_ = CreateCustomChannel(target, channel_creds, args);
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800105 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
Craig Tillere50e5cb2015-08-18 12:44:57 -0700106 }
107
108 string to_string(const int number) {
109 std::stringstream strs;
110 strs << number;
111 return strs.str();
112 }
113
114 void SendRequest() {
115 EchoRequest request;
116 EchoResponse response;
117 request.set_message("Hello");
118 ClientContext context;
119 GPR_ASSERT(!shutdown_);
120 Status s = stub_->Echo(&context, request, &response);
121 GPR_ASSERT(shutdown_);
122 }
123
124 protected:
yang-gd392fa02015-08-20 13:55:29 -0700125 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800126 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Craig Tillere50e5cb2015-08-18 12:44:57 -0700127 std::unique_ptr<Server> server_;
128 bool shutdown_;
129 int port_;
130 gpr_event ev_;
131 TestServiceImpl service_;
132};
133
Dan Zhang340bb832017-01-20 14:49:30 -0500134std::vector<string> GetAllCredentialsTypeList() {
135 std::vector<grpc::string> credentials_types;
136 if (GetCredentialsProvider()->GetChannelCredentials(kInsecureCredentialsType,
137 nullptr) != nullptr) {
138 credentials_types.push_back(kInsecureCredentialsType);
139 }
140 auto sec_list = GetCredentialsProvider()->GetSecureCredentialsTypeList();
141 for (auto sec = sec_list.begin(); sec != sec_list.end(); sec++) {
142 credentials_types.push_back(*sec);
143 }
144 GPR_ASSERT(!credentials_types.empty());
145
146 std::string credentials_type_list("credentials types:");
147 for (const string& type : credentials_types) {
148 credentials_type_list.append(" " + type);
149 }
150 gpr_log(GPR_INFO, "%s", credentials_type_list.c_str());
151 return credentials_types;
152}
153
154INSTANTIATE_TEST_CASE_P(End2EndShutdown, ShutdownTest,
155 ::testing::ValuesIn(GetAllCredentialsTypeList()));
156
Craig Tillere50e5cb2015-08-18 12:44:57 -0700157// TODO(ctiller): leaked objects in this test
Dan Zhang340bb832017-01-20 14:49:30 -0500158TEST_P(ShutdownTest, ShutdownTest) {
Craig Tillere50e5cb2015-08-18 12:44:57 -0700159 ResetStub();
160
161 // send the request in a background thread
162 std::thread thr(std::bind(&ShutdownTest::SendRequest, this));
163
164 // wait for the server to get the event
165 gpr_event_wait(&ev_, gpr_inf_future(GPR_CLOCK_MONOTONIC));
166
167 shutdown_ = true;
168
169 // shutdown should trigger cancellation causing everything to shutdown
170 auto deadline =
171 std::chrono::system_clock::now() + std::chrono::microseconds(100);
172 server_->Shutdown(deadline);
173 EXPECT_GE(std::chrono::system_clock::now(), deadline);
174
175 thr.join();
176}
177
178} // namespace testing
179} // namespace grpc
180
181int main(int argc, char** argv) {
182 grpc_test_init(argc, argv);
183 ::testing::InitGoogleTest(&argc, argv);
184 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800185}