diff --git a/script/test-redirections b/script/test-redirections index 95f1ef1..5f75131 100755 --- a/script/test-redirections +++ b/script/test-redirections @@ -3,15 +3,16 @@ require 'socket' # Start a webserver on a given port, serving a 301 redirection to a specific location -def start_server(port, location) +def start_server(port, location, log_mode) server = TCPServer.new(port) puts "Server started on port #{port}" loop do + # Block until a client connects client = server.accept # Append a hit in the file - File.open("/tmp/bad-redirection#{ENV["RUBY_VERSION"]}.log", 'a') do |file| + File.open("/tmp/bad-redirection#{ENV["RUBY_VERSION"]}.log", log_mode) do |file| file.sync = true # no bufferring file.print("HIT #{port} ") end @@ -38,7 +39,7 @@ end # Go! threads = [] -threads << Thread.new { start_server(9988, 'http://127.0.0.1:9987') } # HTTP 301 -threads << Thread.new { start_server(9987, 'ftp://127.0.0.1:9986') } # HTTP 301 -threads << Thread.new { start_server(9986, 'STOP') } # TCP close +threads << Thread.new { start_server(9988, 'http://127.0.0.1:9987', "w") } # HTTP 301 +threads << Thread.new { start_server(9987, 'ftp://127.0.0.1:9986', "a") } # HTTP 301 +threads << Thread.new { start_server(9986, 'STOP', "a") } # TCP close threads.each(&:join) \ No newline at end of file diff --git a/spec/github_pages_health_check/domain_spec.rb b/spec/github_pages_health_check/domain_spec.rb index 33a38d0..75886c1 100644 --- a/spec/github_pages_health_check/domain_spec.rb +++ b/spec/github_pages_health_check/domain_spec.rb @@ -673,13 +673,6 @@ context "Protocol redirections" do let(:log_file) { "/tmp/bad-redirection#{ENV["RUBY_VERSION"]}.log" } - before do - File.open(log_file, "w") do |file| - # Just truncate the file (without buffering to avoid flakiness) - file.sync = true - end - sleep(0.5) # slow down just a little to limit flakiness too - end it "it follows ftp if requested" do # Make a real request to a local server started with /script/test-redirections @@ -689,6 +682,7 @@ ) # Confirm port 9986 was hit (it is the FTP one) + sleep(0.1) until File.exist?(log_file) expect(File.read(log_file).strip).to eq("HIT 9988 HIT 9987 HIT 9986") end @@ -700,6 +694,7 @@ ) # Confirm port 9986 was NOT hit (it is the FTP one) + sleep(0.1) until File.exist?(log_file) expect(File.read(log_file).strip).to eq("HIT 9988 HIT 9987") end end