server_crash_test.cc
Go to the documentation of this file.
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 <gtest/gtest.h>
20 
21 #include "absl/memory/memory.h"
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/time.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/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
34 #include "src/proto/grpc/testing/echo.grpc.pb.h"
35 #include "test/core/util/port.h"
38 
39 using grpc::testing::EchoRequest;
40 using grpc::testing::EchoResponse;
41 
43 
44 namespace grpc {
45 namespace testing {
46 
47 namespace {
48 
49 class ServiceImpl final : public grpc::testing::EchoTestService::Service {
50  public:
51  ServiceImpl() : bidi_stream_count_(0), response_stream_count_(0) {}
52 
54  ServerContext* /*context*/,
55  ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
57  EchoRequest request;
58  EchoResponse response;
59  while (stream->Read(&request)) {
60  gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
61  response.set_message(request.message());
62  stream->Write(response);
65  }
66  return Status::OK;
67  }
68 
69  Status ResponseStream(ServerContext* /*context*/,
70  const EchoRequest* /*request*/,
71  ServerWriter<EchoResponse>* writer) override {
72  EchoResponse response;
74  for (int i = 0;; i++) {
75  std::ostringstream msg;
76  msg << "Hello " << i;
77  response.set_message(msg.str());
78  if (!writer->Write(response)) break;
81  }
82  return Status::OK;
83  }
84 
85  int bidi_stream_count() { return bidi_stream_count_; }
86 
87  int response_stream_count() { return response_stream_count_; }
88 
89  private:
92 };
93 
94 class CrashTest : public ::testing::Test {
95  protected:
96  CrashTest() {}
97 
98  std::unique_ptr<Server> CreateServerAndClient(const std::string& mode) {
100  std::ostringstream addr_stream;
101  addr_stream << "localhost:" << port;
102  auto addr = addr_stream.str();
103  client_ = absl::make_unique<SubProcess>(
104  std::vector<std::string>({g_root + "/server_crash_test_client",
105  "--address=" + addr, "--mode=" + mode}));
107 
108  ServerBuilder builder;
109  builder.AddListeningPort(addr, grpc::InsecureServerCredentials());
110  builder.RegisterService(&service_);
111  return builder.BuildAndStart();
112  }
113 
114  void KillClient() { client_.reset(); }
115 
116  bool HadOneBidiStream() { return service_.bidi_stream_count() == 1; }
117 
118  bool HadOneResponseStream() { return service_.response_stream_count() == 1; }
119 
120  private:
121  std::unique_ptr<SubProcess> client_;
122  ServiceImpl service_;
123 };
124 
125 TEST_F(CrashTest, ResponseStream) {
126  auto server = CreateServerAndClient("response");
127 
130  KillClient();
131  server->Shutdown();
132  GPR_ASSERT(HadOneResponseStream());
133 }
134 
135 TEST_F(CrashTest, BidiStream) {
136  auto server = CreateServerAndClient("bidi");
137 
140  KillClient();
141  server->Shutdown();
142  GPR_ASSERT(HadOneBidiStream());
143 }
144 
145 } // namespace
146 
147 } // namespace testing
148 } // namespace grpc
149 
150 int main(int argc, char** argv) {
151  std::string me = argv[0];
152  auto lslash = me.rfind('/');
153  if (lslash != std::string::npos) {
154  g_root = me.substr(0, lslash);
155  } else {
156  g_root = ".";
157  }
158 
159  grpc::testing::TestEnvironment env(&argc, argv);
160  ::testing::InitGoogleTest(&argc, argv);
161  return RUN_ALL_TESTS();
162 }
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
log.h
port.h
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
client_
std::unique_ptr< SubProcess > client_
Definition: server_crash_test.cc:121
time.h
response_stream_count_
int response_stream_count_
Definition: server_crash_test.cc:91
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
service_
ServiceImpl service_
Definition: server_crash_test.cc:122
subprocess.h
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
grpc.h
g_root
static std::string g_root
Definition: server_crash_test.cc:42
main
int main(int argc, char **argv)
Definition: server_crash_test.cc:150
channel.h
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
bidi_stream_count_
int bidi_stream_count_
Definition: server_crash_test.cc:90
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
writer
void writer(void *n)
Definition: libuv/docs/code/locks/main.c:22
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
client_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc::testing::TEST_F
TEST_F(ChannelArgumentsTest, SetInt)
Definition: channel_arguments_test.cc:134
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
server
Definition: examples/python/async_streaming/server.py:1
grpc::testing::ServiceImpl::BidiStream
Status BidiStream(ServerContext *, ServerReaderWriter< EchoResponse, EchoRequest > *stream) override
Definition: client_crash_test_server.cc:42
server_context.h
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
server.h
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
server_builder.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
create_channel.h
gpr_time_from_seconds
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:123
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:17