diff options
Diffstat (limited to 'test/test_helper.rb')
-rw-r--r-- | test/test_helper.rb | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/test/test_helper.rb b/test/test_helper.rb index 94a5b1b..0bf3c90 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,5 @@ # -*- encoding: binary -*- +# frozen_string_literal: false # Copyright (c) 2005 Zed A. Shaw # You can redistribute it and/or modify it under the same terms as Ruby 1.8 or @@ -28,22 +29,43 @@ require 'tempfile' require 'fileutils' require 'logger' require 'unicorn' +require 'io/nonblock' if ENV['DEBUG'] require 'ruby-debug' Debugger.start end +unless RUBY_VERSION < '3.1' + warn "Unicorn was only tested against MRI up to 3.0.\n" \ + "It might not properly work with #{RUBY_VERSION}" +end + def redirect_test_io orig_err = STDERR.dup orig_out = STDOUT.dup - STDERR.reopen("test_stderr.#{$$}.log", "a") - STDOUT.reopen("test_stdout.#{$$}.log", "a") + rdr_pid = $$ + new_out = File.open("test_stdout.#$$.log", "a") + new_err = File.open("test_stderr.#$$.log", "a") + new_out.sync = new_err.sync = true + + if tail = ENV['TAIL'] # "tail -F" if GNU, "tail -f" otherwise + require 'shellwords' + cmd = tail.shellsplit + cmd << new_out.path + cmd << new_err.path + pid = Process.spawn(*cmd, { 1 => 2, :pgroup => true }) + sleep 0.1 # wait for tail(1) to startup + end + STDERR.reopen(new_err) + STDOUT.reopen(new_out) STDERR.sync = STDOUT.sync = true at_exit do - File.unlink("test_stderr.#{$$}.log") rescue nil - File.unlink("test_stdout.#{$$}.log") rescue nil + if rdr_pid == $$ + File.unlink(new_out.path) rescue nil + File.unlink(new_err.path) rescue nil + end end begin @@ -51,6 +73,7 @@ def redirect_test_io ensure STDERR.reopen(orig_err) STDOUT.reopen(orig_out) + Process.kill(:TERM, pid) if pid end end @@ -270,3 +293,15 @@ def reset_sig_handlers trap(sig, "DEFAULT") end end + +def tcp_socket(*args) + sock = TCPSocket.new(*args) + sock.nonblock = false + sock +end + +def unix_socket(*args) + sock = UNIXSocket.new(*args) + sock.nonblock = false + sock +end |