| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <thread> |
| 20 |
|
| 21 |
#include <gtest/gtest.h> |
| 22 |
|
| 23 |
#include <grpc/grpc.h> |
| 24 |
#include <grpc/support/log.h> |
| 25 |
#include <grpc/support/sync.h> |
| 26 |
#include <grpcpp/channel.h> |
| 27 |
#include <grpcpp/client_context.h> |
| 28 |
#include <grpcpp/create_channel.h> |
| 29 |
#include <grpcpp/server.h> |
| 30 |
#include <grpcpp/server_builder.h> |
| 31 |
#include <grpcpp/server_context.h> |
| 32 |
|
| 33 |
#include "src/core/lib/gprpp/env.h" |
| 34 |
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 35 |
#include "test/core/util/port.h" |
| 36 |
#include "test/core/util/test_config.h" |
| 37 |
#include "test/cpp/util/test_credentials_provider.h" |
| 38 |
|
| 39 |
namespace grpc { |
| 40 |
namespace testing { |
| 41 |
|
| 42 |
class TestServiceImpl : public grpc::testing::EchoTestService::Service { |
| 43 |
public: |
| 44 |
explicit TestServiceImpl(gpr_event* ev) : ev_(ev) {} |
| 45 |
|
| 46 |
Status Echo(ServerContext* context, const EchoRequest* /*request*/, |
| 47 |
EchoResponse* /*response*/) override { |
| 48 |
gpr_event_set(ev_, reinterpret_cast<void*>(1)); |
| 49 |
while (!context->IsCancelled()) { |
| 50 |
} |
| 51 |
return Status::OK; |
| 52 |
} |
| 53 |
|
| 54 |
private: |
| 55 |
gpr_event* ev_; |
| 56 |
}; |
| 57 |
|
| 58 |
class ShutdownTest : public ::testing::TestWithParam<string> { |
| 59 |
public: |
| 60 |
ShutdownTest() : shutdown_(false), service_(&ev_) { gpr_event_init(&ev_); } |
| 61 |
|
| 62 |
void SetUp() override { |
| 63 |
port_ = grpc_pick_unused_port_or_die(); |
| 64 |
server_ = SetUpServer(port_); |
| 65 |
} |
| 66 |
|
| 67 |
std::unique_ptr<Server> SetUpServer(const int port) { |
| 68 |
std::string server_address = "localhost:" + to_string(port); |
| 69 |
|
| 70 |
ServerBuilder builder; |
| 71 |
auto server_creds = |
| 72 |
GetCredentialsProvider()->GetServerCredentials(GetParam()); |
| 73 |
builder.AddListeningPort(server_address, server_creds); |
| 74 |
builder.RegisterService(&service_); |
| 75 |
std::unique_ptr<Server> server = builder.BuildAndStart(); |
| 76 |
return server; |
| 77 |
} |
| 78 |
|
| 79 |
void TearDown() override { GPR_ASSERT(shutdown_); } |
| 80 |
|
| 81 |
void ResetStub() { |
| 82 |
string target = "dns:localhost:" + to_string(port_); |
| 83 |
ChannelArguments args; |
| 84 |
auto channel_creds = |
| 85 |
GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args); |
| 86 |
channel_ = grpc::CreateCustomChannel(target, channel_creds, args); |
| 87 |
stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
| 88 |
} |
| 89 |
|
| 90 |
string to_string(const int number) { |
| 91 |
std::stringstream strs; |
| 92 |
strs << number; |
| 93 |
return strs.str(); |
| 94 |
} |
| 95 |
|
| 96 |
void SendRequest() { |
| 97 |
EchoRequest request; |
| 98 |
EchoResponse response; |
| 99 |
request.set_message("Hello"); |
| 100 |
ClientContext context; |
| 101 |
GPR_ASSERT(!shutdown_); |
| 102 |
Status s = stub_->Echo(&context, request, &response); |
| 103 |
GPR_ASSERT(shutdown_); |
| 104 |
} |
| 105 |
|
| 106 |
protected: |
| 107 |
std::shared_ptr<Channel> channel_; |
| 108 |
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 109 |
std::unique_ptr<Server> server_; |
| 110 |
bool shutdown_; |
| 111 |
int port_; |
| 112 |
gpr_event ev_; |
| 113 |
TestServiceImpl service_; |
| 114 |
}; |
| 115 |
|
| 116 |
std::vector<string> GetAllCredentialsTypeList() { |
| 117 |
std::vector<std::string> credentials_types; |
| 118 |
if (GetCredentialsProvider()->GetChannelCredentials(kInsecureCredentialsType, |
| 119 |
nullptr) != nullptr) { |
| 120 |
credentials_types.push_back(kInsecureCredentialsType); |
| 121 |
} |
| 122 |
auto sec_list = GetCredentialsProvider()->GetSecureCredentialsTypeList(); |
| 123 |
for (auto sec = sec_list.begin(); sec != sec_list.end(); sec++) { |
| 124 |
credentials_types.push_back(*sec); |
| 125 |
} |
| 126 |
GPR_ASSERT(!credentials_types.empty()); |
| 127 |
|
| 128 |
std::string credentials_type_list("credentials types:"); |
| 129 |
for (const string& type : credentials_types) { |
| 130 |
credentials_type_list.append(" " + type); |
| 131 |
} |
| 132 |
gpr_log(GPR_INFO, "%s", credentials_type_list.c_str()); |
| 133 |
return credentials_types; |
| 134 |
} |
| 135 |
|
| 136 |
INSTANTIATE_TEST_SUITE_P(End2EndShutdown, ShutdownTest, |
| 137 |
::testing::ValuesIn(GetAllCredentialsTypeList())); |
| 138 |
|
| 139 |
// TODO(ctiller): leaked objects in this test |
| 140 |
TEST_P(ShutdownTest, ShutdownTest) { |
| 141 |
ResetStub(); |
| 142 |
|
| 143 |
// send the request in a background thread |
| 144 |
std::thread thr(std::bind(&ShutdownTest::SendRequest, this)); |
| 145 |
|
| 146 |
// wait for the server to get the event |
| 147 |
gpr_event_wait(&ev_, gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
| 148 |
|
| 149 |
shutdown_ = true; |
| 150 |
|
| 151 |
// shutdown should trigger cancellation causing everything to shutdown |
| 152 |
auto deadline = |
| 153 |
std::chrono::system_clock::now() + std::chrono::microseconds(100); |
| 154 |
server_->Shutdown(deadline); |
| 155 |
EXPECT_GE(std::chrono::system_clock::now(), deadline); |
| 156 |
|
| 157 |
thr.join(); |
| 158 |
} |
| 159 |
|
| 160 |
} // namespace testing |
| 161 |
} // namespace grpc |
| 162 |
|
| 163 |
int main(int argc, char** argv) { |
| 164 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 165 |
::testing::InitGoogleTest(&argc, argv); |
| 166 |
return RUN_ALL_TESTS(); |
| 167 |
} |