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 attr_accessor :server_creds 47 48 def initialize(service_impl, rpc_server_args: {}) 49 @service_impl = service_impl 50 @rpc_server_args = rpc_server_args 51 @server_creds = :this_port_is_insecure 52 end 53 54 def run 55 @srv = new_rpc_server_for_testing(@rpc_server_args) 56 port = @srv.add_http2_port('0.0.0.0:0', @server_creds) 57 @srv.handle(@service_impl) 58 59 @thd = Thread.new do 60 @srv.run 61 end 62 @srv.wait_till_running 63 port 64 end 65 66 def stop 67 @srv.stop 68 @thd.join 69 fail 'server not stopped' unless @srv.stopped? 70 end 71end 72 73def start_client(client_main, server_port) 74 this_dir = File.expand_path(File.dirname(__FILE__)) 75 76 tmp_server = TCPServer.new(0) 77 client_control_port = tmp_server.local_address.ip_port 78 tmp_server.close 79 80 client_path = File.join(this_dir, client_main) 81 client_pid = Process.spawn(RbConfig.ruby, 82 client_path, 83 "--client_control_port=#{client_control_port}", 84 "--server_port=#{server_port}") 85 control_stub = ClientControl::ClientController::Stub.new( 86 "localhost:#{client_control_port}", :this_channel_is_insecure) 87 [control_stub, client_pid] 88end 89 90def cleanup(control_stub, client_pid, server_runner) 91 control_stub.shutdown(ClientControl::Void.new) 92 Process.wait(client_pid) 93 94 client_exit_code = $CHILD_STATUS 95 96 if client_exit_code != 0 97 fail "term sig test failure: client exit code: #{client_exit_code}" 98 end 99 100 server_runner.stop 101end 102