shutdown_test.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include <thread>
  19. #include <gtest/gtest.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/support/log.h>
  22. #include <grpc/support/sync.h>
  23. #include <grpcpp/channel.h>
  24. #include <grpcpp/client_context.h>
  25. #include <grpcpp/create_channel.h>
  26. #include <grpcpp/server.h>
  27. #include <grpcpp/server_builder.h>
  28. #include <grpcpp/server_context.h>
  29. #include "src/core/lib/gpr/env.h"
  30. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  31. #include "test/core/util/port.h"
  32. #include "test/core/util/test_config.h"
  33. #include "test/cpp/util/test_credentials_provider.h"
  34. using grpc::testing::EchoRequest;
  35. using grpc::testing::EchoResponse;
  36. namespace grpc {
  37. namespace testing {
  38. class TestServiceImpl : public grpc::testing::EchoTestService::Service {
  39. public:
  40. explicit TestServiceImpl(gpr_event* ev) : ev_(ev) {}
  41. Status Echo(ServerContext* context, const EchoRequest* /*request*/,
  42. EchoResponse* /*response*/) override {
  43. gpr_event_set(ev_, reinterpret_cast<void*>(1));
  44. while (!context->IsCancelled()) {
  45. }
  46. return Status::OK;
  47. }
  48. private:
  49. gpr_event* ev_;
  50. };
  51. class ShutdownTest : public ::testing::TestWithParam<string> {
  52. public:
  53. ShutdownTest() : shutdown_(false), service_(&ev_) { gpr_event_init(&ev_); }
  54. void SetUp() override {
  55. port_ = grpc_pick_unused_port_or_die();
  56. server_ = SetUpServer(port_);
  57. }
  58. std::unique_ptr<Server> SetUpServer(const int port) {
  59. std::string server_address = "localhost:" + to_string(port);
  60. ServerBuilder builder;
  61. auto server_creds =
  62. GetCredentialsProvider()->GetServerCredentials(GetParam());
  63. builder.AddListeningPort(server_address, server_creds);
  64. builder.RegisterService(&service_);
  65. std::unique_ptr<Server> server = builder.BuildAndStart();
  66. return server;
  67. }
  68. void TearDown() override { GPR_ASSERT(shutdown_); }
  69. void ResetStub() {
  70. string target = "dns:localhost:" + to_string(port_);
  71. ChannelArguments args;
  72. auto channel_creds =
  73. GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args);
  74. channel_ = grpc::CreateCustomChannel(target, channel_creds, args);
  75. stub_ = grpc::testing::EchoTestService::NewStub(channel_);
  76. }
  77. string to_string(const int number) {
  78. std::stringstream strs;
  79. strs << number;
  80. return strs.str();
  81. }
  82. void SendRequest() {
  83. EchoRequest request;
  84. EchoResponse response;
  85. request.set_message("Hello");
  86. ClientContext context;
  87. GPR_ASSERT(!shutdown_);
  88. Status s = stub_->Echo(&context, request, &response);
  89. GPR_ASSERT(shutdown_);
  90. }
  91. protected:
  92. std::shared_ptr<Channel> channel_;
  93. std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
  94. std::unique_ptr<Server> server_;
  95. bool shutdown_;
  96. int port_;
  97. gpr_event ev_;
  98. TestServiceImpl service_;
  99. };
  100. std::vector<string> GetAllCredentialsTypeList() {
  101. std::vector<std::string> credentials_types;
  102. if (GetCredentialsProvider()->GetChannelCredentials(kInsecureCredentialsType,
  103. nullptr) != nullptr) {
  104. credentials_types.push_back(kInsecureCredentialsType);
  105. }
  106. auto sec_list = GetCredentialsProvider()->GetSecureCredentialsTypeList();
  107. for (auto sec = sec_list.begin(); sec != sec_list.end(); sec++) {
  108. credentials_types.push_back(*sec);
  109. }
  110. GPR_ASSERT(!credentials_types.empty());
  111. std::string credentials_type_list("credentials types:");
  112. for (const string& type : credentials_types) {
  113. credentials_type_list.append(" " + type);
  114. }
  115. gpr_log(GPR_INFO, "%s", credentials_type_list.c_str());
  116. return credentials_types;
  117. }
  118. INSTANTIATE_TEST_SUITE_P(End2EndShutdown, ShutdownTest,
  119. ::testing::ValuesIn(GetAllCredentialsTypeList()));
  120. // TODO(ctiller): leaked objects in this test
  121. TEST_P(ShutdownTest, ShutdownTest) {
  122. ResetStub();
  123. // send the request in a background thread
  124. std::thread thr(std::bind(&ShutdownTest::SendRequest, this));
  125. // wait for the server to get the event
  126. gpr_event_wait(&ev_, gpr_inf_future(GPR_CLOCK_MONOTONIC));
  127. shutdown_ = true;
  128. // shutdown should trigger cancellation causing everything to shutdown
  129. auto deadline =
  130. std::chrono::system_clock::now() + std::chrono::microseconds(100);
  131. server_->Shutdown(deadline);
  132. EXPECT_GE(std::chrono::system_clock::now(), deadline);
  133. thr.join();
  134. }
  135. } // namespace testing
  136. } // namespace grpc
  137. int main(int argc, char** argv) {
  138. grpc::testing::TestEnvironment env(argc, argv);
  139. ::testing::InitGoogleTest(&argc, argv);
  140. return RUN_ALL_TESTS();
  141. }