Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [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 | |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 34 | #include <thread> |
| 35 | |
David Garcia Quintas | c79b065 | 2016-07-27 21:11:58 -0700 | [diff] [blame] | 36 | #include <gtest/gtest.h> |
| 37 | |
yang-g | d392fa0 | 2015-08-20 13:55:29 -0700 | [diff] [blame] | 38 | #include <grpc++/channel.h> |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 39 | #include <grpc++/client_context.h> |
| 40 | #include <grpc++/create_channel.h> |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 41 | #include <grpc++/server.h> |
| 42 | #include <grpc++/server_builder.h> |
| 43 | #include <grpc++/server_context.h> |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 44 | #include <grpc/grpc.h> |
David Garcia Quintas | c79b065 | 2016-07-27 21:11:58 -0700 | [diff] [blame] | 45 | #include <grpc/support/log.h> |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 46 | #include <grpc/support/sync.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 47 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 48 | #include "src/core/lib/support/env.h" |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 49 | #include "src/proto/grpc/testing/echo.grpc.pb.h" |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 50 | #include "test/core/util/port.h" |
| 51 | #include "test/core/util/test_config.h" |
Dan Zhang | 340bb83 | 2017-01-20 14:49:30 -0500 | [diff] [blame] | 52 | #include "test/cpp/util/test_credentials_provider.h" |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 53 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 54 | using grpc::testing::EchoRequest; |
| 55 | using grpc::testing::EchoResponse; |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 56 | |
| 57 | namespace grpc { |
| 58 | namespace testing { |
| 59 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 60 | class TestServiceImpl : public ::grpc::testing::EchoTestService::Service { |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 61 | public: |
| 62 | explicit TestServiceImpl(gpr_event* ev) : ev_(ev) {} |
| 63 | |
| 64 | Status Echo(ServerContext* context, const EchoRequest* request, |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 65 | EchoResponse* response) override { |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 66 | 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 Zhang | 340bb83 | 2017-01-20 14:49:30 -0500 | [diff] [blame] | 76 | class ShutdownTest : public ::testing::TestWithParam<string> { |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 77 | public: |
| 78 | ShutdownTest() : shutdown_(false), service_(&ev_) { gpr_event_init(&ev_); } |
| 79 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 80 | void SetUp() override { |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 81 | 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 Zhang | 340bb83 | 2017-01-20 14:49:30 -0500 | [diff] [blame] | 89 | auto server_creds = |
| 90 | GetCredentialsProvider()->GetServerCredentials(GetParam()); |
| 91 | builder.AddListeningPort(server_address, server_creds); |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 92 | builder.RegisterService(&service_); |
| 93 | std::unique_ptr<Server> server = builder.BuildAndStart(); |
| 94 | return server; |
| 95 | } |
| 96 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 97 | void TearDown() override { GPR_ASSERT(shutdown_); } |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 98 | |
| 99 | void ResetStub() { |
| 100 | string target = "dns:localhost:" + to_string(port_); |
Dan Zhang | 340bb83 | 2017-01-20 14:49:30 -0500 | [diff] [blame] | 101 | ChannelArguments args; |
| 102 | auto channel_creds = |
| 103 | GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args); |
| 104 | channel_ = CreateCustomChannel(target, channel_creds, args); |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 105 | stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 106 | } |
| 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-g | d392fa0 | 2015-08-20 13:55:29 -0700 | [diff] [blame] | 125 | std::shared_ptr<Channel> channel_; |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 126 | std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 127 | std::unique_ptr<Server> server_; |
| 128 | bool shutdown_; |
| 129 | int port_; |
| 130 | gpr_event ev_; |
| 131 | TestServiceImpl service_; |
| 132 | }; |
| 133 | |
Dan Zhang | 340bb83 | 2017-01-20 14:49:30 -0500 | [diff] [blame] | 134 | std::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 | |
| 154 | INSTANTIATE_TEST_CASE_P(End2EndShutdown, ShutdownTest, |
| 155 | ::testing::ValuesIn(GetAllCredentialsTypeList())); |
| 156 | |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 157 | // TODO(ctiller): leaked objects in this test |
Dan Zhang | 340bb83 | 2017-01-20 14:49:30 -0500 | [diff] [blame] | 158 | TEST_P(ShutdownTest, ShutdownTest) { |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 159 | 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 | |
| 181 | int main(int argc, char** argv) { |
| 182 | grpc_test_init(argc, argv); |
| 183 | ::testing::InitGoogleTest(&argc, argv); |
| 184 | return RUN_ALL_TESTS(); |
David Garcia Quintas | 2bf574f | 2016-01-14 15:27:08 -0800 | [diff] [blame] | 185 | } |