1#!/usr/bin/env ruby 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 17require_relative './end2end_common' 18 19# Test client. Sends RPC's as normal but process also has signal handlers 20class SigHandlingClientController < ClientControl::ClientController::Service 21 def initialize(srv, stub) 22 @srv = srv 23 @stub = stub 24 end 25 26 def do_echo_rpc(req, _) 27 response = @stub.echo(Echo::EchoRequest.new(request: req.request)) 28 fail 'bad response' unless response.response == req.request 29 ClientControl::Void.new 30 end 31 32 def shutdown(_, _) 33 # Spawn a new thread because RpcServer#stop is 34 # synchronous and blocks until either this RPC has finished, 35 # or the server's "poll_period" seconds have passed. 36 @shutdown_thread = Thread.new do 37 @srv.stop 38 end 39 ClientControl::Void.new 40 end 41 42 def join_shutdown_thread 43 @shutdown_thread.join 44 end 45end 46 47def main 48 client_control_port = '' 49 server_port = '' 50 OptionParser.new do |opts| 51 opts.on('--client_control_port=P', String) do |p| 52 client_control_port = p 53 end 54 opts.on('--server_port=P', String) do |p| 55 server_port = p 56 end 57 end.parse! 58 59 # The "shutdown" RPC should end very quickly. 60 # Allow a few seconds to be safe. 61 srv = new_rpc_server_for_testing(poll_period: 3) 62 srv.add_http2_port("0.0.0.0:#{client_control_port}", 63 :this_port_is_insecure) 64 stub = Echo::EchoServer::Stub.new("localhost:#{server_port}", 65 :this_channel_is_insecure) 66 control_service = SigHandlingClientController.new(srv, stub) 67 srv.handle(control_service) 68 server_thread = Thread.new do 69 srv.run_till_terminated_or_interrupted(['int']) 70 end 71 srv.wait_till_running 72 # send a first RPC to notify the parent process that we've started 73 stub.echo(Echo::EchoRequest.new(request: 'client/child started')) 74 server_thread.join 75 control_service.join_shutdown_thread 76end 77 78main 79