blob: ffbaa1986d0593c936ebc2339f25b3c51d0be453 [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 Polcynb14f1ea2017-12-13 14:43:24 -080032require_relative '../spec/support/helpers'
33
34include GRPC::Spec::Helpers
Alexander Polcync44c16e2017-03-14 17:44:21 -070035
36# GreeterServer is simple server that implements the Helloworld Greeter server.
37class EchoServerImpl < Echo::EchoServer::Service
38 # say_hello implements the SayHello rpc method.
39 def echo(echo_req, _)
40 Echo::EchoReply.new(response: echo_req.request)
41 end
42end
43
Alexander Polcyn4e606752017-03-19 23:32:54 -070044# ServerRunner starts an "echo server" that test clients can make calls to
Alexander Polcync44c16e2017-03-14 17:44:21 -070045class ServerRunner
Alex Polcyn81e95812017-09-23 18:48:02 -070046 def initialize(service_impl, rpc_server_args: {})
Alexander Polcyn077f8902017-03-24 09:53:40 -070047 @service_impl = service_impl
Alex Polcyn81e95812017-09-23 18:48:02 -070048 @rpc_server_args = rpc_server_args
Alexander Polcync44c16e2017-03-14 17:44:21 -070049 end
Alexander Polcyn4e606752017-03-19 23:32:54 -070050
Alexander Polcync44c16e2017-03-14 17:44:21 -070051 def run
Alexander Polcynb14f1ea2017-12-13 14:43:24 -080052 @srv = new_rpc_server_for_testing(@rpc_server_args)
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070053 port = @srv.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
Alexander Polcyn077f8902017-03-24 09:53:40 -070054 @srv.handle(@service_impl)
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070055
Alexander Polcync44c16e2017-03-14 17:44:21 -070056 @thd = Thread.new do
Alexander Polcync44c16e2017-03-14 17:44:21 -070057 @srv.run
58 end
59 @srv.wait_till_running
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070060 port
Alexander Polcync44c16e2017-03-14 17:44:21 -070061 end
Alexander Polcyn4e606752017-03-19 23:32:54 -070062
Alexander Polcync44c16e2017-03-14 17:44:21 -070063 def stop
64 @srv.stop
65 @thd.join
Alexander Polcyn4e606752017-03-19 23:32:54 -070066 fail 'server not stopped' unless @srv.stopped?
Alexander Polcync44c16e2017-03-14 17:44:21 -070067 end
68end
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070069
70def start_client(client_main, server_port)
71 this_dir = File.expand_path(File.dirname(__FILE__))
72
73 tmp_server = TCPServer.new(0)
74 client_control_port = tmp_server.local_address.ip_port
75 tmp_server.close
76
77 client_path = File.join(this_dir, client_main)
Alexander Polcyn4e606752017-03-19 23:32:54 -070078 client_pid = Process.spawn(RbConfig.ruby,
79 client_path,
80 "--client_control_port=#{client_control_port}",
81 "--server_port=#{server_port}")
Alexander Polcyn4e606752017-03-19 23:32:54 -070082 control_stub = ClientControl::ClientController::Stub.new(
83 "localhost:#{client_control_port}", :this_channel_is_insecure)
84 [control_stub, client_pid]
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070085end
86
Alexander Polcyn16d97ed2017-03-15 10:01:13 -070087def cleanup(control_stub, client_pid, server_runner)
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070088 control_stub.shutdown(ClientControl::Void.new)
89 Process.wait(client_pid)
90
Alexander Polcyn4e606752017-03-19 23:32:54 -070091 client_exit_code = $CHILD_STATUS
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070092
93 if client_exit_code != 0
Alexander Polcyn4e606752017-03-19 23:32:54 -070094 fail "term sig test failure: client exit code: #{client_exit_code}"
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070095 end
96
97 server_runner.stop
98end