Debian Codegraph @

test/cpp/end2end/client_crash_test.cc

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