blob: 9bf94236667fe818b521be62e24d45c4699e6487 [file] [log] [blame]
Craig Tillerfbac5f12015-05-15 14:20:44 -07001/*
2 *
murgatroid99ace28d32016-01-14 10:12:10 -08003 * Copyright 2015-2016, Google Inc.
Craig Tillerfbac5f12015-05-15 14:20:44 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
yang-g8c2be9f2015-08-19 16:28:09 -070034#include <grpc++/channel.h>
Craig Tillerfbac5f12015-05-15 14:20:44 -070035#include <grpc++/client_context.h>
36#include <grpc++/create_channel.h>
Craig Tillerfbac5f12015-05-15 14:20:44 -070037#include <grpc++/server.h>
38#include <grpc++/server_builder.h>
39#include <grpc++/server_context.h>
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080040#include <grpc/grpc.h>
41#include <grpc/support/thd.h>
42#include <grpc/support/time.h>
Craig Tillerfbac5f12015-05-15 14:20:44 -070043#include <gtest/gtest.h>
44
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080045#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
46#include "src/proto/grpc/testing/echo.grpc.pb.h"
yang-g9e2f90c2015-08-21 15:35:03 -070047#include "test/core/util/port.h"
48#include "test/core/util/test_config.h"
Craig Tillerfbac5f12015-05-15 14:20:44 -070049#include "test/cpp/util/subprocess.h"
50
Craig Tiller1b4e3302015-12-17 16:35:00 -080051using grpc::testing::EchoRequest;
52using grpc::testing::EchoResponse;
Craig Tillerfbac5f12015-05-15 14:20:44 -070053using std::chrono::system_clock;
54
55static std::string g_root;
56
57namespace grpc {
58namespace testing {
59
60namespace {
61
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080062class ServiceImpl GRPC_FINAL
63 : public ::grpc::testing::EchoTestService::Service {
Yang Gaoa9b5de82015-05-26 23:35:21 -070064 public:
65 ServiceImpl() : bidi_stream_count_(0), response_stream_count_(0) {}
66
Craig Tiller7a317e52015-05-19 09:38:29 -070067 Status BidiStream(ServerContext* context,
68 ServerReaderWriter<EchoResponse, EchoRequest>* stream)
69 GRPC_OVERRIDE {
Yang Gaoa9b5de82015-05-26 23:35:21 -070070 bidi_stream_count_++;
Craig Tiller7a317e52015-05-19 09:38:29 -070071 EchoRequest request;
72 EchoResponse response;
73 while (stream->Read(&request)) {
74 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
75 response.set_message(request.message());
76 stream->Write(response);
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 Tillerfd7166d2015-05-19 10:23:03 -070079 }
80 return Status::OK;
81 }
82
83 Status ResponseStream(ServerContext* context, const EchoRequest* request,
84 ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE {
85 EchoResponse response;
Yang Gaoa9b5de82015-05-26 23:35:21 -070086 response_stream_count_++;
Craig Tillerfd7166d2015-05-19 10:23:03 -070087 for (int i = 0;; i++) {
88 std::ostringstream msg;
89 msg << "Hello " << i;
90 response.set_message(msg.str());
91 if (!writer->Write(response)) break;
Craig Tiller677c50c2015-07-13 10:49:06 -070092 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
93 gpr_time_from_seconds(1, GPR_TIMESPAN)));
Craig Tiller7a317e52015-05-19 09:38:29 -070094 }
95 return Status::OK;
96 }
Yang Gaoa9b5de82015-05-26 23:35:21 -070097
98 int bidi_stream_count() { return bidi_stream_count_; }
99
100 int response_stream_count() { return response_stream_count_; }
101
102 private:
103 int bidi_stream_count_;
104 int response_stream_count_;
Craig Tiller7a317e52015-05-19 09:38:29 -0700105};
106
Craig Tillerfbac5f12015-05-15 14:20:44 -0700107class CrashTest : public ::testing::Test {
108 protected:
109 CrashTest() {}
110
Yang Gaoa9b5de82015-05-26 23:35:21 -0700111 std::unique_ptr<Server> CreateServerAndClient(const std::string& mode) {
Craig Tillerfbac5f12015-05-15 14:20:44 -0700112 auto port = grpc_pick_unused_port_or_die();
Craig Tillera969d7c2015-05-15 15:47:51 -0700113 std::ostringstream addr_stream;
114 addr_stream << "localhost:" << port;
115 auto addr = addr_stream.str();
Yang Gaoa9b5de82015-05-26 23:35:21 -0700116 client_.reset(new SubProcess({g_root + "/server_crash_test_client",
117 "--address=" + addr, "--mode=" + mode}));
Craig Tiller7a317e52015-05-19 09:38:29 -0700118 GPR_ASSERT(client_);
119
120 ServerBuilder builder;
121 builder.AddListeningPort(addr, grpc::InsecureServerCredentials());
122 builder.RegisterService(&service_);
123 return builder.BuildAndStart();
Craig Tillerfbac5f12015-05-15 14:20:44 -0700124 }
125
Yang Gaoa9b5de82015-05-26 23:35:21 -0700126 void KillClient() { client_.reset(); }
127
128 bool HadOneBidiStream() { return service_.bidi_stream_count() == 1; }
129
130 bool HadOneResponseStream() { return service_.response_stream_count() == 1; }
Craig Tillerfbac5f12015-05-15 14:20:44 -0700131
132 private:
Craig Tiller7a317e52015-05-19 09:38:29 -0700133 std::unique_ptr<SubProcess> client_;
134 ServiceImpl service_;
Craig Tillerfbac5f12015-05-15 14:20:44 -0700135};
136
Craig Tillerfd7166d2015-05-19 10:23:03 -0700137TEST_F(CrashTest, ResponseStream) {
138 auto server = CreateServerAndClient("response");
139
Craig Tiller677c50c2015-07-13 10:49:06 -0700140 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
141 gpr_time_from_seconds(5, GPR_TIMESPAN)));
Craig Tillerfd7166d2015-05-19 10:23:03 -0700142 KillClient();
143 server->Shutdown();
Yang Gaoa9b5de82015-05-26 23:35:21 -0700144 GPR_ASSERT(HadOneResponseStream());
Craig Tillerfd7166d2015-05-19 10:23:03 -0700145}
146
147TEST_F(CrashTest, BidiStream) {
148 auto server = CreateServerAndClient("bidi");
Craig Tillerfbac5f12015-05-15 14:20:44 -0700149
Craig Tiller677c50c2015-07-13 10:49:06 -0700150 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
151 gpr_time_from_seconds(5, GPR_TIMESPAN)));
Craig Tiller7a317e52015-05-19 09:38:29 -0700152 KillClient();
153 server->Shutdown();
Yang Gaoa9b5de82015-05-26 23:35:21 -0700154 GPR_ASSERT(HadOneBidiStream());
Craig Tillerfbac5f12015-05-15 14:20:44 -0700155}
156
157} // namespace
158
159} // namespace testing
160} // namespace grpc
161
162int main(int argc, char** argv) {
163 std::string me = argv[0];
164 auto lslash = me.rfind('/');
165 if (lslash != std::string::npos) {
166 g_root = me.substr(0, lslash);
167 } else {
168 g_root = ".";
169 }
170
171 grpc_test_init(argc, argv);
172 ::testing::InitGoogleTest(&argc, argv);
173 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800174}