blob: 41b5f334be3b286c11cb5d09155fed585293912c [file] [log] [blame]
Alexander Polcync44c16e2017-03-14 17:44:21 -07001#!/usr/bin/env ruby
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# Copyright 2015 gRPC authors.
Alexander Polcync44c16e2017-03-14 17:44:21 -07004#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005# 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
Alexander Polcync44c16e2017-03-14 17:44:21 -07008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# http://www.apache.org/licenses/LICENSE-2.0
Alexander Polcync44c16e2017-03-14 17:44:21 -070010#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011# 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.
Alexander Polcync44c16e2017-03-14 17:44:21 -070016
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070017require_relative './end2end_common'
Alexander Polcync44c16e2017-03-14 17:44:21 -070018
Alexander Polcyn4e606752017-03-19 23:32:54 -070019# Test client. Sends RPC's as normal but process also has signal handlers
Alexander Polcync44c16e2017-03-14 17:44:21 -070020class SigHandlingClientController < ClientControl::ClientController::Service
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070021 def initialize(srv, stub)
Alexander Polcync44c16e2017-03-14 17:44:21 -070022 @srv = srv
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070023 @stub = stub
Alexander Polcync44c16e2017-03-14 17:44:21 -070024 end
Alexander Polcyn4e606752017-03-19 23:32:54 -070025
Alexander Polcync44c16e2017-03-14 17:44:21 -070026 def do_echo_rpc(req, _)
27 response = @stub.echo(Echo::EchoRequest.new(request: req.request))
Alexander Polcyn4e606752017-03-19 23:32:54 -070028 fail 'bad response' unless response.response == req.request
Alexander Polcync44c16e2017-03-14 17:44:21 -070029 ClientControl::Void.new
30 end
Alexander Polcyn4e606752017-03-19 23:32:54 -070031
Alexander Polcync44c16e2017-03-14 17:44:21 -070032 def shutdown(_, _)
33 Thread.new do
Alexander Polcyn4e606752017-03-19 23:32:54 -070034 # TODO(apolcyn) There is a race between stopping the
35 # server and the "shutdown" rpc completing,
36 # See if stop method on server can end active RPC cleanly, to
37 # avoid this sleep.
Alexander Polcync44c16e2017-03-14 17:44:21 -070038 sleep 3
39 @srv.stop
40 end
41 ClientControl::Void.new
42 end
43end
44
45def main
46 client_control_port = ''
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070047 server_port = ''
Alexander Polcync44c16e2017-03-14 17:44:21 -070048 OptionParser.new do |opts|
49 opts.on('--client_control_port=P', String) do |p|
50 client_control_port = p
51 end
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070052 opts.on('--server_port=P', String) do |p|
53 server_port = p
54 end
Alexander Polcync44c16e2017-03-14 17:44:21 -070055 end.parse!
56
Alexander Polcyn4e606752017-03-19 23:32:54 -070057 Signal.trap('TERM') do
58 STDERR.puts 'SIGTERM received'
Alexander Polcync44c16e2017-03-14 17:44:21 -070059 end
60
Alexander Polcyn4e606752017-03-19 23:32:54 -070061 Signal.trap('INT') do
62 STDERR.puts 'SIGINT received'
Alexander Polcync44c16e2017-03-14 17:44:21 -070063 end
64
65 srv = GRPC::RpcServer.new
Alexander Polcyn4e606752017-03-19 23:32:54 -070066 srv.add_http2_port("0.0.0.0:#{client_control_port}",
67 :this_port_is_insecure)
68 stub = Echo::EchoServer::Stub.new("localhost:#{server_port}",
69 :this_channel_is_insecure)
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070070 srv.handle(SigHandlingClientController.new(srv, stub))
Alexander Polcync44c16e2017-03-14 17:44:21 -070071 srv.run
72end
73
74main