| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <gtest/gtest.h> |
| 20 |
|
| 21 |
#include "absl/memory/memory.h" |
| 22 |
|
| 23 |
#include <grpc/grpc.h> |
| 24 |
#include <grpc/support/log.h> |
| 25 |
#include <grpc/support/time.h> |
| 26 |
#include <grpcpp/channel.h> |
| 27 |
#include <grpcpp/client_context.h> |
| 28 |
#include <grpcpp/create_channel.h> |
| 29 |
#include <grpcpp/server.h> |
| 30 |
#include <grpcpp/server_builder.h> |
| 31 |
#include <grpcpp/server_context.h> |
| 32 |
|
| 33 |
#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 34 |
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 35 |
#include "test/core/util/port.h" |
| 36 |
#include "test/core/util/test_config.h" |
| 37 |
#include "test/cpp/util/subprocess.h" |
| 38 |
|
| 39 |
using grpc::testing::EchoRequest; |
| 40 |
using grpc::testing::EchoResponse; |
| 41 |
|
| 42 |
static std::string g_root; |
| 43 |
|
| 44 |
namespace grpc { |
| 45 |
namespace testing { |
| 46 |
|
| 47 |
namespace { |
| 48 |
|
| 49 |
class ServiceImpl final : public grpc::testing::EchoTestService::Service { |
| 50 |
public: |
| 51 |
ServiceImpl() : bidi_stream_count_(0), response_stream_count_(0) {} |
| 52 |
|
| 53 |
Status BidiStream( |
| 54 |
ServerContext* /*context*/, |
| 55 |
ServerReaderWriter<EchoResponse, EchoRequest>* stream) override { |
| 56 |
bidi_stream_count_++; |
| 57 |
EchoRequest request; |
| 58 |
EchoResponse response; |
| 59 |
while (stream->Read(&request)) { |
| 60 |
gpr_log(GPR_INFO, "recv msg %s", request.message().c_str()); |
| 61 |
response.set_message(request.message()); |
| 62 |
stream->Write(response); |
| 63 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 64 |
gpr_time_from_seconds(1, GPR_TIMESPAN))); |
| 65 |
} |
| 66 |
return Status::OK; |
| 67 |
} |
| 68 |
|
| 69 |
Status ResponseStream(ServerContext* /*context*/, |
| 70 |
const EchoRequest* /*request*/, |
| 71 |
ServerWriter<EchoResponse>* writer) override { |
| 72 |
EchoResponse response; |
| 73 |
response_stream_count_++; |
| 74 |
for (int i = 0;; i++) { |
| 75 |
std::ostringstream msg; |
| 76 |
msg << "Hello " << i; |
| 77 |
response.set_message(msg.str()); |
| 78 |
if (!writer->Write(response)) break; |
| 79 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 80 |
gpr_time_from_seconds(1, GPR_TIMESPAN))); |
| 81 |
} |
| 82 |
return Status::OK; |
| 83 |
} |
| 84 |
|
| 85 |
int bidi_stream_count() { return bidi_stream_count_; } |
| 86 |
|
| 87 |
int response_stream_count() { return response_stream_count_; } |
| 88 |
|
| 89 |
private: |
| 90 |
int bidi_stream_count_; |
| 91 |
int response_stream_count_; |
| 92 |
}; |
| 93 |
|
| 94 |
class CrashTest : public ::testing::Test { |
| 95 |
protected: |
| 96 |
CrashTest() {} |
| 97 |
|
| 98 |
std::unique_ptr<Server> CreateServerAndClient(const std::string& mode) { |
| 99 |
auto port = grpc_pick_unused_port_or_die(); |
| 100 |
std::ostringstream addr_stream; |
| 101 |
addr_stream << "localhost:" << port; |
| 102 |
auto addr = addr_stream.str(); |
| 103 |
client_ = std::make_unique<SubProcess>( |
| 104 |
std::vector<std::string>({g_root + "/server_crash_test_client", |
| 105 |
"--address=" + addr, "--mode=" + mode})); |
| 106 |
GPR_ASSERT(client_); |
| 107 |
|
| 108 |
ServerBuilder builder; |
| 109 |
builder.AddListeningPort(addr, grpc::InsecureServerCredentials()); |
| 110 |
builder.RegisterService(&service_); |
| 111 |
return builder.BuildAndStart(); |
| 112 |
} |
| 113 |
|
| 114 |
void KillClient() { client_.reset(); } |
| 115 |
|
| 116 |
bool HadOneBidiStream() { return service_.bidi_stream_count() == 1; } |
| 117 |
|
| 118 |
bool HadOneResponseStream() { return service_.response_stream_count() == 1; } |
| 119 |
|
| 120 |
private: |
| 121 |
std::unique_ptr<SubProcess> client_; |
| 122 |
ServiceImpl service_; |
| 123 |
}; |
| 124 |
|
| 125 |
TEST_F(CrashTest, ResponseStream) { |
| 126 |
auto server = CreateServerAndClient("response"); |
| 127 |
|
| 128 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 129 |
gpr_time_from_seconds(60, GPR_TIMESPAN))); |
| 130 |
KillClient(); |
| 131 |
server->Shutdown(); |
| 132 |
GPR_ASSERT(HadOneResponseStream()); |
| 133 |
} |
| 134 |
|
| 135 |
TEST_F(CrashTest, BidiStream) { |
| 136 |
auto server = CreateServerAndClient("bidi"); |
| 137 |
|
| 138 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 139 |
gpr_time_from_seconds(60, GPR_TIMESPAN))); |
| 140 |
KillClient(); |
| 141 |
server->Shutdown(); |
| 142 |
GPR_ASSERT(HadOneBidiStream()); |
| 143 |
} |
| 144 |
|
| 145 |
} // namespace |
| 146 |
|
| 147 |
} // namespace testing |
| 148 |
} // namespace grpc |
| 149 |
|
| 150 |
int main(int argc, char** argv) { |
| 151 |
std::string me = argv[0]; |
| 152 |
auto lslash = me.rfind('/'); |
| 153 |
if (lslash != std::string::npos) { |
| 154 |
g_root = me.substr(0, lslash); |
| 155 |
} else { |
| 156 |
g_root = "."; |
| 157 |
} |
| 158 |
|
| 159 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 160 |
::testing::InitGoogleTest(&argc, argv); |
| 161 |
return RUN_ALL_TESTS(); |
| 162 |
} |