Skip to content
Snippets Groups Projects
Commit f8dc32e9 authored by Alexander Polcyn's avatar Alexander Polcyn
Browse files

make end2end test ports dynamic and slight refactor

parent 7a32e8a4
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,10 @@ $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) ...@@ -38,6 +38,10 @@ $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir)
require 'grpc' require 'grpc'
require 'echo_services_pb' require 'echo_services_pb'
require 'client_control_services_pb'
require 'socket'
require 'optparse'
require 'thread'
# GreeterServer is simple server that implements the Helloworld Greeter server. # GreeterServer is simple server that implements the Helloworld Greeter server.
class EchoServerImpl < Echo::EchoServer::Service class EchoServerImpl < Echo::EchoServer::Service
...@@ -48,17 +52,18 @@ class EchoServerImpl < Echo::EchoServer::Service ...@@ -48,17 +52,18 @@ class EchoServerImpl < Echo::EchoServer::Service
end end
class ServerRunner class ServerRunner
def initialize(port) def initialize
@port = port
end end
def run def run
@srv = GRPC::RpcServer.new @srv = GRPC::RpcServer.new
port = @srv.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
@srv.handle(EchoServerImpl)
@thd = Thread.new do @thd = Thread.new do
@srv.add_http2_port("localhost:#{@port}", :this_port_is_insecure)
@srv.handle(EchoServerImpl)
@srv.run @srv.run
end end
@srv.wait_till_running @srv.wait_till_running
port
end end
def stop def stop
@srv.stop @srv.stop
...@@ -66,3 +71,32 @@ class ServerRunner ...@@ -66,3 +71,32 @@ class ServerRunner
raise "server not stopped" unless @srv.stopped? raise "server not stopped" unless @srv.stopped?
end end
end end
def start_client(client_main, server_port)
this_dir = File.expand_path(File.dirname(__FILE__))
tmp_server = TCPServer.new(0)
client_control_port = tmp_server.local_address.ip_port
tmp_server.close
client_path = File.join(this_dir, client_main)
client_pid = Process.spawn(RbConfig.ruby, client_path,
"--client_control_port=#{client_control_port}",
"--server_port=#{server_port}")
sleep 1
control_stub = ClientControl::ClientController::Stub.new("localhost:#{client_control_port}", :this_channel_is_insecure)
return control_stub, client_pid
end
def cleanup(control_stub, server_runner)
control_stub.shutdown(ClientControl::Void.new)
Process.wait(client_pid)
client_exit_code = $?.exitstatus
if client_exit_code != 0
raise "term sig test failure: client exit code: #{client_exit_code}"
end
server_runner.stop
end
...@@ -7,15 +7,11 @@ Google::Protobuf::DescriptorPool.generated_pool.build do ...@@ -7,15 +7,11 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
add_message "client_control.DoEchoRpcRequest" do add_message "client_control.DoEchoRpcRequest" do
optional :request, :string, 1 optional :request, :string, 1
end end
add_message "client_control.CreateClientStubRequest" do
optional :server_address, :string, 1
end
add_message "client_control.Void" do add_message "client_control.Void" do
end end
end end
module ClientControl module ClientControl
DoEchoRpcRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("client_control.DoEchoRpcRequest").msgclass DoEchoRpcRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("client_control.DoEchoRpcRequest").msgclass
CreateClientStubRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("client_control.CreateClientStubRequest").msgclass
Void = Google::Protobuf::DescriptorPool.generated_pool.lookup("client_control.Void").msgclass Void = Google::Protobuf::DescriptorPool.generated_pool.lookup("client_control.Void").msgclass
end end
...@@ -45,7 +45,6 @@ module ClientControl ...@@ -45,7 +45,6 @@ module ClientControl
self.service_name = 'client_control.ClientController' self.service_name = 'client_control.ClientController'
rpc :DoEchoRpc, DoEchoRpcRequest, Void rpc :DoEchoRpc, DoEchoRpcRequest, Void
rpc :CreateClientStub, CreateClientStubRequest, Void
rpc :Shutdown, Void, Void rpc :Shutdown, Void, Void
end end
......
...@@ -33,7 +33,6 @@ package client_control; ...@@ -33,7 +33,6 @@ package client_control;
service ClientController { service ClientController {
rpc DoEchoRpc (DoEchoRpcRequest) returns (Void) {} rpc DoEchoRpc (DoEchoRpcRequest) returns (Void) {}
rpc CreateClientStub(CreateClientStubRequest) returns (Void) {}
rpc Shutdown(Void) returns (Void) {} rpc Shutdown(Void) returns (Void) {}
} }
...@@ -41,8 +40,4 @@ message DoEchoRpcRequest { ...@@ -41,8 +40,4 @@ message DoEchoRpcRequest {
string request = 1; string request = 1;
} }
message CreateClientStubRequest {
string server_address = 1;
}
message Void{} message Void{}
...@@ -29,32 +29,18 @@ ...@@ -29,32 +29,18 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
this_dir = File.expand_path(File.dirname(__FILE__)) require_relative './end2end_common'
protos_lib_dir = File.join(this_dir, 'lib')
grpc_lib_dir = File.join(File.dirname(this_dir), 'lib')
$LOAD_PATH.unshift(grpc_lib_dir) unless $LOAD_PATH.include?(grpc_lib_dir)
$LOAD_PATH.unshift(protos_lib_dir) unless $LOAD_PATH.include?(protos_lib_dir)
$LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir)
require 'grpc'
require 'echo_services_pb'
require 'client_control_services_pb'
require 'optparse'
require 'thread'
class SigHandlingClientController < ClientControl::ClientController::Service class SigHandlingClientController < ClientControl::ClientController::Service
def initialize(srv) def initialize(srv, stub)
@srv = srv @srv = srv
@stub = stub
end end
def do_echo_rpc(req, _) def do_echo_rpc(req, _)
response = @stub.echo(Echo::EchoRequest.new(request: req.request)) response = @stub.echo(Echo::EchoRequest.new(request: req.request))
raise "bad response" unless response.response == req.request raise "bad response" unless response.response == req.request
ClientControl::Void.new ClientControl::Void.new
end end
def create_client_stub(req, _)
@stub = Echo::EchoServer::Stub.new(req.server_address, :this_channel_is_insecure)
ClientControl::Void.new
end
def shutdown(_, _) def shutdown(_, _)
Thread.new do Thread.new do
#TODO(apolcyn) There is a race between stopping the server and the "shutdown" rpc completing, #TODO(apolcyn) There is a race between stopping the server and the "shutdown" rpc completing,
...@@ -68,10 +54,14 @@ end ...@@ -68,10 +54,14 @@ end
def main def main
client_control_port = '' client_control_port = ''
server_port = ''
OptionParser.new do |opts| OptionParser.new do |opts|
opts.on('--client_control_port=P', String) do |p| opts.on('--client_control_port=P', String) do |p|
client_control_port = p client_control_port = p
end end
opts.on('--server_port=P', String) do |p|
server_port = p
end
end.parse! end.parse!
Signal.trap("TERM") do Signal.trap("TERM") do
...@@ -83,8 +73,9 @@ def main ...@@ -83,8 +73,9 @@ def main
end end
srv = GRPC::RpcServer.new srv = GRPC::RpcServer.new
srv.add_http2_port("localhost:#{client_control_port}", :this_port_is_insecure) srv.add_http2_port("0.0.0.0:#{client_control_port}", :this_port_is_insecure)
srv.handle(SigHandlingClientController.new(srv)) stub = Echo::EchoServer::Stub.new("localhost:#{server_port}", :this_channel_is_insecure)
srv.handle(SigHandlingClientController.new(srv, stub))
srv.run srv.run
end end
......
...@@ -32,39 +32,20 @@ ...@@ -32,39 +32,20 @@
# smoke test for a grpc-using app that receives and # smoke test for a grpc-using app that receives and
# handles process-ending signals # handles process-ending signals
this_dir = File.expand_path(File.dirname(__FILE__)) require_relative './end2end_common'
protos_lib_dir = File.join(this_dir, 'lib')
grpc_lib_dir = File.join(File.dirname(this_dir), 'lib')
$LOAD_PATH.unshift(grpc_lib_dir) unless $LOAD_PATH.include?(grpc_lib_dir)
$LOAD_PATH.unshift(protos_lib_dir) unless $LOAD_PATH.include?(protos_lib_dir)
$LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir)
require 'grpc'
require 'echo_server'
require 'client_control_services_pb'
def main def main
this_dir = File.expand_path(File.dirname(__FILE__))
lib_dir = File.join(File.dirname(this_dir), 'lib')
server_port = '50051'
STDERR.puts "start server" STDERR.puts "start server"
server_runner = ServerRunner.new(server_port) server_runner = ServerRunner.new
server_runner.run server_port = server_runner.run
sleep 1 sleep 1
client_control_port = '50052'
STDERR.puts "start client" STDERR.puts "start client"
client_path = File.join(this_dir, "sig_handling_client.rb") control_stub, client_pid = start_client("sig_handling_client.rb", server_port)
client_pid = Process.spawn(RbConfig.ruby, client_path, "--client_control_port=#{client_control_port}")
control_stub = ClientControl::ClientController::Stub.new("localhost:#{client_control_port}", :this_channel_is_insecure)
sleep 1 sleep 1
control_stub.create_client_stub(ClientControl::CreateClientStubRequest.new(server_address: "localhost:#{server_port}"))
count = 0 count = 0
while count < 5 while count < 5
control_stub.do_echo_rpc(ClientControl::DoEchoRpcRequest.new(request: 'hello')) control_stub.do_echo_rpc(ClientControl::DoEchoRpcRequest.new(request: 'hello'))
...@@ -73,16 +54,7 @@ def main ...@@ -73,16 +54,7 @@ def main
count += 1 count += 1
end end
control_stub.shutdown(ClientControl::Void.new) cleanup(control_stub, server_runner)
Process.wait(client_pid)
client_exit_code = $?.exitstatus
if client_exit_code != 0
raise "term sig test failure: client exit code: #{client_exit_code}"
end
server_runner.stop
end end
main main
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment