blob: 93257b2705c3174810481d924119c06425be5842 [file] [log] [blame]
Craig Tillerfbac5f12015-05-15 14:20:44 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tillerfbac5f12015-05-15 14:20:44 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tillerfbac5f12015-05-15 14:20:44 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tillerfbac5f12015-05-15 14:20:44 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tillerfbac5f12015-05-15 14:20:44 -070016 *
17 */
18
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080019#include <grpc/grpc.h>
David Garcia Quintasc79b0652016-07-27 21:11:58 -070020#include <grpc/support/log.h>
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080021#include <grpc/support/time.h>
Vijay Paic90a8562018-03-08 21:20:24 -080022#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>
Craig Tillerfbac5f12015-05-15 14:20:44 -070028
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080029#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
30#include "src/proto/grpc/testing/echo.grpc.pb.h"
yang-g9e2f90c2015-08-21 15:35:03 -070031#include "test/core/util/port.h"
32#include "test/core/util/test_config.h"
Craig Tillerfbac5f12015-05-15 14:20:44 -070033#include "test/cpp/util/subprocess.h"
34
Nicolas "Pixel" Noble3726e3d2017-05-10 18:33:12 +020035#include <gtest/gtest.h>
36
Craig Tiller1b4e3302015-12-17 16:35:00 -080037using grpc::testing::EchoRequest;
38using grpc::testing::EchoResponse;
Craig Tillerfbac5f12015-05-15 14:20:44 -070039using std::chrono::system_clock;
40
41static std::string g_root;
42
43namespace grpc {
44namespace testing {
45
46namespace {
47
Vijay Pai713c7b82016-11-01 16:33:18 -070048class ServiceImpl final : public ::grpc::testing::EchoTestService::Service {
Yang Gaoa9b5de82015-05-26 23:35:21 -070049 public:
50 ServiceImpl() : bidi_stream_count_(0), response_stream_count_(0) {}
51
Vijay Pai713c7b82016-11-01 16:33:18 -070052 Status BidiStream(
53 ServerContext* context,
54 ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
Yang Gaoa9b5de82015-05-26 23:35:21 -070055 bidi_stream_count_++;
Craig Tiller7a317e52015-05-19 09:38:29 -070056 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);
Craig Tiller677c50c2015-07-13 10:49:06 -070062 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
63 gpr_time_from_seconds(1, GPR_TIMESPAN)));
Craig Tillerfd7166d2015-05-19 10:23:03 -070064 }
65 return Status::OK;
66 }
67
68 Status ResponseStream(ServerContext* context, const EchoRequest* request,
Vijay Paic0b2acb2016-11-01 16:31:56 -070069 ServerWriter<EchoResponse>* writer) override {
Craig Tillerfd7166d2015-05-19 10:23:03 -070070 EchoResponse response;
Yang Gaoa9b5de82015-05-26 23:35:21 -070071 response_stream_count_++;
Craig Tillerfd7166d2015-05-19 10:23:03 -070072 for (int i = 0;; i++) {
73 std::ostringstream msg;
74 msg << "Hello " << i;
75 response.set_message(msg.str());
76 if (!writer->Write(response)) break;
Craig Tiller677c50c2015-07-13 10:49:06 -070077 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
78 gpr_time_from_seconds(1, GPR_TIMESPAN)));
Craig Tiller7a317e52015-05-19 09:38:29 -070079 }
80 return Status::OK;
81 }
Yang Gaoa9b5de82015-05-26 23:35:21 -070082
83 int bidi_stream_count() { return bidi_stream_count_; }
84
85 int response_stream_count() { return response_stream_count_; }
86
87 private:
88 int bidi_stream_count_;
89 int response_stream_count_;
Craig Tiller7a317e52015-05-19 09:38:29 -070090};
91
Craig Tillerfbac5f12015-05-15 14:20:44 -070092class CrashTest : public ::testing::Test {
93 protected:
94 CrashTest() {}
95
Yang Gaoa9b5de82015-05-26 23:35:21 -070096 std::unique_ptr<Server> CreateServerAndClient(const std::string& mode) {
Craig Tillerfbac5f12015-05-15 14:20:44 -070097 auto port = grpc_pick_unused_port_or_die();
Craig Tillera969d7c2015-05-15 15:47:51 -070098 std::ostringstream addr_stream;
99 addr_stream << "localhost:" << port;
100 auto addr = addr_stream.str();
Yang Gaoa9b5de82015-05-26 23:35:21 -0700101 client_.reset(new SubProcess({g_root + "/server_crash_test_client",
102 "--address=" + addr, "--mode=" + mode}));
Craig Tiller7a317e52015-05-19 09:38:29 -0700103 GPR_ASSERT(client_);
104
105 ServerBuilder builder;
106 builder.AddListeningPort(addr, grpc::InsecureServerCredentials());
107 builder.RegisterService(&service_);
108 return builder.BuildAndStart();
Craig Tillerfbac5f12015-05-15 14:20:44 -0700109 }
110
Yang Gaoa9b5de82015-05-26 23:35:21 -0700111 void KillClient() { client_.reset(); }
112
113 bool HadOneBidiStream() { return service_.bidi_stream_count() == 1; }
114
115 bool HadOneResponseStream() { return service_.response_stream_count() == 1; }
Craig Tillerfbac5f12015-05-15 14:20:44 -0700116
117 private:
Craig Tiller7a317e52015-05-19 09:38:29 -0700118 std::unique_ptr<SubProcess> client_;
119 ServiceImpl service_;
Craig Tillerfbac5f12015-05-15 14:20:44 -0700120};
121
Craig Tillerfd7166d2015-05-19 10:23:03 -0700122TEST_F(CrashTest, ResponseStream) {
123 auto server = CreateServerAndClient("response");
124
Craig Tiller677c50c2015-07-13 10:49:06 -0700125 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tillerfbcd9a72016-12-05 08:47:02 -0800126 gpr_time_from_seconds(60, GPR_TIMESPAN)));
Craig Tillerfd7166d2015-05-19 10:23:03 -0700127 KillClient();
128 server->Shutdown();
Yang Gaoa9b5de82015-05-26 23:35:21 -0700129 GPR_ASSERT(HadOneResponseStream());
Craig Tillerfd7166d2015-05-19 10:23:03 -0700130}
131
132TEST_F(CrashTest, BidiStream) {
133 auto server = CreateServerAndClient("bidi");
Craig Tillerfbac5f12015-05-15 14:20:44 -0700134
Craig Tiller677c50c2015-07-13 10:49:06 -0700135 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tillerfbcd9a72016-12-05 08:47:02 -0800136 gpr_time_from_seconds(60, GPR_TIMESPAN)));
Craig Tiller7a317e52015-05-19 09:38:29 -0700137 KillClient();
138 server->Shutdown();
Yang Gaoa9b5de82015-05-26 23:35:21 -0700139 GPR_ASSERT(HadOneBidiStream());
Craig Tillerfbac5f12015-05-15 14:20:44 -0700140}
141
142} // namespace
143
144} // namespace testing
145} // namespace grpc
146
147int main(int argc, char** argv) {
148 std::string me = argv[0];
149 auto lslash = me.rfind('/');
150 if (lslash != std::string::npos) {
151 g_root = me.substr(0, lslash);
152 } else {
153 g_root = ".";
154 }
155
156 grpc_test_init(argc, argv);
157 ::testing::InitGoogleTest(&argc, argv);
158 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800159}