Skip to content

Commit

Permalink
add end2end tests to formatter and adjust to formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
apolcyn committed Mar 20, 2017
1 parent b862b6a commit 4e60675
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 38 deletions.
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ load 'tools/distrib/docker_for_windows.rb'
# Add rubocop style checking tasks
RuboCop::RakeTask.new(:rubocop) do |task|
task.options = ['-c', 'src/ruby/.rubocop.yml']
task.patterns = ['src/ruby/{lib,spec}/**/*.rb']
# add end2end tests to formatter but don't add generated proto _pb.rb's
task.patterns = ['src/ruby/{lib,spec}/**/*.rb', 'src/ruby/end2end/*.rb']
end

spec = Gem::Specification.load('grpc.gemspec')
Expand Down
1 change: 1 addition & 0 deletions src/ruby/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ AllCops:
- 'bin/math_services_pb.rb'
- 'pb/grpc/health/v1/*'
- 'pb/test/**/*'
- 'end2end/lib/*'

Metrics/CyclomaticComplexity:
Max: 9
Expand Down
6 changes: 5 additions & 1 deletion src/ruby/end2end/channel_closing_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@

require_relative './end2end_common'

# Calls '#close' on a Channel when "shutdown" called. This tries to
# trigger a hang or crash bug by closing a channel actively being watched
class ChannelClosingClientController < ClientControl::ClientController::Service
def initialize(ch)
@ch = ch
end

def shutdown(_, _)
@ch.close
ClientControl::Void.new
Expand All @@ -53,7 +56,8 @@ def main
end
end.parse!

ch = GRPC::Core::Channel.new("localhost:#{server_port}", {}, :this_channel_is_insecure)
ch = GRPC::Core::Channel.new("localhost:#{server_port}", {},
:this_channel_is_insecure)

srv = GRPC::RpcServer.new
thd = Thread.new do
Expand Down
16 changes: 9 additions & 7 deletions src/ruby/end2end/channel_closing_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,24 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# make sure that the client doesn't hang when channel is closed explictly while it's used
# make sure that the client doesn't hang when channel is closed
# explictly while it's used

require_relative './end2end_common'

def main
STDERR.puts "start server"
STDERR.puts 'start server'
server_runner = ServerRunner.new
server_port = server_runner.run

sleep 1

STDERR.puts "start client"
control_stub, client_pid = start_client("channel_closing_client.rb", server_port)
STDERR.puts 'start client'
control_stub, client_pid = start_client('channel_closing_client.rb',
server_port)

sleep 3


begin
Timeout.timeout(10) do
control_stub.shutdown(ClientControl::Void.new)
Expand All @@ -55,8 +56,9 @@ def main
STDERR.puts "timeout wait for client pid #{client_pid}"
Process.kill('SIGKILL', client_pid)
Process.wait(client_pid)
STDERR.puts "killed client child"
raise 'Timed out waiting for client process. It likely hangs when a channel is closed while connectivity is watched'
STDERR.puts 'killed client child'
raise 'Timed out waiting for client process. It likely hangs when a ' \
'channel is closed while connectivity is watched'
end

server_runner.stop
Expand Down
7 changes: 4 additions & 3 deletions src/ruby/end2end/channel_state_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@
def main
server_port = ''
OptionParser.new do |opts|
opts.on('--client_control_port=P', String) do |p|
STDERR.puts "client_control_port ignored"
opts.on('--client_control_port=P', String) do
STDERR.puts 'client_control_port ignored'
end
opts.on('--server_port=P', String) do |p|
server_port = p
end
end.parse!

ch = GRPC::Core::Channel.new("localhost:#{server_port}", {}, :this_channel_is_insecure)
ch = GRPC::Core::Channel.new("localhost:#{server_port}", {},
:this_channel_is_insecure)

loop do
state = ch.connectivity_state
Expand Down
11 changes: 6 additions & 5 deletions src/ruby/end2end/channel_state_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
require_relative './end2end_common'

def main
STDERR.puts "start server"
STDERR.puts 'start server'
server_runner = ServerRunner.new
server_port = server_runner.run

sleep 1

STDERR.puts "start client"
_, client_pid = start_client("channel_state_client.rb", server_port)
STDERR.puts 'start client'
_, client_pid = start_client('channel_state_client.rb', server_port)

sleep 3

Expand All @@ -53,8 +53,9 @@ def main
STDERR.puts "timeout wait for client pid #{client_pid}"
Process.kill('SIGKILL', client_pid)
Process.wait(client_pid)
STDERR.puts "killed client child"
raise 'Timed out waiting for client process. It likely hangs when ended abruptly'
STDERR.puts 'killed client child'
raise 'Timed out waiting for client process. ' \
'It likely hangs when ended abruptly'
end

