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 ServiceImpl GRPC_FINAL |
| 63 : public ::grpc::testing::EchoTestService::Service { |
| 64 public: |
| 65 ServiceImpl() : bidi_stream_count_(0), response_stream_count_(0) {} |
| 66 |
| 67 Status BidiStream(ServerContext* context, |
| 68 ServerReaderWriter<EchoResponse, EchoRequest>* stream) |
| 69 GRPC_OVERRIDE { |
| 70 bidi_stream_count_++; |
| 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); |
| 77 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 78 gpr_time_from_seconds(1, GPR_TIMESPAN))); |
| 79 } |
| 80 return Status::OK; |
| 81 } |
| 82 |
| 83 Status ResponseStream(ServerContext* context, const EchoRequest* request, |
| 84 ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE { |
| 85 EchoResponse response; |
| 86 response_stream_count_++; |
| 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; |
| 92 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 93 gpr_time_from_seconds(1, GPR_TIMESPAN))); |
| 94 } |
| 95 return Status::OK; |
| 96 } |
| 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_; |
| 105 }; |
| 106 |
| 107 class CrashTest : public ::testing::Test { |
| 108 protected: |
| 109 CrashTest() {} |
| 110 |
| 111 std::unique_ptr<Server> CreateServerAndClient(const std::string& mode) { |
| 112 auto port = grpc_pick_unused_port_or_die(); |
| 113 std::ostringstream addr_stream; |
| 114 addr_stream << "localhost:" << port; |
| 115 auto addr = addr_stream.str(); |
| 116 client_.reset(new SubProcess({g_root + "/server_crash_test_client", |
| 117 "--address=" + addr, "--mode=" + mode})); |
| 118 GPR_ASSERT(client_); |
| 119 |
| 120 ServerBuilder builder; |
| 121 builder.AddListeningPort(addr, grpc::InsecureServerCredentials()); |
| 122 builder.RegisterService(&service_); |
| 123 return builder.BuildAndStart(); |
| 124 } |
| 125 |
| 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; } |
| 131 |
| 132 private: |
| 133 std::unique_ptr<SubProcess> client_; |
| 134 ServiceImpl service_; |
| 135 }; |
| 136 |
| 137 TEST_F(CrashTest, ResponseStream) { |
| 138 auto server = CreateServerAndClient("response"); |
| 139 |
| 140 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 141 gpr_time_from_seconds(5, GPR_TIMESPAN))); |
| 142 KillClient(); |
| 143 server->Shutdown(); |
| 144 GPR_ASSERT(HadOneResponseStream()); |
| 145 } |
| 146 |
| 147 TEST_F(CrashTest, BidiStream) { |
| 148 auto server = CreateServerAndClient("bidi"); |
| 149 |
| 150 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 151 gpr_time_from_seconds(5, GPR_TIMESPAN))); |
| 152 KillClient(); |
| 153 server->Shutdown(); |
| 154 GPR_ASSERT(HadOneBidiStream()); |
| 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(); |
| 174 } |
OLD | NEW |