2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2021-03-27 08:02:41 -04:00
|
|
|
describe "Signal.trap" do
|
|
|
|
platform_is_not :windows do
|
2017-05-07 08:04:49 -04:00
|
|
|
before :each do
|
|
|
|
ScratchPad.clear
|
2019-06-27 15:02:36 -04:00
|
|
|
@proc = -> {}
|
2017-05-07 08:04:49 -04:00
|
|
|
@saved_trap = Signal.trap(:HUP, @proc)
|
2021-03-27 08:02:41 -04:00
|
|
|
@hup_number = Signal.list["HUP"]
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after :each do
|
|
|
|
Signal.trap(:HUP, @saved_trap) if @saved_trap
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the previous handler" do
|
|
|
|
Signal.trap(:HUP, @saved_trap).should equal(@proc)
|
|
|
|
end
|
|
|
|
|
2021-03-27 08:02:41 -04:00
|
|
|
it "accepts a block" do
|
2017-05-07 08:04:49 -04:00
|
|
|
done = false
|
|
|
|
|
2021-03-27 08:02:41 -04:00
|
|
|
Signal.trap(:HUP) do |signo|
|
|
|
|
signo.should == @hup_number
|
2017-05-07 08:04:49 -04:00
|
|
|
ScratchPad.record :block_trap
|
|
|
|
done = true
|
|
|
|
end
|
|
|
|
|
|
|
|
Process.kill :HUP, Process.pid
|
|
|
|
Thread.pass until done
|
|
|
|
|
|
|
|
ScratchPad.recorded.should == :block_trap
|
|
|
|
end
|
|
|
|
|
2021-03-27 08:02:41 -04:00
|
|
|
it "accepts a proc" do
|
|
|
|
done = false
|
|
|
|
|
|
|
|
handler = -> signo {
|
|
|
|
signo.should == @hup_number
|
|
|
|
ScratchPad.record :proc_trap
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
|
|
|
|
Signal.trap(:HUP, handler)
|
|
|
|
|
|
|
|
Process.kill :HUP, Process.pid
|
|
|
|
Thread.pass until done
|
|
|
|
|
|
|
|
ScratchPad.recorded.should == :proc_trap
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts a method" do
|
|
|
|
done = false
|
|
|
|
|
|
|
|
handler_class = Class.new
|
|
|
|
hup_number = @hup_number
|
|
|
|
|
|
|
|
handler_class.define_method :handler_method do |signo|
|
|
|
|
signo.should == hup_number
|
|
|
|
ScratchPad.record :method_trap
|
|
|
|
done = true
|
|
|
|
end
|
|
|
|
|
|
|
|
handler_method = handler_class.new.method(:handler_method)
|
|
|
|
|
|
|
|
Signal.trap(:HUP, handler_method)
|
|
|
|
|
|
|
|
Process.kill :HUP, Process.pid
|
|
|
|
Thread.pass until done
|
|
|
|
|
|
|
|
ScratchPad.recorded.should == :method_trap
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts anything you can call" do
|
|
|
|
done = false
|
|
|
|
|
|
|
|
callable = Object.new
|
|
|
|
hup_number = @hup_number
|
|
|
|
|
|
|
|
callable.singleton_class.define_method :call do |signo|
|
|
|
|
signo.should == hup_number
|
|
|
|
ScratchPad.record :callable_trap
|
|
|
|
done = true
|
|
|
|
end
|
|
|
|
|
|
|
|
Signal.trap(:HUP, callable)
|
|
|
|
|
|
|
|
Process.kill :HUP, Process.pid
|
|
|
|
Thread.pass until done
|
|
|
|
|
|
|
|
ScratchPad.recorded.should == :callable_trap
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception for a non-callable at the point of use" do
|
|
|
|
not_callable = Object.new
|
|
|
|
Signal.trap(:HUP, not_callable)
|
|
|
|
-> {
|
|
|
|
Process.kill :HUP, Process.pid
|
|
|
|
loop { Thread.pass }
|
|
|
|
}.should raise_error(NoMethodError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts a non-callable that becomes callable when used" do
|
|
|
|
done = false
|
|
|
|
|
|
|
|
late_callable = Object.new
|
|
|
|
hup_number = @hup_number
|
|
|
|
|
|
|
|
Signal.trap(:HUP, late_callable)
|
|
|
|
|
|
|
|
late_callable.singleton_class.define_method :call do |signo|
|
|
|
|
signo.should == hup_number
|
|
|
|
ScratchPad.record :late_callable_trap
|
|
|
|
done = true
|
|
|
|
end
|
|
|
|
|
|
|
|
Process.kill :HUP, Process.pid
|
|
|
|
Thread.pass until done
|
|
|
|
|
|
|
|
ScratchPad.recorded.should == :late_callable_trap
|
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "is possible to create a new Thread when the handler runs" do
|
|
|
|
done = false
|
|
|
|
|
|
|
|
Signal.trap(:HUP) do
|
|
|
|
thr = Thread.new { }
|
|
|
|
thr.join
|
|
|
|
ScratchPad.record(thr.group == Thread.main.group)
|
|
|
|
|
|
|
|
done = true
|
|
|
|
end
|
|
|
|
|
|
|
|
Process.kill :HUP, Process.pid
|
|
|
|
Thread.pass until done
|
|
|
|
|
|
|
|
ScratchPad.recorded.should be_true
|
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "registers an handler doing nothing with :IGNORE" do
|
|
|
|
Signal.trap :HUP, :IGNORE
|
2021-02-27 07:00:26 -05:00
|
|
|
Signal.trap(:HUP, @saved_trap).should == "IGNORE"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can register a new handler after :IGNORE" do
|
|
|
|
Signal.trap :HUP, :IGNORE
|
|
|
|
|
|
|
|
done = false
|
|
|
|
Signal.trap(:HUP) do
|
|
|
|
ScratchPad.record :block_trap
|
|
|
|
done = true
|
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
Process.kill(:HUP, Process.pid).should == 1
|
2021-02-27 07:00:26 -05:00
|
|
|
Thread.pass until done
|
|
|
|
ScratchPad.recorded.should == :block_trap
|
2019-06-27 15:02:36 -04:00
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "ignores the signal when passed nil" do
|
|
|
|
Signal.trap :HUP, nil
|
|
|
|
Signal.trap(:HUP, @saved_trap).should be_nil
|
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "accepts :DEFAULT in place of a proc" do
|
2017-05-07 08:04:49 -04:00
|
|
|
Signal.trap :HUP, :DEFAULT
|
2019-06-27 15:02:36 -04:00
|
|
|
Signal.trap(:HUP, @saved_trap).should == "DEFAULT"
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "accepts :SIG_DFL in place of a proc" do
|
2017-05-07 08:04:49 -04:00
|
|
|
Signal.trap :HUP, :SIG_DFL
|
2019-06-27 15:02:36 -04:00
|
|
|
Signal.trap(:HUP, @saved_trap).should == "DEFAULT"
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "accepts :SIG_IGN in place of a proc" do
|
2017-05-07 08:04:49 -04:00
|
|
|
Signal.trap :HUP, :SIG_IGN
|
2019-06-27 15:02:36 -04:00
|
|
|
Signal.trap(:HUP, @saved_trap).should == "IGNORE"
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "accepts :IGNORE in place of a proc" do
|
2017-05-07 08:04:49 -04:00
|
|
|
Signal.trap :HUP, :IGNORE
|
2019-06-27 15:02:36 -04:00
|
|
|
Signal.trap(:HUP, @saved_trap).should == "IGNORE"
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts 'SIG_DFL' in place of a proc" do
|
|
|
|
Signal.trap :HUP, "SIG_DFL"
|
|
|
|
Signal.trap(:HUP, @saved_trap).should == "DEFAULT"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts 'DEFAULT' in place of a proc" do
|
|
|
|
Signal.trap :HUP, "DEFAULT"
|
|
|
|
Signal.trap(:HUP, @saved_trap).should == "DEFAULT"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts 'SIG_IGN' in place of a proc" do
|
|
|
|
Signal.trap :HUP, "SIG_IGN"
|
2019-06-27 15:02:36 -04:00
|
|
|
Signal.trap(:HUP, @saved_trap).should == "IGNORE"
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts 'IGNORE' in place of a proc" do
|
|
|
|
Signal.trap :HUP, "IGNORE"
|
2019-06-27 15:02:36 -04:00
|
|
|
Signal.trap(:HUP, @saved_trap).should == "IGNORE"
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "accepts long names as Strings" do
|
|
|
|
Signal.trap "SIGHUP", @proc
|
|
|
|
Signal.trap("SIGHUP", @saved_trap).should equal(@proc)
|
|
|
|
end
|
2018-08-03 12:19:40 -04:00
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "accepts short names as Strings" do
|
|
|
|
Signal.trap "HUP", @proc
|
|
|
|
Signal.trap("HUP", @saved_trap).should equal(@proc)
|
2018-08-03 12:19:40 -04:00
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "accepts long names as Symbols" do
|
|
|
|
Signal.trap :SIGHUP, @proc
|
|
|
|
Signal.trap(:SIGHUP, @saved_trap).should equal(@proc)
|
2018-08-03 12:19:40 -04:00
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "accepts short names as Symbols" do
|
|
|
|
Signal.trap :HUP, @proc
|
|
|
|
Signal.trap(:HUP, @saved_trap).should equal(@proc)
|
2018-08-03 12:19:40 -04:00
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
# See man 2 signal
|
|
|
|
%w[KILL STOP].each do |signal|
|
2018-08-03 12:19:40 -04:00
|
|
|
it "raises ArgumentError or Errno::EINVAL for SIG#{signal}" do
|
|
|
|
-> {
|
2021-03-27 08:02:41 -04:00
|
|
|
Signal.trap(signal, -> {})
|
2018-08-03 12:19:40 -04:00
|
|
|
}.should raise_error(StandardError) { |e|
|
|
|
|
[ArgumentError, Errno::EINVAL].should include(e.class)
|
|
|
|
e.message.should =~ /Invalid argument|Signal already used by VM or OS/
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "allows to register a handler for all known signals, except reserved signals for which it raises ArgumentError" do
|
|
|
|
out = ruby_exe(fixture(__FILE__, "trap_all.rb"), args: "2>&1")
|
2018-08-03 12:19:40 -04:00
|
|
|
out.should == "OK\n"
|
|
|
|
$?.exitstatus.should == 0
|
|
|
|
end
|
|
|
|
|
2018-08-27 10:25:00 -04:00
|
|
|
it "returns 'DEFAULT' for the initial SIGINT handler" do
|
2021-03-27 08:02:41 -04:00
|
|
|
ruby_exe("print Signal.trap(:INT) { abort }").should == 'DEFAULT'
|
2018-08-27 10:25:00 -04:00
|
|
|
end
|
|
|
|
|
2018-01-29 11:08:16 -05:00
|
|
|
it "returns SYSTEM_DEFAULT if passed DEFAULT and no handler was ever set" do
|
|
|
|
Signal.trap("PROF", "DEFAULT").should == "SYSTEM_DEFAULT"
|
|
|
|
end
|
2018-08-27 10:25:00 -04:00
|
|
|
|
|
|
|
it "accepts 'SYSTEM_DEFAULT' and uses the OS handler for SIGPIPE" do
|
|
|
|
code = <<-RUBY
|
|
|
|
p Signal.trap('PIPE', 'SYSTEM_DEFAULT')
|
|
|
|
r, w = IO.pipe
|
|
|
|
r.close
|
|
|
|
loop { w.write("a"*1024) }
|
|
|
|
RUBY
|
2022-06-26 08:50:14 -04:00
|
|
|
out = ruby_exe(code, exit_status: :SIGPIPE)
|
2018-08-27 10:25:00 -04:00
|
|
|
status = $?
|
|
|
|
out.should == "nil\n"
|
2020-05-03 06:28:29 -04:00
|
|
|
status.should.signaled?
|
2018-08-27 10:25:00 -04:00
|
|
|
end
|
2018-01-29 11:08:16 -05:00
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
describe "the special EXIT signal code" do
|
|
|
|
it "accepts the EXIT code" do
|
2021-03-27 08:02:41 -04:00
|
|
|
code = "Signal.trap(:EXIT, proc { print 1 })"
|
2017-05-07 08:04:49 -04:00
|
|
|
ruby_exe(code).should == "1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs the proc before at_exit handlers" do
|
2021-03-27 08:02:41 -04:00
|
|
|
code = "at_exit {print 1}; Signal.trap(:EXIT, proc {print 2}); at_exit {print 3}"
|
2017-05-07 08:04:49 -04:00
|
|
|
ruby_exe(code).should == "231"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can unset the handler" do
|
2021-03-27 08:02:41 -04:00
|
|
|
code = "Signal.trap(:EXIT, proc { print 1 }); Signal.trap(:EXIT, 'DEFAULT')"
|
2017-05-07 08:04:49 -04:00
|
|
|
ruby_exe(code).should == ""
|
|
|
|
end
|
|
|
|
end
|
2021-03-27 08:02:41 -04:00
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|