blob: a1b824fcbf6bb0334818ae63c748bb5e54238300 [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
Alexander Polcyn077f8902017-03-24 09:53:40 -070043 def initialize(service_impl)
44 @service_impl = service_impl
Alexander Polcync44c16e2017-03-14 17:44:21 -070045 end
Alexander Polcyn4e606752017-03-19 23:32:54 -070046
Alexander Polcync44c16e2017-03-14 17:44:21 -070047 def run
48 @srv = GRPC::RpcServer.new
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070049 port = @srv.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
Alexander Polcyn077f8902017-03-24 09:53:40 -070050 @srv.handle(@service_impl)
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070051
Alexander Polcync44c16e2017-03-14 17:44:21 -070052 @thd = Thread.new do
Alexander Polcync44c16e2017-03-14 17:44:21 -070053 @srv.run
54 end
55 @srv.wait_till_running
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070056 port
Alexander Polcync44c16e2017-03-14 17:44:21 -070057 end
Alexander Polcyn4e606752017-03-19 23:32:54 -070058
Alexander Polcync44c16e2017-03-14 17:44:21 -070059 def stop
60 @srv.stop
61 @thd.join
Alexander Polcyn4e606752017-03-19 23:32:54 -070062 fail 'server not stopped' unless @srv.stopped?
Alexander Polcync44c16e2017-03-14 17:44:21 -070063 end
64end
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070065
66def 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 Polcyn4e606752017-03-19 23:32:54 -070074 client_pid = Process.spawn(RbConfig.ruby,
75 client_path,
76 "--client_control_port=#{client_control_port}",
77 "--server_port=#{server_port}")
Alexander Polcynf8dc32e2017-03-15 00:04:33 -070078 sleep 1
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