blob: 790fc23e92360d22a66985aaa8ece40b282aa625 [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
17this_dir = File.expand_path(File.dirname(__FILE__))
18protos_lib_dir = File.join(this_dir, 'lib')
19grpc_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
24require 'grpc'
25require 'echo_services_pb'
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070026require 'client_control_services_pb'
27require 'socket'
28require 'optparse'
29require 'thread'
Alexander Polcyn16d97ed2017-03-15 10:01:13 -070030require 'timeout'
Alexander Polcyn4e606752017-03-19 23:32:54 -070031require 'English' # see https://github.com/bbatsov/rubocop/issues/1747
Alexander Polcync44c16e2017-03-14 17:44:21 -070032
33# GreeterServer is simple server that implements the Helloworld Greeter server.
34class 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
39end
40
Alexander Polcyn4e606752017-03-19 23:32:54 -070041# ServerRunner starts an "echo server" that test clients can make calls to
Alexander Polcync44c16e2017-03-14 17:44:21 -070042class ServerRunner
Alex Polcyn81e95812017-09-23 18:48:02 -070043 def initialize(service_impl, rpc_server_args: {})
Alexander Polcyn077f8902017-03-24 09:53:40 -070044 @service_impl = service_impl
Alex Polcyn81e95812017-09-23 18:48:02 -070045 @rpc_server_args = rpc_server_args
Alexander Polcync44c16e2017-03-14 17:44:21 -070046 end
Alexander Polcyn4e606752017-03-19 23:32:54 -070047
Alexander Polcync44c16e2017-03-14 17:44:21 -070048 def run
Alex Polcyn81e95812017-09-23 18:48:02 -070049 @srv = GRPC::RpcServer.new(@rpc_server_args)
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070050 port = @srv.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
Alexander Polcyn077f8902017-03-24 09:53:40 -070051 @srv.handle(@service_impl)
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070052
Alexander Polcync44c16e2017-03-14 17:44:21 -070053 @thd = Thread.new do
Alexander Polcync44c16e2017-03-14 17:44:21 -070054 @srv.run
55 end
56 @srv.wait_till_running
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070057 port
Alexander Polcync44c16e2017-03-14 17:44:21 -070058 end
Alexander Polcyn4e606752017-03-19 23:32:54 -070059
Alexander Polcync44c16e2017-03-14 17:44:21 -070060 def stop
61 @srv.stop
62 @thd.join
Alexander Polcyn4e606752017-03-19 23:32:54 -070063 fail 'server not stopped' unless @srv.stopped?
Alexander Polcync44c16e2017-03-14 17:44:21 -070064 end
65end
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070066
67def start_client(client_main, server_port)
68 this_dir = File.expand_path(File.dirname(__FILE__))
69
70 tmp_server = TCPServer.new(0)
71 client_control_port = tmp_server.local_address.ip_port
72 tmp_server.close
73
74 client_path = File.join(this_dir, client_main)
Alexander Polcyn4e606752017-03-19 23:32:54 -070075 client_pid = Process.spawn(RbConfig.ruby,
76 client_path,
77 "--client_control_port=#{client_control_port}",
78 "--server_port=#{server_port}")
Alexander Polcyn4e606752017-03-19 23:32:54 -070079 control_stub = ClientControl::ClientController::Stub.new(
80 "localhost:#{client_control_port}", :this_channel_is_insecure)
81 [control_stub, client_pid]
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070082end
83
Alexander Polcyn16d97ed2017-03-15 10:01:13 -070084def cleanup(control_stub, client_pid, server_runner)
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070085 control_stub.shutdown(ClientControl::Void.new)
86 Process.wait(client_pid)
87
Alexander Polcyn4e606752017-03-19 23:32:54 -070088 client_exit_code = $CHILD_STATUS
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070089
90 if client_exit_code != 0
Alexander Polcyn4e606752017-03-19 23:32:54 -070091 fail "term sig test failure: client exit code: #{client_exit_code}"
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070092 end
93
94 server_runner.stop
95end