summaryrefslogtreecommitdiff
path: root/spec/ruby/core/signal
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
commit1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch)
treea3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/signal
parent75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff)
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/signal')
-rw-r--r--spec/ruby/core/signal/list_spec.rb64
-rw-r--r--spec/ruby/core/signal/signame_spec.rb23
-rw-r--r--spec/ruby/core/signal/trap_spec.rb135
3 files changed, 222 insertions, 0 deletions
diff --git a/spec/ruby/core/signal/list_spec.rb b/spec/ruby/core/signal/list_spec.rb
new file mode 100644
index 0000000000..510b671337
--- /dev/null
+++ b/spec/ruby/core/signal/list_spec.rb
@@ -0,0 +1,64 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Signal.list" do
+ RUBY_SIGNALS = %w{
+ EXIT
+ HUP
+ INT
+ QUIT
+ ILL
+ TRAP
+ IOT
+ ABRT
+ EMT
+ FPE
+ KILL
+ BUS
+ SEGV
+ SYS
+ PIPE
+ ALRM
+ TERM
+ URG
+ STOP
+ TSTP
+ CONT
+ CHLD
+ CLD
+ TTIN
+ TTOU
+ IO
+ XCPU
+ XFSZ
+ VTALRM
+ PROF
+ WINCH
+ USR1
+ USR2
+ LOST
+ MSG
+ PWR
+ POLL
+ DANGER
+ MIGRATE
+ PRE
+ GRANT
+ RETRACT
+ SOUND
+ INFO
+ }
+
+ it "doesn't contain other signals than the known list" do
+ (Signal.list.keys - RUBY_SIGNALS).should == []
+ end
+
+ if Signal.list["CHLD"]
+ it "redefines CLD with CHLD if defined" do
+ Signal.list["CLD"].should == Signal.list["CHLD"]
+ end
+ end
+
+ it "includes the EXIT key with a value of zero" do
+ Signal.list["EXIT"].should == 0
+ end
+end
diff --git a/spec/ruby/core/signal/signame_spec.rb b/spec/ruby/core/signal/signame_spec.rb
new file mode 100644
index 0000000000..1874a67933
--- /dev/null
+++ b/spec/ruby/core/signal/signame_spec.rb
@@ -0,0 +1,23 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Signal.signame" do
+ it "takes a signal name with a well known signal number" do
+ Signal.signame(0).should == "EXIT"
+ end
+
+ ruby_version_is "2.0"..."2.3" do
+ it "raises an ArgumentError if the argument is an invalid signal number" do
+ lambda { Signal.signame(-1) }.should raise_error(ArgumentError)
+ end
+ end
+
+ ruby_version_is "2.3" do
+ it "returns nil if the argument is an invalid signal number" do
+ Signal.signame(-1).should == nil
+ end
+ end
+
+ it "raises a TypeError when the passed argument can't be coerced to Integer" do
+ lambda { Signal.signame("hello") }.should raise_error(TypeError)
+ end
+end
diff --git a/spec/ruby/core/signal/trap_spec.rb b/spec/ruby/core/signal/trap_spec.rb
new file mode 100644
index 0000000000..787de1735c
--- /dev/null
+++ b/spec/ruby/core/signal/trap_spec.rb
@@ -0,0 +1,135 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+platform_is_not :windows do
+ describe "Signal.trap" do
+ before :each do
+ ScratchPad.clear
+
+ @proc = lambda { ScratchPad.record :proc_trap }
+ @saved_trap = Signal.trap(:HUP, @proc)
+ 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 in place of a proc/command argument" do
+ done = false
+
+ Signal.trap(:HUP) do
+ ScratchPad.record :block_trap
+ done = true
+ end
+
+ Process.kill :HUP, Process.pid
+ Thread.pass until done
+
+ ScratchPad.recorded.should == :block_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 "ignores the signal when passed nil" do
+ Signal.trap :HUP, nil
+ Signal.trap(:HUP, @saved_trap).should be_nil
+ end
+
+ it "accepts 'DEFAULT' as a symbol in place of a proc" do
+ Signal.trap :HUP, :DEFAULT
+ Signal.trap(:HUP, :DEFAULT).should == "DEFAULT"
+ end
+
+ it "accepts 'SIG_DFL' as a symbol in place of a proc" do
+ Signal.trap :HUP, :SIG_DFL
+ Signal.trap(:HUP, :SIG_DFL).should == "DEFAULT"
+ end
+
+ it "accepts 'SIG_IGN' as a symbol in place of a proc" do
+ Signal.trap :HUP, :SIG_IGN
+ Signal.trap(:HUP, :SIG_IGN).should == "IGNORE"
+ end
+
+ it "accepts 'IGNORE' as a symbol in place of a proc" do
+ Signal.trap :HUP, :IGNORE
+ Signal.trap(:HUP, :IGNORE).should == "IGNORE"
+ end
+
+ it "accepts long names as Strings" do
+ Signal.trap "SIGHUP", @proc
+ Signal.trap("SIGHUP", @saved_trap).should equal(@proc)
+ end
+
+ it "acceps 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
+
+ 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, "SIG_IGN").should == "IGNORE"
+ end
+
+ it "accepts 'IGNORE' in place of a proc" do
+ Signal.trap :HUP, "IGNORE"
+ Signal.trap(:HUP, "IGNORE").should == "IGNORE"
+ end
+ end
+end
+
+describe "Signal.trap" do
+ describe "the special EXIT signal code" do
+ it "accepts the EXIT code" do
+ code = "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}; trap(:EXIT, proc {print 2}); at_exit {print 3}"
+ ruby_exe(code).should == "231"
+ end
+
+ it "can unset the handler" do
+ code = "trap(:EXIT, proc { print 1 }); trap(:EXIT, 'DEFAULT')"
+ ruby_exe(code).should == ""
+ end
+ end
+end