1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

envutil.rb: signal list to invoke_ruby

* test/lib/envutil.rb (invoke_ruby): allow `signal` optional
  keyword argument to be a list of signals to be sent to the
  target process.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49805 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-03-02 06:44:22 +00:00
parent 575d4a3069
commit 876d664069

View file

@ -30,10 +30,13 @@ module EnvUtil
LANG_ENVS = %w"LANG LC_ALL LC_CTYPE" LANG_ENVS = %w"LANG LC_ALL LC_CTYPE"
DEFAULT_SIGNALS = Signal.list
DEFAULT_SIGNALS.delete("TERM") if /mswin|mingw/ =~ RUBY_PLATFORM
def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr = false, def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr = false,
encoding: nil, timeout: 10, reprieve: 1, encoding: nil, timeout: 10, reprieve: 1,
stdout_filter: nil, stderr_filter: nil, stdout_filter: nil, stderr_filter: nil,
signal: (/mswin|mingw/ =~ RUBY_PLATFORM ? :KILL : :TERM), signal: :TERM,
rubybin: EnvUtil.rubybin, rubybin: EnvUtil.rubybin,
**opt) **opt)
in_c, in_p = IO.pipe in_c, in_p = IO.pipe
@ -68,25 +71,34 @@ module EnvUtil
stdout = th_stdout.value if capture_stdout stdout = th_stdout.value if capture_stdout
stderr = th_stderr.value if capture_stderr && capture_stderr != :merge_to_stdout stderr = th_stderr.value if capture_stderr && capture_stderr != :merge_to_stdout
else else
signals = Array(signal).select do |sig|
DEFAULT_SIGNALS[sig.to_s] or
DEFAULT_SIGNALS[Signal.signame(sig)]
end
signals |= [:KILL]
case pgroup = opt[:pgroup] case pgroup = opt[:pgroup]
when 0, true when 0, true
pgroup = -pid pgroup = -pid
when nil, false when nil, false
pgroup = pid pgroup = pid
end end
begin while signal = signals.shift
Process.kill signal, pgroup begin
Timeout.timeout((reprieve unless signal == :KILL)) do Process.kill signal, pgroup
Process.wait(pid) rescue Errno::Invalid
next
rescue Errno::ESRCH
break
end end
rescue Errno::ESRCH if signals.empty? or !reprieve
break Process.wait(pid)
rescue Timeout::Error else
raise if signal == :KILL begin
signal = :KILL Timeout.timeout(reprieve) {Process.wait(pid)}
else rescue Timeout::Error
break end
end while true end
end
bt = caller_locations bt = caller_locations
raise Timeout::Error, "execution of #{bt.shift.label} expired", bt.map(&:to_s) raise Timeout::Error, "execution of #{bt.shift.label} expired", bt.map(&:to_s)
end end