summaryrefslogtreecommitdiff
path: root/spec/ruby/core/signal/trap_spec.rb
blob: 3c78922694009cfd9cf09bad48e4602a6d91ecaa (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require_relative '../../spec_helper'

describe "Signal.trap" do
  platform_is_not :windows do
    before :each do
      ScratchPad.clear
      @proc = -> {}
      @saved_trap = Signal.trap(:HUP, @proc)
      @hup_number = Signal.list["HUP"]
    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

    it "accepts a block" do
      done = false

      Signal.trap(:HUP) do |signo|
        signo.should == @hup_number
        ScratchPad.record :block_trap
        done = true
      end

      Process.kill :HUP, Process.pid
      Thread.pass until done

      ScratchPad.recorded.should == :block_trap
    end

    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

    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

    it "registers an handler doing nothing with :IGNORE" do
      Signal.trap :HUP, :IGNORE
      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

      Process.kill(:HUP, Process.pid).should == 1
      Thread.pass until done
      ScratchPad.recorded.should == :block_trap
    end

    it "ignores the signal when passed nil" do
      Signal.trap :HUP, nil
      Signal.trap(:HUP, @saved_trap).should be_nil
    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_DFL in place of a proc" do
      Signal.trap :HUP, :SIG_DFL
      Signal.trap(:HUP, @saved_trap).should == "DEFAULT"
    end

    it "accepts :SIG_IGN in place of a proc" do
      Signal.trap :HUP, :SIG_IGN
      Signal.trap(:HUP, @saved_trap).should == "IGNORE"
    end

    it "accepts :IGNORE in place of a proc" do
      Signal.trap :HUP, :IGNORE
      Signal.trap(:HUP, @saved_trap).should == "IGNORE"
    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"
      Signal.trap(:HUP, @saved_trap).should == "IGNORE"
    end

    it "accepts 'IGNORE' in place of a proc" do
      Signal.trap :HUP, "IGNORE"
      Signal.trap(:HUP, @saved_trap).should == "IGNORE"
    end

    it "accepts long names as Strings" do
      Signal.trap "SIGHUP", @proc
      Signal.trap("SIGHUP", @saved_trap).should equal(@proc)
    end

    it "accepts short names as Strings" do
      Signal.trap "HUP", @proc
      Signal.trap("HUP", @saved_trap).should equal(@proc)
    end

    it "accepts long names as Symbols" do
      Signal.trap :SIGHUP, @proc
      Signal.trap(:SIGHUP, @saved_trap).should equal(@proc)
    end

    it "accepts short names as Symbols" do
      Signal.trap :HUP, @proc
      Signal.trap(:HUP, @saved_trap).should equal(@proc)
    end

    # See man 2 signal
    %w[KILL STOP].each do |signal|
      it "raises ArgumentError or Errno::EINVAL for SIG#{signal}" do
        -> {
          Signal.trap(signal, -> {})
        }.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

    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")
      out.should == "OK\n"
      $?.exitstatus.should == 0
    end

    it "returns 'DEFAULT' for the initial SIGINT handler" do
      ruby_exe("print Signal.trap(:INT) { abort }").should == 'DEFAULT'
    end

    it "returns SYSTEM_DEFAULT if passed DEFAULT and no handler was ever set" do
      Signal.trap("PROF", "DEFAULT").should == "SYSTEM_DEFAULT"
    end

    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
      out = ruby_exe(code, exit_status: nil)
      status = $?
      out.should == "nil\n"
      status.should.signaled?
      status.termsig.should be_kind_of(Integer)
      Signal.signame(status.termsig).should == "PIPE"
    end
  end

  describe "the special EXIT signal code" do
    it "accepts the EXIT code" do
      code = "Signal.trap(:EXIT, proc { print 1 })"
      ruby_exe(code).should == "1"
    end

    it "runs the proc before at_exit handlers" do
      code = "at_exit {print 1}; Signal.trap(:EXIT, proc {print 2}); at_exit {print 3}"
      ruby_exe(code).should == "231"
    end

    it "can unset the handler" do
      code = "Signal.trap(:EXIT, proc { print 1 }); Signal.trap(:EXIT, 'DEFAULT')"
      ruby_exe(code).should == ""
    end
  end

end