OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 34 #include <grpc++/channel.h> |
| 35 #include <grpc++/client_context.h> |
| 36 #include <grpc++/create_channel.h> |
| 37 #include <grpc++/server.h> |
| 38 #include <grpc++/server_builder.h> |
| 39 #include <grpc++/server_context.h> |
| 40 #include <grpc/grpc.h> |
| 41 #include <grpc/support/thd.h> |
| 42 #include <grpc/support/time.h> |
| 43 #include <gtest/gtest.h> |
| 44 |
| 45 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 46 #include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 47 #include "test/core/util/port.h" |
| 48 #include "test/core/util/test_config.h" |
| 49 #include "test/cpp/util/subprocess.h" |
| 50 |
| 51 using grpc::testing::EchoRequest; |
| 52 using grpc::testing::EchoResponse; |
| 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 |
| 66 std::unique_ptr<grpc::testing::EchoTestService::Stub> CreateServerAndStub() { |
| 67 auto port = grpc_pick_unused_port_or_die(); |
| 68 std::ostringstream addr_stream; |
| 69 addr_stream << "localhost:" << port; |
| 70 auto addr = addr_stream.str(); |
| 71 server_.reset(new SubProcess({ |
| 72 g_root + "/client_crash_test_server", "--address=" + addr, |
| 73 })); |
| 74 GPR_ASSERT(server_); |
| 75 return grpc::testing::EchoTestService::NewStub( |
| 76 CreateChannel(addr, InsecureChannelCredentials())); |
| 77 } |
| 78 |
| 79 void KillServer() { server_.reset(); } |
| 80 |
| 81 private: |
| 82 std::unique_ptr<SubProcess> server_; |
| 83 }; |
| 84 |
| 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 |
| 107 EXPECT_FALSE(stream->Finish().ok()); |
| 108 } |
| 109 |
| 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 |
| 129 // This may succeed or fail depending on how quick the server was |
| 130 stream->Read(&response); |
| 131 |
| 132 EXPECT_FALSE(stream->Finish().ok()); |
| 133 } |
| 134 |
| 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); |
| 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; |
| 158 } |
OLD | NEW |