1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
require 'test/unit'
require 'timeout'
require 'tempfile'
require_relative 'envutil'
class TestSignal < Test::Unit::TestCase
def have_fork?
begin
Process.fork {}
return true
rescue NotImplementedError
return false
end
end
def test_signal
return unless Process.respond_to?(:kill)
begin
x = 0
oldtrap = Signal.trap(:INT) {|sig| x = 2 }
Process.kill :INT, Process.pid
10.times do
break if 2 == x
sleep 0.1
end
assert_equal 2, x
Signal.trap(:INT) { raise "Interrupt" }
ex = assert_raise(RuntimeError) {
Process.kill :INT, Process.pid
sleep 0.1
}
assert_kind_of Exception, ex
assert_match(/Interrupt/, ex.message)
ensure
Signal.trap :INT, oldtrap if oldtrap
end
end
def test_signal_process_group
return unless Process.respond_to?(:kill)
return unless Process.respond_to?(:pgroup) # for mswin32
bug4362 = '[ruby-dev:43169]'
assert_nothing_raised(bug4362) do
pid = Process.spawn(EnvUtil.rubybin, '-e', '"sleep 10"', :pgroup => true)
Process.kill(:"-TERM", pid)
Process.waitpid(pid)
assert_equal(true, $?.signaled?)
assert_equal(Signal.list["TERM"], $?.termsig)
end
end
def test_exit_action
return unless have_fork? # skip this test
begin
r, w = IO.pipe
r0, w0 = IO.pipe
pid = Process.spawn(EnvUtil.rubybin, '-e', <<-'End', 3=>w, 4=>r0)
w = IO.new(3, "w")
r0 = IO.new(4, "r")
Signal.trap(:USR1, "EXIT")
w.syswrite("a")
Thread.start { sleep(2) }
r0.sysread(4096)
End
r.sysread(1)
sleep 0.1
assert_nothing_raised("[ruby-dev:26128]") {
Process.kill(:USR1, pid)
begin
Timeout.timeout(3) {
Process.waitpid pid
}
rescue Timeout::Error
Process.kill(:TERM, pid)
raise
end
}
ensure
r.close
w.close
r0.close
w0.close
end
end
def test_invalid_signal_name
return unless Process.respond_to?(:kill)
assert_raise(ArgumentError) { Process.kill(:XXXXXXXXXX, $$) }
end
def test_signal_exception
assert_raise(ArgumentError) { SignalException.new }
assert_raise(ArgumentError) { SignalException.new(-1) }
assert_raise(ArgumentError) { SignalException.new(:XXXXXXXXXX) }
Signal.list.each do |signm, signo|
next if signm == "EXIT"
assert_equal(SignalException.new(signm).signo, signo)
assert_equal(SignalException.new(signm.to_sym).signo, signo)
assert_equal(SignalException.new(signo).signo, signo)
end
end
def test_interrupt
assert_raise(Interrupt) { raise Interrupt.new }
end
def test_signal2
return unless Process.respond_to?(:kill)
begin
x = false
oldtrap = Signal.trap(:INT) {|sig| x = true }
GC.start
assert_raise(ArgumentError) { Process.kill }
Timeout.timeout(10) do
x = false
Process.kill(SignalException.new(:INT).signo, $$)
sleep(0.01) until x
x = false
Process.kill("INT", $$)
sleep(0.01) until x
x = false
Process.kill("SIGINT", $$)
sleep(0.01) until x
x = false
o = Object.new
def o.to_str; "SIGINT"; end
Process.kill(o, $$)
sleep(0.01) until x
end
assert_raise(ArgumentError) { Process.kill(Object.new, $$) }
ensure
Signal.trap(:INT, oldtrap) if oldtrap
end
end
def test_trap
return unless Process.respond_to?(:kill)
begin
oldtrap = Signal.trap(:INT) {|sig| }
assert_raise(ArgumentError) { Signal.trap }
assert_raise(SecurityError) do
s = proc {}.taint
Signal.trap(:INT, s)
end
# FIXME!
Signal.trap(:INT, nil)
Signal.trap(:INT, "")
Signal.trap(:INT, "SIG_IGN")
Signal.trap(:INT, "IGNORE")
Signal.trap(:INT, "SIG_DFL")
Signal.trap(:INT, "SYSTEM_DEFAULT")
Signal.trap(:INT, "EXIT")
Signal.trap(:INT, "xxxxxx")
Signal.trap(:INT, "xxxx")
Signal.trap(SignalException.new(:INT).signo, "SIG_DFL")
assert_raise(ArgumentError) { Signal.trap(-1, "xxxx") }
o = Object.new
def o.to_str; "SIGINT"; end
Signal.trap(o, "SIG_DFL")
assert_raise(ArgumentError) { Signal.trap("XXXXXXXXXX", "SIG_DFL") }
ensure
Signal.trap(:INT, oldtrap) if oldtrap
end
end
def test_kill_immediately_before_termination
return unless have_fork? # skip this test
r, w = IO.pipe
pid = Process.fork do
r.close
Signal.trap(:USR1) { w.syswrite("foo") }
Process.kill :USR1, $$
end
w.close
assert_equal(r.read, "foo")
end
def test_signal_requiring
skip "limitation of GenerateConsoleCtrlEvent()" if /mswin|mingw/ =~ RUBY_PLATFORM
t = Tempfile.new(%w"require_ensure_test .rb")
t.puts "sleep"
t.close
error = IO.popen([EnvUtil.rubybin, "-e", <<EOS, t.path, :err => File::NULL]) do |child|
trap(:INT, "DEFAULT")
th = Thread.new do
begin
require ARGV[0]
ensure
Marshal.dump($!, STDOUT)
end
end
STDOUT.puts
STDOUT.flush
th.join
EOS
child.gets
Process.kill("INT", child.pid)
Marshal.load(child)
end
t.close!
assert_nil(error)
end
end
|