mirror of https://github.com/grpc/grpc.git
parent
102d5d88a6
commit
39ac83a49e
8 changed files with 355 additions and 2 deletions
@ -0,0 +1,61 @@ |
||||
#!/usr/bin/env ruby |
||||
|
||||
# Copyright 2015 gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
require_relative './end2end_common' |
||||
|
||||
# Test client. Sends RPC's as normal but process also has signal handlers |
||||
class SigHandlingClientController < ClientControl::ClientController::Service |
||||
def initialize(stub) |
||||
@stub = stub |
||||
end |
||||
|
||||
def do_echo_rpc(req, _) |
||||
response = @stub.echo(Echo::EchoRequest.new(request: req.request)) |
||||
fail 'bad response' unless response.response == req.request |
||||
ClientControl::Void.new |
||||
end |
||||
end |
||||
|
||||
def main |
||||
client_control_port = '' |
||||
server_port = '' |
||||
OptionParser.new do |opts| |
||||
opts.on('--client_control_port=P', String) do |p| |
||||
client_control_port = p |
||||
end |
||||
opts.on('--server_port=P', String) do |p| |
||||
server_port = p |
||||
end |
||||
end.parse! |
||||
|
||||
# Allow a few seconds to be safe. |
||||
srv = new_rpc_server_for_testing |
||||
srv.add_http2_port("0.0.0.0:#{client_control_port}", |
||||
:this_port_is_insecure) |
||||
stub = Echo::EchoServer::Stub.new("localhost:#{server_port}", |
||||
:this_channel_is_insecure) |
||||
control_service = SigHandlingClientController.new(stub) |
||||
srv.handle(control_service) |
||||
server_thread = Thread.new do |
||||
srv.run_till_terminated_or_interrupted(['int']) |
||||
end |
||||
srv.wait_till_running |
||||
# send a first RPC to notify the parent process that we've started |
||||
stub.echo(Echo::EchoRequest.new(request: 'client/child started')) |
||||
server_thread.join |
||||
end |
||||
|
||||
main |
@ -0,0 +1,83 @@ |
||||
#!/usr/bin/env ruby |
||||
|
||||
# Copyright 2016 gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# smoke test for a grpc-using app that receives and |
||||
# handles process-ending signals |
||||
|
||||
require_relative './end2end_common' |
||||
|
||||
# A service that calls back it's received_rpc_callback |
||||
# upon receiving an RPC. Used for synchronization/waiting |
||||
# for child process to start. |
||||
class ClientStartedService < Echo::EchoServer::Service |
||||
def initialize(received_rpc_callback) |
||||
@received_rpc_callback = received_rpc_callback |
||||
end |
||||
|
||||
def echo(echo_req, _) |
||||
@received_rpc_callback.call unless @received_rpc_callback.nil? |
||||
@received_rpc_callback = nil |
||||
Echo::EchoReply.new(response: echo_req.request) |
||||
end |
||||
end |
||||
|
||||
def main |
||||
STDERR.puts 'start server' |
||||
client_started = false |
||||
client_started_mu = Mutex.new |
||||
client_started_cv = ConditionVariable.new |
||||
received_rpc_callback = proc do |
||||
client_started_mu.synchronize do |
||||
client_started = true |
||||
client_started_cv.signal |
||||
end |
||||
end |
||||
|
||||
client_started_service = ClientStartedService.new(received_rpc_callback) |
||||
server_runner = ServerRunner.new(client_started_service) |
||||
server_port = server_runner.run |
||||
STDERR.puts 'start client' |
||||
control_stub, client_pid = start_client('graceful_sig_handling_client.rb', server_port) |
||||
|
||||
client_started_mu.synchronize do |
||||
client_started_cv.wait(client_started_mu) until client_started |
||||
end |
||||
|
||||
control_stub.do_echo_rpc( |
||||
ClientControl::DoEchoRpcRequest.new(request: 'hello')) |
||||
|
||||
STDERR.puts 'killing client' |
||||
Process.kill('SIGINT', client_pid) |
||||
Process.wait(client_pid) |
||||
client_exit_status = $CHILD_STATUS |
||||
|
||||
if client_exit_status.exited? |
||||
if client_exit_status.exitstatus != 0 |
||||
STDERR.puts 'Client did not close gracefully' |
||||
exit(1) |
||||
end |
||||
else |
||||
STDERR.puts 'Client did not close gracefully' |
||||
exit(1) |
||||
end |
||||
|
||||
STDERR.puts 'Client ended gracefully' |
||||
|
||||
# no need to call cleanup, client should already be dead |
||||
server_runner.stop |
||||
end |
||||
|
||||
main |
@ -0,0 +1,78 @@ |
||||
#!/usr/bin/env ruby |
||||
|
||||
# Copyright 2015 gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
require_relative './end2end_common' |
||||
|
||||
# Test client. Sends RPC's as normal but process also has signal handlers |
||||
class SigHandlingClientController < ClientControl::ClientController::Service |
||||
def initialize(srv, stub) |
||||
@srv = srv |
||||
@stub = stub |
||||
end |
||||
|
||||
def do_echo_rpc(req, _) |
||||
response = @stub.echo(Echo::EchoRequest.new(request: req.request)) |
||||
fail 'bad response' unless response.response == req.request |
||||
ClientControl::Void.new |
||||
end |
||||
|
||||
def shutdown(_, _) |
||||
# Spawn a new thread because RpcServer#stop is |
||||
# synchronous and blocks until either this RPC has finished, |
||||
# or the server's "poll_period" seconds have passed. |
||||
@shutdown_thread = Thread.new do |
||||
@srv.stop |
||||
end |
||||
ClientControl::Void.new |
||||
end |
||||
|
||||
def join_shutdown_thread |
||||
@shutdown_thread.join |
||||
end |
||||
end |
||||
|
||||
def main |
||||
client_control_port = '' |
||||
server_port = '' |
||||
OptionParser.new do |opts| |
||||
opts.on('--client_control_port=P', String) do |p| |
||||
client_control_port = p |
||||
end |
||||
opts.on('--server_port=P', String) do |p| |
||||
server_port = p |
||||
end |
||||
end.parse! |
||||
|
||||
# The "shutdown" RPC should end very quickly. |
||||
# Allow a few seconds to be safe. |
||||
srv = new_rpc_server_for_testing(poll_period: 3) |
||||
srv.add_http2_port("0.0.0.0:#{client_control_port}", |
||||
:this_port_is_insecure) |
||||
stub = Echo::EchoServer::Stub.new("localhost:#{server_port}", |
||||
:this_channel_is_insecure) |
||||
control_service = SigHandlingClientController.new(srv, stub) |
||||
srv.handle(control_service) |
||||
server_thread = Thread.new do |
||||
srv.run_till_terminated_or_interrupted(['int']) |
||||
end |
||||
srv.wait_till_running |
||||
# send a first RPC to notify the parent process that we've started |
||||
stub.echo(Echo::EchoRequest.new(request: 'client/child started')) |
||||
server_thread.join |
||||
control_service.join_shutdown_thread |
||||
end |
||||
|
||||
main |
@ -0,0 +1,62 @@ |
||||
#!/usr/bin/env ruby |
||||
|
||||
# Copyright 2016 gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# smoke test for a grpc-using app that receives and |
||||
# handles process-ending signals |
||||
|
||||
require_relative './end2end_common' |
||||
|
||||
# A service that calls back it's received_rpc_callback |
||||
# upon receiving an RPC. Used for synchronization/waiting |
||||
# for child process to start. |
||||
class ClientStartedService < Echo::EchoServer::Service |
||||
def initialize(received_rpc_callback) |
||||
@received_rpc_callback = received_rpc_callback |
||||
end |
||||
|
||||
def echo(echo_req, _) |
||||
@received_rpc_callback.call unless @received_rpc_callback.nil? |
||||
@received_rpc_callback = nil |
||||
Echo::EchoReply.new(response: echo_req.request) |
||||
end |
||||
end |
||||
|
||||
def main |
||||
STDERR.puts 'start server' |
||||
client_started = false |
||||
client_started_mu = Mutex.new |
||||
client_started_cv = ConditionVariable.new |
||||
received_rpc_callback = proc do |
||||
client_started_mu.synchronize do |
||||
client_started = true |
||||
client_started_cv.signal |
||||
end |
||||
end |
||||
|
||||
client_started_service = ClientStartedService.new(received_rpc_callback) |
||||
server_runner = ServerRunner.new(client_started_service) |
||||
server_port = server_runner.run |
||||
STDERR.puts 'start client' |
||||
control_stub, client_pid = start_client('./graceful_sig_stop_client.rb', server_port) |
||||
|
||||
client_started_mu.synchronize do |
||||
client_started_cv.wait(client_started_mu) until client_started |
||||
end |
||||
|
||||
cleanup(control_stub, client_pid, server_runner) |
||||
end |
||||
|
||||
main |
Loading…
Reference in new issue