diff options
Diffstat (limited to 'spec/ruby/core/thread/raise_spec.rb')
| -rw-r--r-- | spec/ruby/core/thread/raise_spec.rb | 66 |
1 files changed, 50 insertions, 16 deletions
diff --git a/spec/ruby/core/thread/raise_spec.rb b/spec/ruby/core/thread/raise_spec.rb index 27de3cc627..3b02a2e005 100644 --- a/spec/ruby/core/thread/raise_spec.rb +++ b/spec/ruby/core/thread/raise_spec.rb @@ -3,6 +3,16 @@ require_relative 'fixtures/classes' require_relative '../../shared/kernel/raise' describe "Thread#raise" do + it "is a public method" do + Thread.public_instance_methods.should.include?(:raise) + end + + it_behaves_like :kernel_raise, :raise, ThreadSpecs::NewThreadToRaise + it_behaves_like :kernel_raise_across_contexts, :raise, ThreadSpecs::NewThreadToRaise + ruby_version_is "4.0" do + it_behaves_like :kernel_raise_with_cause, :raise, ThreadSpecs::NewThreadToRaise + end + it "ignores dead threads and returns nil" do t = Thread.new { :dead } Thread.pass while t.alive? @@ -26,27 +36,27 @@ describe "Thread#raise on a sleeping thread" do it "raises a RuntimeError if no exception class is given" do @thr.raise Thread.pass while @thr.status - ScratchPad.recorded.should be_kind_of(RuntimeError) + ScratchPad.recorded.should.is_a?(RuntimeError) end it "raises the given exception" do @thr.raise Exception Thread.pass while @thr.status - ScratchPad.recorded.should be_kind_of(Exception) + ScratchPad.recorded.should.is_a?(Exception) end it "raises the given exception with the given message" do @thr.raise Exception, "get to work" Thread.pass while @thr.status - ScratchPad.recorded.should be_kind_of(Exception) + ScratchPad.recorded.should.is_a?(Exception) ScratchPad.recorded.message.should == "get to work" end it "raises the given exception and the backtrace is the one of the interrupted thread" do @thr.raise Exception Thread.pass while @thr.status - ScratchPad.recorded.should be_kind_of(Exception) - ScratchPad.recorded.backtrace[0].should include("sleep") + ScratchPad.recorded.should.is_a?(Exception) + ScratchPad.recorded.backtrace[0].should.include?("sleep") end it "is captured and raised by Thread#value" do @@ -58,7 +68,7 @@ describe "Thread#raise on a sleeping thread" do ThreadSpecs.spin_until_sleeping(t) t.raise - -> { t.value }.should raise_error(RuntimeError) + -> { t.value }.should.raise(RuntimeError) end it "raises a RuntimeError when called with no arguments inside rescue" do @@ -76,7 +86,7 @@ describe "Thread#raise on a sleeping thread" do ThreadSpecs.spin_until_sleeping(t) t.raise end - -> { t.value }.should raise_error(RuntimeError) + -> { t.value }.should.raise(RuntimeError) end it "re-raises a previously rescued exception without overwriting the backtrace" do @@ -98,9 +108,33 @@ describe "Thread#raise on a sleeping thread" do raise_again_line = __LINE__; t.raise raised raised_again = t.value - raised_again.backtrace.first.should include("#{__FILE__}:#{initial_raise_line}:") - raised_again.backtrace.first.should_not include("#{__FILE__}:#{raise_again_line}:") + raised_again.backtrace.first.should.include?("#{__FILE__}:#{initial_raise_line}:") + raised_again.backtrace.first.should_not.include?("#{__FILE__}:#{raise_again_line}:") + end + end + + it "calls #exception in both the caller and in the target thread" do + cls = Class.new(Exception) do + attr_accessor :log + def initialize(*args) + @log = [] # This is shared because the super #exception uses a shallow clone + super + end + + def exception(*args) + @log << [self, Thread.current, args] + super + end end + exc = cls.new + + @thr.raise exc, "Thread#raise #exception spec" + @thr.join + ScratchPad.recorded.should.is_a?(cls) + exc.log.should == [ + [exc, Thread.current, ["Thread#raise #exception spec"]], + [ScratchPad.recorded, @thr, []] + ] end end @@ -121,19 +155,19 @@ describe "Thread#raise on a running thread" do it "raises a RuntimeError if no exception class is given" do @thr.raise Thread.pass while @thr.status - ScratchPad.recorded.should be_kind_of(RuntimeError) + ScratchPad.recorded.should.is_a?(RuntimeError) end it "raises the given exception" do @thr.raise Exception Thread.pass while @thr.status - ScratchPad.recorded.should be_kind_of(Exception) + ScratchPad.recorded.should.is_a?(Exception) end it "raises the given exception with the given message" do @thr.raise Exception, "get to work" Thread.pass while @thr.status - ScratchPad.recorded.should be_kind_of(Exception) + ScratchPad.recorded.should.is_a?(Exception) ScratchPad.recorded.message.should == "get to work" end @@ -147,7 +181,7 @@ describe "Thread#raise on a running thread" do q.pop # wait for `report_on_exception = false`. t.raise - -> { t.value }.should raise_error(RuntimeError) + -> { t.value }.should.raise(RuntimeError) end it "raises the given argument even when there is an active exception" do @@ -166,7 +200,7 @@ describe "Thread#raise on a running thread" do rescue Thread.pass until raised t.raise RangeError - -> { t.value }.should raise_error(RangeError) + -> { t.value }.should.raise(RangeError) end end @@ -187,7 +221,7 @@ describe "Thread#raise on a running thread" do Thread.pass until raised t.raise end - -> { t.value }.should raise_error(RuntimeError) + -> { t.value }.should.raise(RuntimeError) end end @@ -203,6 +237,6 @@ describe "Thread#raise on same thread" do Thread.current.raise end end - -> { t.value }.should raise_error(RuntimeError, '') + -> { t.value }.should.raise(RuntimeError, '') end end |
