Skip to content

Commit

Permalink
[CI] test_redirect_io.rb - fixup for intermittent failures (#3157)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSP-Greg committed May 17, 2023
1 parent b61bbc7 commit c467e57
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 deletions test/test_redirect_io.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# frozen_string_literal: true

require_relative "helper"
require_relative "helpers/integration"

class TestRedirectIO < TestIntegration
parallelize_me!

FILE_STR = 'puma startup'

def setup
skip_unless_signal_exist? :HUP
super
Expand All @@ -13,6 +17,12 @@ def setup
@err_file = Tempfile.new('puma-err')
@out_file_path = @out_file.path
@err_file_path = @err_file.path

@cli_args = ['--redirect-stdout', @out_file_path,
'--redirect-stderr', @err_file_path,
'test/rackup/hello.ru'
]

end

def teardown
Expand All @@ -30,56 +40,17 @@ def teardown
def test_sighup_redirects_io_single
skip_if :jruby # Server isn't coming up in CI, TODO Fix

cli_args = [
'--redirect-stdout', @out_file_path,
'--redirect-stderr', @err_file_path,
'test/rackup/hello.ru'
]
cli_server cli_args.join ' '

wait_until_file_has_content @out_file_path
assert_match 'puma startup', File.read(@out_file_path)

wait_until_file_has_content @err_file_path
assert_match 'puma startup', File.read(@err_file_path)

log_rotate_output_files

Process.kill :HUP, @server.pid

wait_until_file_has_content @out_file_path
assert_match 'puma startup', File.read(@out_file_path)
cli_server @cli_args.join ' '

wait_until_file_has_content @err_file_path
assert_match 'puma startup', File.read(@err_file_path)
rotate_check_logs
end

def test_sighup_redirects_io_cluster
skip_unless :fork

cli_args = [
'-w', '1',
'--redirect-stdout', @out_file_path,
'--redirect-stderr', @err_file_path,
'test/rackup/hello.ru'
]
cli_server cli_args.join ' '

wait_until_file_has_content @out_file_path
assert_match 'puma startup', File.read(@out_file_path)

wait_until_file_has_content @err_file_path
assert_match 'puma startup', File.read(@err_file_path)

log_rotate_output_files

Process.kill :HUP, @server.pid
cli_server (['-w', '1'] + @cli_args).join ' '

wait_until_file_has_content @out_file_path
assert_match 'puma startup', File.read(@out_file_path)

wait_until_file_has_content @err_file_path
assert_match 'puma startup', File.read(@err_file_path)
rotate_check_logs
end

private
Expand All @@ -88,21 +59,43 @@ def log_rotate_output_files
# rename both files to .old
@old_out_file_path = "#{@out_file_path}.old"
@old_err_file_path = "#{@err_file_path}.old"

File.rename @out_file_path, @old_out_file_path
File.rename @err_file_path, @old_err_file_path

File.new(@out_file_path, File::CREAT).close
File.new(@err_file_path, File::CREAT).close
end

def wait_until_file_has_content(path)
def rotate_check_logs
assert_file_contents @out_file_path
assert_file_contents @err_file_path

log_rotate_output_files

Process.kill :HUP, @pid

assert_file_contents @out_file_path
assert_file_contents @err_file_path
end

def assert_file_contents(path, include = FILE_STR)
retries = 0
retries_max = 50 # 5 seconds
File.open(path) do |file|
begin
file.read_nonblock 1
file.seek 0
assert_includes file.read, include,
"File #{File.basename(path)} does not include #{include}"
rescue EOFError
sleep 0.1
retry
retries += 1
if retries < retries_max
retry
else
flunk 'File read took too long'
end
end
end
end
Expand Down

0 comments on commit c467e57

Please sign in to comment.