xref: /aosp_15_r20/external/grpc-grpc/test/cpp/end2end/client_crash_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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/core/lib/gprpp/crash.h"
34 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
35 #include "src/proto/grpc/testing/echo.grpc.pb.h"
36 #include "test/core/util/port.h"
37 #include "test/core/util/test_config.h"
38 #include "test/cpp/util/subprocess.h"
39 
40 static std::string g_root;
41 
42 namespace grpc {
43 namespace testing {
44 
45 namespace {
46 
47 class CrashTest : public ::testing::Test {
48  protected:
CrashTest()49   CrashTest() {}
50 
CreateServerAndStub()51   std::unique_ptr<grpc::testing::EchoTestService::Stub> CreateServerAndStub() {
52     auto port = grpc_pick_unused_port_or_die();
53     std::ostringstream addr_stream;
54     addr_stream << "localhost:" << port;
55     auto addr = addr_stream.str();
56     server_ = std::make_unique<SubProcess>(std::vector<std::string>({
57         g_root + "/client_crash_test_server",
58         "--address=" + addr,
59     }));
60     GPR_ASSERT(server_);
61     return grpc::testing::EchoTestService::NewStub(
62         grpc::CreateChannel(addr, InsecureChannelCredentials()));
63   }
64 
KillServer()65   void KillServer() { server_.reset(); }
66 
67  private:
68   std::unique_ptr<SubProcess> server_;
69 };
70 
TEST_F(CrashTest,KillBeforeWrite)71 TEST_F(CrashTest, KillBeforeWrite) {
72   auto stub = CreateServerAndStub();
73 
74   EchoRequest request;
75   EchoResponse response;
76   ClientContext context;
77   context.set_wait_for_ready(true);
78 
79   auto stream = stub->BidiStream(&context);
80 
81   request.set_message("Hello");
82   EXPECT_TRUE(stream->Write(request));
83   EXPECT_TRUE(stream->Read(&response));
84   EXPECT_EQ(response.message(), request.message());
85 
86   KillServer();
87 
88   request.set_message("You should be dead");
89   // This may succeed or fail depending on the state of the TCP connection
90   stream->Write(request);
91   // But the read will definitely fail
92   EXPECT_FALSE(stream->Read(&response));
93 
94   EXPECT_FALSE(stream->Finish().ok());
95 }
96 
TEST_F(CrashTest,KillAfterWrite)97 TEST_F(CrashTest, KillAfterWrite) {
98   auto stub = CreateServerAndStub();
99 
100   EchoRequest request;
101   EchoResponse response;
102   ClientContext context;
103   context.set_wait_for_ready(true);
104 
105   auto stream = stub->BidiStream(&context);
106 
107   request.set_message("Hello");
108   EXPECT_TRUE(stream->Write(request));
109   EXPECT_TRUE(stream->Read(&response));
110   EXPECT_EQ(response.message(), request.message());
111 
112   request.set_message("I'm going to kill you");
113   EXPECT_TRUE(stream->Write(request));
114 
115   KillServer();
116 
117   // This may succeed or fail depending on how quick the server was
118   stream->Read(&response);
119 
120   EXPECT_FALSE(stream->Finish().ok());
121 }
122 
123 }  // namespace
124 
125 }  // namespace testing
126 }  // namespace grpc
127 
main(int argc,char ** argv)128 int main(int argc, char** argv) {
129   std::string me = argv[0];
130   auto lslash = me.rfind('/');
131   if (lslash != std::string::npos) {
132     g_root = me.substr(0, lslash);
133   } else {
134     g_root = ".";
135   }
136 
137   grpc::testing::TestEnvironment env(&argc, argv);
138   ::testing::InitGoogleTest(&argc, argv);
139   // Order seems to matter on these tests: run three times to eliminate that
140   for (int i = 0; i < 3; i++) {
141     if (RUN_ALL_TESTS() != 0) {
142       return 1;
143     }
144   }
145   return 0;
146 }
147