server_runner.stop
Expand Down
22 changes: 14 additions & 8 deletions src/ruby/end2end/end2end_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
require 'optparse'
require 'thread'
require 'timeout'
require 'English' # see https://github.com/bbatsov/rubocop/issues/1747

# GreeterServer is simple server that implements the Helloworld Greeter server.
class EchoServerImpl < Echo::EchoServer::Service
Expand All @@ -52,9 +53,11 @@ def echo(echo_req, _)
end
end

# ServerRunner starts an "echo server" that test clients can make calls to
class ServerRunner
def initialize
end

def run
@srv = GRPC::RpcServer.new
port = @srv.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
Expand All @@ -66,10 +69,11 @@ def run
@srv.wait_till_running
port
end

def stop
@srv.stop
@thd.join
raise "server not stopped" unless @srv.stopped?
fail 'server not stopped' unless @srv.stopped?
end
end

Expand All @@ -81,22 +85,24 @@ def start_client(client_main, server_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}")
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
control_stub = ClientControl::ClientController::Stub.new(
"localhost:#{client_control_port}", :this_channel_is_insecure)
[control_stub, client_pid]
end

def cleanup(control_stub, client_pid, server_runner)
control_stub.shutdown(ClientControl::Void.new)
Process.wait(client_pid)

client_exit_code = $?.exitstatus
client_exit_code = $CHILD_STATUS

if client_exit_code != 0
raise "term sig test failure: client exit code: #{client_exit_code}"
fail "term sig test failure: client exit code: #{client_exit_code}"
end

server_runner.stop
Expand Down
25 changes: 16 additions & 9 deletions src/ruby/end2end/sig_handling_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@

require_relative './end2end_common'

# Test client. Sends RPC's as normal but process also has signal handlers
class SigHandlingClientController < ClientControl::ClientController::Service
def initialize(srv, stub)
@srv = srv
@stub = stub
end

def do_echo_rpc(req, _)
response = @stub.echo(Echo::EchoRequest.new(request: req.request))
raise "bad response" unless response.response == req.request
fail 'bad response' unless response.response == req.request
ClientControl::Void.new
end

def shutdown(_, _)
Thread.new do
#TODO(apolcyn) There is a race between stopping the server and the "shutdown" rpc completing,
# See if stop method on server can end active RPC cleanly, to avoid this sleep.
# TODO(apolcyn) There is a race between stopping the
# server and the "shutdown" rpc completing,
# See if stop method on server can end active RPC cleanly, to
# avoid this sleep.
sleep 3
@srv.stop
end
Expand All @@ -64,17 +69,19 @@ def main
end
end.parse!

Signal.trap("TERM") do
STDERR.puts "SIGTERM received"
Signal.trap('TERM') do
STDERR.puts 'SIGTERM received'
end

Signal.trap("INT") do
STDERR.puts "SIGINT received"
Signal.trap('INT') do
STDERR.puts 'SIGINT received'
end

srv = GRPC::RpcServer.new
srv.add_http2_port("0.0.0.0:#{client_control_port}", :this_port_is_insecure)
stub = Echo::EchoServer::Stub.new("localhost:#{server_port}", :this_channel_is_insecure)
srv.add_http2_port("0.0.0.0:#{client_control_port}",
:this_port_is_insecure)
stub = Echo::EchoServer::Stub.new("localhost:#{server_port}",
:this_channel_is_insecure)
srv.handle(SigHandlingClientController.new(srv, stub))
srv.run
end
Expand Down
9 changes: 5 additions & 4 deletions src/ruby/end2end/sig_handling_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@
require_relative './end2end_common'

def main
STDERR.puts "start server"
STDERR.puts 'start server'
server_runner = ServerRunner.new
server_port = server_runner.run

sleep 1

STDERR.puts "start client"
control_stub, client_pid = start_client("sig_handling_client.rb", server_port)
STDERR.puts 'start client'
control_stub, client_pid = start_client('sig_handling_client.rb', server_port)

sleep 1

count = 0
while count < 5
control_stub.do_echo_rpc(ClientControl::DoEchoRpcRequest.new(request: 'hello'))
control_stub.do_echo_rpc(
ClientControl::DoEchoRpcRequest.new(request: 'hello'))
Process.kill('SIGTERM', client_pid)
Process.kill('SIGINT', client_pid)
count += 1
Expand Down

0 comments on commit 4e60675

Please sign in to comment.