Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | # Copyright 2015 gRPC authors. |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 4 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 10 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 16 | |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 17 | require_relative './end2end_common' |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 18 | |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 19 | # Test client. Sends RPC's as normal but process also has signal handlers |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 20 | class SigHandlingClientController < ClientControl::ClientController::Service |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 21 | def initialize(srv, stub) |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 22 | @srv = srv |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 23 | @stub = stub |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 24 | end |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 25 | |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 26 | def do_echo_rpc(req, _) |
| 27 | response = @stub.echo(Echo::EchoRequest.new(request: req.request)) |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 28 | fail 'bad response' unless response.response == req.request |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 29 | ClientControl::Void.new |
| 30 | end |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 31 | |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 32 | def shutdown(_, _) |
| 33 | Thread.new do |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 34 | # 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 Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 38 | sleep 3 |
| 39 | @srv.stop |
| 40 | end |
| 41 | ClientControl::Void.new |
| 42 | end |
| 43 | end |
| 44 | |
| 45 | def main |
| 46 | client_control_port = '' |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 47 | server_port = '' |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 48 | OptionParser.new do |opts| |
| 49 | opts.on('--client_control_port=P', String) do |p| |
| 50 | client_control_port = p |
| 51 | end |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 52 | opts.on('--server_port=P', String) do |p| |
| 53 | server_port = p |
| 54 | end |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 55 | end.parse! |
| 56 | |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 57 | Signal.trap('TERM') do |
| 58 | STDERR.puts 'SIGTERM received' |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 59 | end |
| 60 | |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 61 | Signal.trap('INT') do |
| 62 | STDERR.puts 'SIGINT received' |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 63 | end |
| 64 | |
| 65 | srv = GRPC::RpcServer.new |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 66 | 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 Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 70 | srv.handle(SigHandlingClientController.new(srv, stub)) |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 71 | srv.run |
| 72 | end |
| 73 | |
| 74 | main |