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