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 | |
| 17 | this_dir = File.expand_path(File.dirname(__FILE__)) |
| 18 | protos_lib_dir = File.join(this_dir, 'lib') |
| 19 | grpc_lib_dir = File.join(File.dirname(this_dir), 'lib') |
| 20 | $LOAD_PATH.unshift(grpc_lib_dir) unless $LOAD_PATH.include?(grpc_lib_dir) |
| 21 | $LOAD_PATH.unshift(protos_lib_dir) unless $LOAD_PATH.include?(protos_lib_dir) |
| 22 | $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) |
| 23 | |
| 24 | require 'grpc' |
| 25 | require 'echo_services_pb' |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 26 | require 'client_control_services_pb' |
| 27 | require 'socket' |
| 28 | require 'optparse' |
| 29 | require 'thread' |
Alexander Polcyn | 16d97ed | 2017-03-15 10:01:13 -0700 | [diff] [blame] | 30 | require 'timeout' |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 31 | require 'English' # see https://github.com/bbatsov/rubocop/issues/1747 |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 32 | |
| 33 | # GreeterServer is simple server that implements the Helloworld Greeter server. |
| 34 | class EchoServerImpl < Echo::EchoServer::Service |
| 35 | # say_hello implements the SayHello rpc method. |
| 36 | def echo(echo_req, _) |
| 37 | Echo::EchoReply.new(response: echo_req.request) |
| 38 | end |
| 39 | end |
| 40 | |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 41 | # ServerRunner starts an "echo server" that test clients can make calls to |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 42 | class ServerRunner |
Alexander Polcyn | 077f890 | 2017-03-24 09:53:40 -0700 | [diff] [blame] | 43 | def initialize(service_impl) |
| 44 | @service_impl = service_impl |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 45 | end |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 46 | |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 47 | def run |
| 48 | @srv = GRPC::RpcServer.new |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 49 | port = @srv.add_http2_port('0.0.0.0:0', :this_port_is_insecure) |
Alexander Polcyn | 077f890 | 2017-03-24 09:53:40 -0700 | [diff] [blame] | 50 | @srv.handle(@service_impl) |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 51 | |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 52 | @thd = Thread.new do |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 53 | @srv.run |
| 54 | end |
| 55 | @srv.wait_till_running |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 56 | port |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 57 | end |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 58 | |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 59 | def stop |
| 60 | @srv.stop |
| 61 | @thd.join |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 62 | fail 'server not stopped' unless @srv.stopped? |
Alexander Polcyn | c44c16e | 2017-03-14 17:44:21 -0700 | [diff] [blame] | 63 | end |
| 64 | end |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 65 | |
| 66 | def start_client(client_main, server_port) |
| 67 | this_dir = File.expand_path(File.dirname(__FILE__)) |
| 68 | |
| 69 | tmp_server = TCPServer.new(0) |
| 70 | client_control_port = tmp_server.local_address.ip_port |
| 71 | tmp_server.close |
| 72 | |
| 73 | client_path = File.join(this_dir, client_main) |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 74 | client_pid = Process.spawn(RbConfig.ruby, |
| 75 | client_path, |
| 76 | "--client_control_port=#{client_control_port}", |
| 77 | "--server_port=#{server_port}") |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 78 | sleep 1 |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 79 | control_stub = ClientControl::ClientController::Stub.new( |
| 80 | "localhost:#{client_control_port}", :this_channel_is_insecure) |
| 81 | [control_stub, client_pid] |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 82 | end |
| 83 | |
Alexander Polcyn | 16d97ed | 2017-03-15 10:01:13 -0700 | [diff] [blame] | 84 | def cleanup(control_stub, client_pid, server_runner) |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 85 | control_stub.shutdown(ClientControl::Void.new) |
| 86 | Process.wait(client_pid) |
| 87 | |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 88 | client_exit_code = $CHILD_STATUS |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 89 | |
| 90 | if client_exit_code != 0 |
Alexander Polcyn | 4e60675 | 2017-03-19 23:32:54 -0700 | [diff] [blame] | 91 | fail "term sig test failure: client exit code: #{client_exit_code}" |
Alexander Polcyn | f8dc32e | 2017-03-15 00:04:33 -0700 | [diff] [blame] | 92 | end |
| 93 | |
| 94 | server_runner.stop |
| 95 | end |