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 | 5a05f51 | 2016-01-13 22:43:20 -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 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -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" |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [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 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 62 | class ServiceImpl GRPC_FINAL |
| 63 | : public ::grpc::testing::EchoTestService::Service { |
Yang Gao | a9b5de8 | 2015-05-26 23:35:21 -0700 | [diff] [blame] | 64 | public: |
| 65 | ServiceImpl() : bidi_stream_count_(0), response_stream_count_(0) {} |
| 66 | |
Craig Tiller | 7a317e5 | 2015-05-19 09:38:29 -0700 | [diff] [blame] | 67 | Status BidiStream(ServerContext* context, |
| 68 | ServerReaderWriter<EchoResponse, EchoRequest>* stream) |
| 69 | GRPC_OVERRIDE { |
Yang Gao | a9b5de8 | 2015-05-26 23:35:21 -0700 | [diff] [blame] | 70 | bidi_stream_count_++; |
Craig Tiller | 7a317e5 | 2015-05-19 09:38:29 -0700 | [diff] [blame] | 71 | 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 Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 77 | gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 78 | gpr_time_from_seconds(1, GPR_TIMESPAN))); |
Craig Tiller | fd7166d | 2015-05-19 10:23:03 -0700 | [diff] [blame] | 79 | } |
| 80 | return Status::OK; |
| 81 | } |
| 82 | |
| 83 | Status ResponseStream(ServerContext* context, const EchoRequest* request, |
| 84 | ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE { |
| 85 | EchoResponse response; |
Yang Gao | a9b5de8 | 2015-05-26 23:35:21 -0700 | [diff] [blame] | 86 | response_stream_count_++; |
Craig Tiller | fd7166d | 2015-05-19 10:23:03 -0700 | [diff] [blame] | 87 | 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 Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 92 | gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 93 | gpr_time_from_seconds(1, GPR_TIMESPAN))); |
Craig Tiller | 7a317e5 | 2015-05-19 09:38:29 -0700 | [diff] [blame] | 94 | } |
| 95 | return Status::OK; |
| 96 | } |
Yang Gao | a9b5de8 | 2015-05-26 23:35:21 -0700 | [diff] [blame] | 97 | |
| 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 Tiller | 7a317e5 | 2015-05-19 09:38:29 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 107 | class CrashTest : public ::testing::Test { |
| 108 | protected: |
| 109 | CrashTest() {} |
| 110 | |
Yang Gao | a9b5de8 | 2015-05-26 23:35:21 -0700 | [diff] [blame] | 111 | std::unique_ptr<Server> CreateServerAndClient(const std::string& mode) { |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 112 | auto port = grpc_pick_unused_port_or_die(); |
Craig Tiller | a969d7c | 2015-05-15 15:47:51 -0700 | [diff] [blame] | 113 | std::ostringstream addr_stream; |
| 114 | addr_stream << "localhost:" << port; |
| 115 | auto addr = addr_stream.str(); |
Yang Gao | a9b5de8 | 2015-05-26 23:35:21 -0700 | [diff] [blame] | 116 | client_.reset(new SubProcess({g_root + "/server_crash_test_client", |
| 117 | "--address=" + addr, "--mode=" + mode})); |
Craig Tiller | 7a317e5 | 2015-05-19 09:38:29 -0700 | [diff] [blame] | 118 | GPR_ASSERT(client_); |
| 119 | |
| 120 | ServerBuilder builder; |
| 121 | builder.AddListeningPort(addr, grpc::InsecureServerCredentials()); |
| 122 | builder.RegisterService(&service_); |
| 123 | return builder.BuildAndStart(); |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Yang Gao | a9b5de8 | 2015-05-26 23:35:21 -0700 | [diff] [blame] | 126 | 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 Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 131 | |
| 132 | private: |
Craig Tiller | 7a317e5 | 2015-05-19 09:38:29 -0700 | [diff] [blame] | 133 | std::unique_ptr<SubProcess> client_; |
| 134 | ServiceImpl service_; |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Craig Tiller | fd7166d | 2015-05-19 10:23:03 -0700 | [diff] [blame] | 137 | TEST_F(CrashTest, ResponseStream) { |
| 138 | auto server = CreateServerAndClient("response"); |
| 139 | |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 140 | gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 141 | gpr_time_from_seconds(5, GPR_TIMESPAN))); |
Craig Tiller | fd7166d | 2015-05-19 10:23:03 -0700 | [diff] [blame] | 142 | KillClient(); |
| 143 | server->Shutdown(); |
Yang Gao | a9b5de8 | 2015-05-26 23:35:21 -0700 | [diff] [blame] | 144 | GPR_ASSERT(HadOneResponseStream()); |
Craig Tiller | fd7166d | 2015-05-19 10:23:03 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST_F(CrashTest, BidiStream) { |
| 148 | auto server = CreateServerAndClient("bidi"); |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 149 | |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 150 | gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 151 | gpr_time_from_seconds(5, GPR_TIMESPAN))); |
Craig Tiller | 7a317e5 | 2015-05-19 09:38:29 -0700 | [diff] [blame] | 152 | KillClient(); |
| 153 | server->Shutdown(); |
Yang Gao | a9b5de8 | 2015-05-26 23:35:21 -0700 | [diff] [blame] | 154 | GPR_ASSERT(HadOneBidiStream()); |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } // namespace |
| 158 | |
| 159 | } // namespace testing |
| 160 | } // namespace grpc |
| 161 | |
| 162 | int 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 Quintas | 2bf574f | 2016-01-14 15:27:08 -0800 | [diff] [blame^] | 174 | } |