client_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 CrashTest : public ::testing::Test {
50  protected:
51  CrashTest() {}
52 
53  std::unique_ptr<grpc::testing::EchoTestService::Stub> CreateServerAndStub() {
55  std::ostringstream addr_stream;
56  addr_stream << "localhost:" << port;
57  auto addr = addr_stream.str();
58  server_ = absl::make_unique<SubProcess>(std::vector<std::string>({
59  g_root + "/client_crash_test_server",
60  "--address=" + addr,
61  }));
63  return grpc::testing::EchoTestService::NewStub(
65  }
66 
67  void KillServer() { server_.reset(); }
68 
69  private:
70  std::unique_ptr<SubProcess> server_;
71 };
72 
73 TEST_F(CrashTest, KillBeforeWrite) {
74  auto stub = CreateServerAndStub();
75 
76  EchoRequest request;
77  EchoResponse response;
78  ClientContext context;
80 
81  auto stream = stub->BidiStream(&context);
82 
83  request.set_message("Hello");
84  EXPECT_TRUE(stream->Write(request));
85  EXPECT_TRUE(stream->Read(&response));
86  EXPECT_EQ(response.message(), request.message());
87 
88  KillServer();
89 
90  request.set_message("You should be dead");
91  // This may succeed or fail depending on the state of the TCP connection
92  stream->Write(request);
93  // But the read will definitely fail
94  EXPECT_FALSE(stream->Read(&response));
95 
96  EXPECT_FALSE(stream->Finish().ok());
97 }
98 
99 TEST_F(CrashTest, KillAfterWrite) {
100  auto stub = CreateServerAndStub();
101 
102  EchoRequest request;
103  EchoResponse response;
104  ClientContext context;
106 
107  auto stream = stub->BidiStream(&context);
108 
109  request.set_message("Hello");
110  EXPECT_TRUE(stream->Write(request));
111  EXPECT_TRUE(stream->Read(&response));
112  EXPECT_EQ(response.message(), request.message());
113 
114  request.set_message("I'm going to kill you");
115  EXPECT_TRUE(stream->Write(request));
116 
117  KillServer();
118 
119  // This may succeed or fail depending on how quick the server was
120  stream->Read(&response);
121 
122  EXPECT_FALSE(stream->Finish().ok());
123 }
124 
125 } // namespace
126 
127 } // namespace testing
128 } // namespace grpc
129 
130 int main(int argc, char** argv) {
131  std::string me = argv[0];
132  auto lslash = me.rfind('/');
133  if (lslash != std::string::npos) {
134  g_root = me.substr(0, lslash);
135  } else {
136  g_root = ".";
137  }
138 
139  grpc::testing::TestEnvironment env(&argc, argv);
140  ::testing::InitGoogleTest(&argc, argv);
141  // Order seems to matter on these tests: run three times to eliminate that
142  for (int i = 0; i < 3; i++) {
143  if (RUN_ALL_TESTS() != 0) {
144  return 1;
145  }
146  }
147  return 0;
148 }
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
testing
Definition: aws_request_signer_test.cc:25
g_root
static std::string g_root
Definition: client_crash_test.cc:42
log.h
port.h
server_
std::unique_ptr< SubProcess > server_
Definition: client_crash_test.cc:70
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
grpc::ClientContext::set_wait_for_ready
void set_wait_for_ready(bool wait_for_ready)
Definition: grpcpp/impl/codegen/client_context.h:285
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
time.h
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
subprocess.h
main
int main(int argc, char **argv)
Definition: client_crash_test.cc:130
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc.h
channel.h
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
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
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::testing::EXPECT_EQ
EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange")
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
grpc::testing::EXPECT_TRUE
EXPECT_TRUE(grpc::experimental::StsCredentialsOptionsFromJson(minimum_valid_json, &options) .ok())
server.h
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
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
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:55