2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
2005-06-03 10:39:15 -04:00
|
|
|
require 'timeout'
|
2003-09-04 12:18:59 -04:00
|
|
|
|
|
|
|
class TestSignal < Test::Unit::TestCase
|
2005-09-15 09:42:15 -04:00
|
|
|
def have_fork?
|
|
|
|
begin
|
|
|
|
fork{}
|
|
|
|
true
|
|
|
|
rescue NotImplementedError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
def test_signal
|
2003-10-19 22:31:47 -04:00
|
|
|
defined?(Process.kill) or return
|
|
|
|
begin
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = 0
|
2003-10-19 22:31:47 -04:00
|
|
|
oldtrap = trap "SIGINT", proc{|sig| $x = 2}
|
2003-09-04 12:18:59 -04:00
|
|
|
Process.kill "SIGINT", $$
|
|
|
|
sleep 0.1
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(2, $x)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
trap "SIGINT", proc{raise "Interrupt"}
|
2003-09-05 11:15:43 -04:00
|
|
|
|
|
|
|
x = assert_raises(RuntimeError) do
|
2003-09-04 12:18:59 -04:00
|
|
|
Process.kill "SIGINT", $$
|
|
|
|
sleep 0.1
|
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
assert(x)
|
|
|
|
assert_match(/Interrupt/, x.message)
|
2003-10-19 22:31:47 -04:00
|
|
|
ensure
|
|
|
|
trap "SIGINT", oldtrap
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
end
|
2005-06-03 10:39:15 -04:00
|
|
|
|
|
|
|
def test_exit_action
|
2005-09-15 09:42:15 -04:00
|
|
|
return unless have_fork? # snip this test
|
2005-06-03 10:39:15 -04:00
|
|
|
begin
|
|
|
|
r, w = IO.pipe
|
2005-07-03 05:37:49 -04:00
|
|
|
r0, w0 = IO.pipe
|
2005-06-03 10:39:15 -04:00
|
|
|
pid = fork {
|
|
|
|
trap(:USR1, "EXIT")
|
2005-07-03 05:37:49 -04:00
|
|
|
w0.close
|
|
|
|
w.syswrite("a")
|
2005-06-03 10:39:15 -04:00
|
|
|
Thread.start { Thread.pass }
|
|
|
|
r0.sysread(4096)
|
|
|
|
}
|
2005-07-03 05:37:49 -04:00
|
|
|
r.sysread(1)
|
2005-06-03 10:39:15 -04:00
|
|
|
sleep 0.1
|
|
|
|
assert_nothing_raised("[ruby-dev:26128]") {
|
|
|
|
Process.kill(:USR1, pid)
|
2005-06-05 03:04:06 -04:00
|
|
|
begin
|
|
|
|
Timeout.timeout(1) {
|
|
|
|
Process.waitpid pid
|
|
|
|
}
|
|
|
|
rescue Timeout::Error
|
|
|
|
Process.kill(:TERM, pid)
|
|
|
|
raise
|
|
|
|
end
|
2005-06-03 10:39:15 -04:00
|
|
|
}
|
|
|
|
ensure
|
|
|
|
r.close
|
|
|
|
w.close
|
2005-07-03 05:37:49 -04:00
|
|
|
r0.close
|
|
|
|
w0.close
|
2005-06-03 10:39:15 -04:00
|
|
|
end
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|