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(_, _) |
Alex Polcyn | 81e9581 | 2017-09-23 18:48:02 -0700 | [diff] [blame] | 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 |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 37 | @srv.stop |
| 38 | end |
| 39 | ClientControl::Void.new |
| 40 | end |
Alex Polcyn | 81e9581 | 2017-09-23 18:48:02 -0700 | [diff] [blame] | 41 | |
| 42 | def join_shutdown_thread |
| 43 | @shutdown_thread.join |
| 44 | end |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 45 | end |
| 46 | |
| 47 | def main |
| 48 | client_control_port = '' |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 49 | server_port = '' |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 50 | OptionParser.new do |opts| |
| 51 | opts.on('--client_control_port=P', String) do |p| |
| 52 | client_control_port = p |
| 53 | end |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 54 | opts.on('--server_port=P', String) do |p| |
| 55 | server_port = p |
| 56 | end |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 57 | end.parse! |
| 58 | |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 59 | Signal.trap('TERM') do |
| 60 | STDERR.puts 'SIGTERM received' |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 61 | end |
| 62 | |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 63 | Signal.trap('INT') do |
| 64 | STDERR.puts 'SIGINT received' |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 65 | end |
| 66 | |
Alex Polcyn | 81e9581 | 2017-09-23 18:48:02 -0700 | [diff] [blame] | 67 | # The "shutdown" RPC should end very quickly. |
| 68 | # Allow a few seconds to be safe. |
Alexander Polcyn | b14f1ea | 2017-12-13 14:43:24 -0800 | [diff] [blame] | 69 | srv = new_rpc_server_for_testing(poll_period: 3) |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 70 | srv.add_http2_port("0.0.0.0:#{client_control_port}", |
| 71 | :this_port_is_insecure) |
| 72 | stub = Echo::EchoServer::Stub.new("localhost:#{server_port}", |
| 73 | :this_channel_is_insecure) |
Alex Polcyn | 81e9581 | 2017-09-23 18:48:02 -0700 | [diff] [blame] | 74 | control_service = SigHandlingClientController.new(srv, stub) |
| 75 | srv.handle(control_service) |
| 76 | server_thread = Thread.new do |
| 77 | srv.run |
| 78 | end |
| 79 | srv.wait_till_running |
| 80 | # send a first RPC to notify the parent process that we've started |
| 81 | stub.echo(Echo::EchoRequest.new(request: 'client/child started')) |
| 82 | server_thread.join |
| 83 | control_service.join_shutdown_thread |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 84 | end |
| 85 | |
| 86 | main |