blob: d0efabc5ca20729da72bc30cdcd8566ce2eae3be [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 Kuchibhotlab0d0c8e2016-01-13 22:52:17 -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
Craig Tiller1b4e3302015-12-17 16:35:00 -080045#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
46#include "src/proto/grpc/testing/echo.grpc.pb.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080047#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
62class CrashTest : public ::testing::Test {
63 protected:
64 CrashTest() {}
65
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080066 std::unique_ptr<grpc::testing::EchoTestService::Stub> CreateServerAndStub() {
Craig Tillerfbac5f12015-05-15 14:20:44 -070067 auto port = grpc_pick_unused_port_or_die();
Craig Tillera969d7c2015-05-15 15:47:51 -070068 std::ostringstream addr_stream;
69 addr_stream << "localhost:" << port;
70 auto addr = addr_stream.str();
Craig Tillerfbac5f12015-05-15 14:20:44 -070071 server_.reset(new SubProcess({
Craig Tillerd6c98df2015-08-18 09:33:44 -070072 g_root + "/client_crash_test_server", "--address=" + addr,
Craig Tillerfbac5f12015-05-15 14:20:44 -070073 }));
74 GPR_ASSERT(server_);
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080075 return grpc::testing::EchoTestService::NewStub(
Julien Boeufe5adc0e2015-10-12 14:08:10 -070076 CreateChannel(addr, InsecureChannelCredentials()));
Craig Tillerfbac5f12015-05-15 14:20:44 -070077 }
78
Craig Tillerd6c98df2015-08-18 09:33:44 -070079 void KillServer() { server_.reset(); }
Craig Tillerfbac5f12015-05-15 14:20:44 -070080
81 private:
82 std::unique_ptr<SubProcess> server_;
83};
84
Craig Tillerf54f2d02015-05-29 12:31:05 -070085TEST_F(CrashTest, KillBeforeWrite) {
86 auto stub = CreateServerAndStub();
87
88 EchoRequest request;
89 EchoResponse response;
90 ClientContext context;
91
92 auto stream = stub->BidiStream(&context);
93
94 request.set_message("Hello");
95 EXPECT_TRUE(stream->Write(request));
96 EXPECT_TRUE(stream->Read(&response));
97 EXPECT_EQ(response.message(), request.message());
98
99 KillServer();
100
101 request.set_message("You should be dead");
102 // This may succeed or fail depending on the state of the TCP connection
103 stream->Write(request);
104 // But the read will definitely fail
105 EXPECT_FALSE(stream->Read(&response));
106
Craig Tillerbf8ac3f2015-06-17 09:15:48 -0700107 EXPECT_FALSE(stream->Finish().ok());
Craig Tillerf54f2d02015-05-29 12:31:05 -0700108}
109
Craig Tillerfbac5f12015-05-15 14:20:44 -0700110TEST_F(CrashTest, KillAfterWrite) {
111 auto stub = CreateServerAndStub();
112
113 EchoRequest request;
114 EchoResponse response;
115 ClientContext context;
116
117 auto stream = stub->BidiStream(&context);
118
119 request.set_message("Hello");
120 EXPECT_TRUE(stream->Write(request));
121 EXPECT_TRUE(stream->Read(&response));
122 EXPECT_EQ(response.message(), request.message());
123
124 request.set_message("I'm going to kill you");
125 EXPECT_TRUE(stream->Write(request));
126
127 KillServer();
128
Craig Tiller8cca4692015-06-22 13:50:59 -0700129 // This may succeed or fail depending on how quick the server was
130 stream->Read(&response);
Craig Tillerfbac5f12015-05-15 14:20:44 -0700131
Yang Gaoc1a2c312015-06-16 10:59:46 -0700132 EXPECT_FALSE(stream->Finish().ok());
Craig Tillerfbac5f12015-05-15 14:20:44 -0700133}
134
Craig Tillerfbac5f12015-05-15 14:20:44 -0700135} // namespace
136
137} // namespace testing
138} // namespace grpc
139
140int main(int argc, char** argv) {
141 std::string me = argv[0];
142 auto lslash = me.rfind('/');
143 if (lslash != std::string::npos) {
144 g_root = me.substr(0, lslash);
145 } else {
146 g_root = ".";
147 }
148
149 grpc_test_init(argc, argv);
150 ::testing::InitGoogleTest(&argc, argv);
Craig Tillerf54f2d02015-05-29 12:31:05 -0700151 // Order seems to matter on these tests: run three times to eliminate that
152 for (int i = 0; i < 3; i++) {
153 if (RUN_ALL_TESTS() != 0) {
154 return 1;
155 }
156 }
157 return 0;
murgatroid99ace28d32016-01-14 10:12:10 -0800158}