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