Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame^] | 3 | * Copyright 2015-2016, Google Inc. |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 4 | * 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-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 34 | #include <grpc++/channel.h> |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 35 | #include <grpc++/client_context.h> |
| 36 | #include <grpc++/create_channel.h> |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 37 | #include <grpc++/server.h> |
| 38 | #include <grpc++/server_builder.h> |
| 39 | #include <grpc++/server_context.h> |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 40 | #include <grpc/grpc.h> |
| 41 | #include <grpc/support/thd.h> |
| 42 | #include <grpc/support/time.h> |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 43 | #include <gtest/gtest.h> |
| 44 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 45 | #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 46 | #include "src/proto/grpc/testing/echo.grpc.pb.h" |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 47 | #include "test/core/util/port.h" |
| 48 | #include "test/core/util/test_config.h" |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 49 | #include "test/cpp/util/subprocess.h" |
| 50 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 51 | using grpc::testing::EchoRequest; |
| 52 | using grpc::testing::EchoResponse; |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 53 | using std::chrono::system_clock; |
| 54 | |
| 55 | static std::string g_root; |
| 56 | |
| 57 | namespace grpc { |
| 58 | namespace testing { |
| 59 | |
| 60 | namespace { |
| 61 | |
| 62 | class CrashTest : public ::testing::Test { |
| 63 | protected: |
| 64 | CrashTest() {} |
| 65 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 66 | std::unique_ptr<grpc::testing::EchoTestService::Stub> CreateServerAndStub() { |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 67 | auto port = grpc_pick_unused_port_or_die(); |
Craig Tiller | a969d7c | 2015-05-15 15:47:51 -0700 | [diff] [blame] | 68 | std::ostringstream addr_stream; |
| 69 | addr_stream << "localhost:" << port; |
| 70 | auto addr = addr_stream.str(); |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 71 | server_.reset(new SubProcess({ |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 72 | g_root + "/client_crash_test_server", "--address=" + addr, |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 73 | })); |
| 74 | GPR_ASSERT(server_); |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 75 | return grpc::testing::EchoTestService::NewStub( |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 76 | CreateChannel(addr, InsecureChannelCredentials())); |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 79 | void KillServer() { server_.reset(); } |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | std::unique_ptr<SubProcess> server_; |
| 83 | }; |
| 84 | |
Craig Tiller | f54f2d0 | 2015-05-29 12:31:05 -0700 | [diff] [blame] | 85 | TEST_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 Tiller | bf8ac3f | 2015-06-17 09:15:48 -0700 | [diff] [blame] | 107 | EXPECT_FALSE(stream->Finish().ok()); |
Craig Tiller | f54f2d0 | 2015-05-29 12:31:05 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 110 | TEST_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 Tiller | 8cca469 | 2015-06-22 13:50:59 -0700 | [diff] [blame] | 129 | // This may succeed or fail depending on how quick the server was |
| 130 | stream->Read(&response); |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 131 | |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 132 | EXPECT_FALSE(stream->Finish().ok()); |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 135 | } // namespace |
| 136 | |
| 137 | } // namespace testing |
| 138 | } // namespace grpc |
| 139 | |
| 140 | int 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 Tiller | f54f2d0 | 2015-05-29 12:31:05 -0700 | [diff] [blame] | 151 | // 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; |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame^] | 158 | } |