Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix-up test_refork, hot_restart_does_not_drop_connections [changelog skip] #2441

Merged
merged 2 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 30 additions & 22 deletions test/helpers/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,35 @@ def connect(path = nil, unix: false)
s
end

# use only if all socket writes are fast
# does not wait for a read
def fast_connect(path = nil, unix: false)
s = unix ? UNIXSocket.new(@bind_path) : TCPSocket.new(HOST, @tcp_port)
@ios_to_close << s
fast_write s, "GET /#{path} HTTP/1.1\r\n\r\n"
s
end

def fast_write(io, str)
n = 0
while true
begin
n = io.syswrite str
rescue Errno::EAGAIN, Errno::EWOULDBLOCK => e
if !IO.select(nil, [io], nil, 5)
raise e
end

retry
rescue Errno::EPIPE, SystemCallError, IOError => e
raise e
end

return if n == str.bytesize
str = str.byteslice(n..-1)
end
end

def read_body(connection, time_out = 10)
Timeout.timeout(time_out) do
loop do
Expand Down Expand Up @@ -193,7 +222,6 @@ def hot_restart_does_not_drop_connections(num_threads: 1, total_requests: 500)
begin
socket = TCPSocket.new HOST, @tcp_port
fast_write socket, "POST / HTTP/1.1\r\nContent-Length: #{message.bytesize}\r\n\r\n#{message}"
true until socket.gets == "\r\n"
body = read_body(socket, 10)
if body == "Hello World"
mutex.synchronize {
Expand Down Expand Up @@ -263,7 +291,7 @@ def hot_restart_does_not_drop_connections(num_threads: 1, total_requests: 500)

if Puma.windows?
# 5 is default thread count in Puma?
reset_max = num_threads > 1 ? restart_count * 5 : 5
reset_max = num_threads * restart_count
assert_operator reset_max, :>=, reset, "#{msg}Expected reset_max >= reset errors"
else
assert_equal 0, reset, "#{msg}Expected no reset errors"
Expand All @@ -287,24 +315,4 @@ def hot_restart_does_not_drop_connections(num_threads: 1, total_requests: 500)
$debugging_info << "#{full_name}\n#{msg}\n"
end
end

def fast_write(io, str)
n = 0
while true
begin
n = io.syswrite str
rescue Errno::EAGAIN, Errno::EWOULDBLOCK => e
if !IO.select(nil, [io], nil, 5)
raise e
end

retry
rescue Errno::EPIPE, SystemCallError, IOError => e
raise e
end

return if n == str.bytesize
str = str.byteslice(n..-1)
end
end
end
29 changes: 22 additions & 7 deletions test/test_integration_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,30 @@ def test_worker_index_is_with_in_options_limit
assert(worker_index_within_number_of_workers)
end

# use three workers to keep accepting clients
def test_refork
refork = Tempfile.new('refork')
cli_server "-w #{workers} test/rackup/sleep.ru", config: <<RUBY
fork_worker 1
on_refork {File.write('#{refork.path}', 'Reforked')}
refork = Tempfile.new 'refork'
wrkrs = 3
cli_server "-w #{wrkrs} test/rackup/hello_with_delay.ru", config: <<RUBY
fork_worker 20
on_refork { File.write '#{refork.path}', 'Reforked' }
RUBY
pids = get_worker_pids
read_body(connect('sleep1')) until refork.read == 'Reforked'
refute_includes pids, get_worker_pids(1, workers - 1)
pids = get_worker_pids 0, wrkrs

socks = []
until refork.read == 'Reforked'
socks << fast_connect
sleep 0.004
end

100.times {
socks << fast_connect
sleep 0.004
}

socks.each { |s| read_body s }

refute_includes pids, get_worker_pids(1, wrkrs - 1)
end

def test_fork_worker_spawn
Expand Down
2 changes: 1 addition & 1 deletion test/test_integration_pumactl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_prune_bundler_with_multiple_workers

cli_pumactl "stop", unix: true

_, status = Process.wait2(@pid)
_, _ = Process.wait2(@pid)
@server = nil
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_pumactl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def assert_command_cli_output(options, expected_out)
out, _ = capture_subprocess_io do
begin
cmd.run
rescue SystemExit => e
rescue SystemExit
end
end
assert_match expected_out, out
Expand Down