1#!/usr/bin/env ruby 2 3# Copyright 2015 gRPC authors. 4# 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 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 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. 16 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' 26require 'client_control_services_pb' 27require 'socket' 28require 'optparse' 29require 'thread' 30require 'timeout' 31require 'English' # see https://github.com/bbatsov/rubocop/issues/1747 32require_relative '../spec/support/helpers' 33 34include GRPC::Spec::Helpers 35 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 44# ServerRunner starts an "echo server" that test clients can make calls to 45class ServerRunner 46 def initialize(service_impl, rpc_server_args: {}) 47 @service_impl = service_impl 48 @rpc_server_args = rpc_server_args 49 end 50 51 def run 52 @srv = new_rpc_server_for_testing(@rpc_server_args) 53 port = @srv.add_http2_port('0.0.0.0:0', :this_port_is_insecure) 54 @srv.handle(@service_impl) 55 56 @thd = Thread.new do 57 @srv.run 58 end 59 @srv.wait_till_running 60 port 61 end 62 63 def stop 64 @srv.stop 65 @thd.join 66 fail 'server not stopped' unless @srv.stopped? 67 end 68end 69 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) 78 client_pid = Process.spawn(RbConfig.ruby, 79 client_path, 80 "--client_control_port=#{client_control_port}", 81 "--server_port=#{server_port}") 82 control_stub = ClientControl::ClientController::Stub.new( 83 "localhost:#{client_control_port}", :this_channel_is_insecure) 84 [control_stub, client_pid] 85end 86 87def cleanup(control_stub, client_pid, server_runner) 88 control_stub.shutdown(ClientControl::Void.new) 89 Process.wait(client_pid) 90 91 client_exit_code = $CHILD_STATUS 92 93 if client_exit_code != 0 94 fail "term sig test failure: client exit code: #{client_exit_code}" 95 end 96 97 server_runner.stop 98end 99