server_crash_test.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <gtest/gtest.h>
  19. #include "absl/memory/memory.h"
  20. #include <grpc/grpc.h>
  21. #include <grpc/support/log.h>
  22. #include <grpc/support/time.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/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.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/subprocess.h"
  34. using grpc::testing::EchoRequest;
  35. using grpc::testing::EchoResponse;
  36. static std::string g_root;
  37. namespace grpc {
  38. namespace testing {
  39. namespace {
  40. class ServiceImpl final : public grpc::testing::EchoTestService::Service {
  41. public:
  42. ServiceImpl() : bidi_stream_count_(0), response_stream_count_(0) {}
  43. Status BidiStream(
  44. ServerContext* /*context*/,
  45. ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
  46. bidi_stream_count_++;
  47. EchoRequest request;
  48. EchoResponse response;
  49. while (stream->Read(&request)) {
  50. gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
  51. response.set_message(request.message());
  52. stream->Write(response);
  53. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  54. gpr_time_from_seconds(1, GPR_TIMESPAN)));
  55. }
  56. return Status::OK;
  57. }
  58. Status ResponseStream(ServerContext* /*context*/,
  59. const EchoRequest* /*request*/,
  60. ServerWriter<EchoResponse>* writer) override {
  61. EchoResponse response;
  62. response_stream_count_++;
  63. for (int i = 0;; i++) {
  64. std::ostringstream msg;
  65. msg << "Hello " << i;
  66. response.set_message(msg.str());
  67. if (!writer->Write(response)) break;
  68. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  69. gpr_time_from_seconds(1, GPR_TIMESPAN)));
  70. }
  71. return Status::OK;
  72. }
  73. int bidi_stream_count() { return bidi_stream_count_; }
  74. int response_stream_count() { return response_stream_count_; }
  75. private:
  76. int bidi_stream_count_;
  77. int response_stream_count_;
  78. };
  79. class CrashTest : public ::testing::Test {
  80. protected:
  81. CrashTest() {}
  82. std::unique_ptr<Server> CreateServerAndClient(const std::string& mode) {
  83. auto port = grpc_pick_unused_port_or_die();
  84. std::ostringstream addr_stream;
  85. addr_stream << "localhost:" << port;
  86. auto addr = addr_stream.str();
  87. client_ = absl::make_unique<SubProcess>(
  88. std::vector<std::string>({g_root + "/server_crash_test_client",
  89. "--address=" + addr, "--mode=" + mode}));
  90. GPR_ASSERT(client_);
  91. ServerBuilder builder;
  92. builder.AddListeningPort(addr, grpc::InsecureServerCredentials());
  93. builder.RegisterService(&service_);
  94. return builder.BuildAndStart();
  95. }
  96. void KillClient() { client_.reset(); }
  97. bool HadOneBidiStream() { return service_.bidi_stream_count() == 1; }
  98. bool HadOneResponseStream() { return service_.response_stream_count() == 1; }
  99. private:
  100. std::unique_ptr<SubProcess> client_;
  101. ServiceImpl service_;
  102. };
  103. TEST_F(CrashTest, ResponseStream) {
  104. auto server = CreateServerAndClient("response");
  105. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  106. gpr_time_from_seconds(60, GPR_TIMESPAN)));
  107. KillClient();
  108. server->Shutdown();
  109. GPR_ASSERT(HadOneResponseStream());
  110. }
  111. TEST_F(CrashTest, BidiStream) {
  112. auto server = CreateServerAndClient("bidi");
  113. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  114. gpr_time_from_seconds(60, GPR_TIMESPAN)));
  115. KillClient();
  116. server->Shutdown();
  117. GPR_ASSERT(HadOneBidiStream());
  118. }
  119. } // namespace
  120. } // namespace testing
  121. } // namespace grpc
  122. int main(int argc, char** argv) {
  123. std::string me = argv[0];
  124. auto lslash = me.rfind('/');
  125. if (lslash != std::string::npos) {
  126. g_root = me.substr(0, lslash);
  127. } else {
  128. g_root = ".";
  129. }
  130. grpc::testing::TestEnvironment env(argc, argv);
  131. ::testing::InitGoogleTest(&argc, argv);
  132. return RUN_ALL_TESTS();
  133. }