diff options
Diffstat (limited to 'spec/ruby/core/thread/shared')
| -rw-r--r-- | spec/ruby/core/thread/shared/exit.rb | 43 | ||||
| -rw-r--r-- | spec/ruby/core/thread/shared/start.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/thread/shared/to_s.rb | 53 | ||||
| -rw-r--r-- | spec/ruby/core/thread/shared/wakeup.rb | 5 |
4 files changed, 98 insertions, 5 deletions
diff --git a/spec/ruby/core/thread/shared/exit.rb b/spec/ruby/core/thread/shared/exit.rb index 4686b7b28b..13e8832684 100644 --- a/spec/ruby/core/thread/shared/exit.rb +++ b/spec/ruby/core/thread/shared/exit.rb @@ -66,6 +66,26 @@ describe :thread_exit, shared: true do ScratchPad.recorded.should == nil end + it "does not reset $!" do + ScratchPad.record [] + + exc = RuntimeError.new("foo") + thread = Thread.new do + begin + raise exc + ensure + ScratchPad << $! + begin + Thread.current.send(@method) + ensure + ScratchPad << $! + end + end + end + thread.join + ScratchPad.recorded.should == [exc, exc] + end + it "cannot be rescued" do thread = Thread.new do begin @@ -73,7 +93,7 @@ describe :thread_exit, shared: true do rescue Exception ScratchPad.record :in_rescue end - ScratchPad.record :end_of_thread_block + ScratchPad.record :end_of_thread_block end thread.join @@ -93,6 +113,25 @@ describe :thread_exit, shared: true do ScratchPad.recorded.should == nil end + it "kills other fibers of that thread without running their ensure clauses" do + t = Thread.new do + f = Fiber.new do + ScratchPad.record :fiber_resumed + begin + Fiber.yield + ensure + ScratchPad.record :fiber_ensure + end + end + f.resume + sleep + end + Thread.pass until t.stop? + t.send(@method) + t.join + ScratchPad.recorded.should == :fiber_resumed + end + # This spec is a mess. It fails randomly, it hangs on MRI, it needs to be removed quarantine! do it "killing dying running does nothing" do @@ -116,7 +155,7 @@ describe :thread_exit, shared: true do it "propagates inner exception to Thread.join if there is an outer ensure clause" do thread = ThreadSpecs.dying_thread_with_outer_ensure(@method) { } - lambda { thread.join }.should raise_error(RuntimeError, "In dying thread") + -> { thread.join }.should raise_error(RuntimeError, "In dying thread") end it "runs all outer ensure clauses even if inner ensure clause raises exception" do diff --git a/spec/ruby/core/thread/shared/start.rb b/spec/ruby/core/thread/shared/start.rb index 80ce063a0e..2ba926bf00 100644 --- a/spec/ruby/core/thread/shared/start.rb +++ b/spec/ruby/core/thread/shared/start.rb @@ -4,7 +4,7 @@ describe :thread_start, shared: true do end it "raises an ArgumentError if not passed a block" do - lambda { + -> { Thread.send(@method) }.should raise_error(ArgumentError) end diff --git a/spec/ruby/core/thread/shared/to_s.rb b/spec/ruby/core/thread/shared/to_s.rb new file mode 100644 index 0000000000..43640deb33 --- /dev/null +++ b/spec/ruby/core/thread/shared/to_s.rb @@ -0,0 +1,53 @@ +require_relative '../fixtures/classes' + +describe :thread_to_s, shared: true do + it "returns a description including file and line number" do + thread, line = Thread.new { "hello" }, __LINE__ + thread.join + thread.send(@method).should =~ /^#<Thread:([^ ]*?) #{Regexp.escape __FILE__}:#{line} \w+>$/ + end + + it "has a binary encoding" do + ThreadSpecs.status_of_current_thread.send(@method).encoding.should == Encoding::BINARY + end + + it "can check it's own status" do + ThreadSpecs.status_of_current_thread.send(@method).should include('run') + end + + it "describes a running thread" do + ThreadSpecs.status_of_running_thread.send(@method).should include('run') + end + + it "describes a sleeping thread" do + ThreadSpecs.status_of_sleeping_thread.send(@method).should include('sleep') + end + + it "describes a blocked thread" do + ThreadSpecs.status_of_blocked_thread.send(@method).should include('sleep') + end + + it "describes a completed thread" do + ThreadSpecs.status_of_completed_thread.send(@method).should include('dead') + end + + it "describes a killed thread" do + ThreadSpecs.status_of_killed_thread.send(@method).should include('dead') + end + + it "describes a thread with an uncaught exception" do + ThreadSpecs.status_of_thread_with_uncaught_exception.send(@method).should include('dead') + end + + it "describes a dying sleeping thread" do + ThreadSpecs.status_of_dying_sleeping_thread.send(@method).should include('sleep') + end + + it "reports aborting on a killed thread" do + ThreadSpecs.status_of_dying_running_thread.send(@method).should include('aborting') + end + + it "reports aborting on a killed thread after sleep" do + ThreadSpecs.status_of_dying_thread_after_sleep.send(@method).should include('aborting') + end +end diff --git a/spec/ruby/core/thread/shared/wakeup.rb b/spec/ruby/core/thread/shared/wakeup.rb index 71838d88e5..6f010fea25 100644 --- a/spec/ruby/core/thread/shared/wakeup.rb +++ b/spec/ruby/core/thread/shared/wakeup.rb @@ -36,7 +36,7 @@ describe :thread_wakeup, shared: true do it "does not result in a deadlock" do t = Thread.new do - 100.times { Thread.stop } + 10.times { Thread.stop } end while t.status @@ -47,6 +47,7 @@ describe :thread_wakeup, shared: true do t.status.should == false end Thread.pass + sleep 0.001 end t.status.should == false @@ -56,6 +57,6 @@ describe :thread_wakeup, shared: true do it "raises a ThreadError when trying to wake up a dead thread" do t = Thread.new { 1 } t.join - lambda { t.send @method }.should raise_error(ThreadError) + -> { t.send @method }.should raise_error(ThreadError) end end |
