From 8c5b60eb22d6d661e87992a65d54e3a5bc0aeed4 Mon Sep 17 00:00:00 2001 From: eregon Date: Sat, 28 Oct 2017 15:15:48 +0000 Subject: Update to ruby/spec@a6b8805 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60525 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/core/thread/element_set_spec.rb | 8 +- spec/ruby/core/thread/fixtures/classes.rb | 7 +- spec/ruby/core/thread/join_spec.rb | 5 +- spec/ruby/core/thread/key_spec.rb | 2 +- spec/ruby/core/thread/raise_spec.rb | 6 ++ spec/ruby/core/thread/report_on_exception_spec.rb | 102 ++++++++++++++++++++++ spec/ruby/core/thread/shared/exit.rb | 2 +- spec/ruby/core/thread/value_spec.rb | 5 +- 8 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 spec/ruby/core/thread/report_on_exception_spec.rb (limited to 'spec/ruby/core/thread') diff --git a/spec/ruby/core/thread/element_set_spec.rb b/spec/ruby/core/thread/element_set_spec.rb index 47b4d06328..b76078f685 100644 --- a/spec/ruby/core/thread/element_set_spec.rb +++ b/spec/ruby/core/thread/element_set_spec.rb @@ -9,12 +9,12 @@ describe "Thread#[]=" do it "raises a RuntimeError if the thread is frozen" do running = false t = Thread.new do - Thread.pass until running t.freeze - t[:foo] = "bar" + -> { + t[:foo] = "bar" + }.should raise_error(RuntimeError, /frozen/) end - running = true - lambda { t.join }.should raise_error(RuntimeError) + t.join end it "raises exceptions on the wrong type of keys" do diff --git a/spec/ruby/core/thread/fixtures/classes.rb b/spec/ruby/core/thread/fixtures/classes.rb index b572c8dd82..601e515e3e 100644 --- a/spec/ruby/core/thread/fixtures/classes.rb +++ b/spec/ruby/core/thread/fixtures/classes.rb @@ -120,7 +120,10 @@ module ThreadSpecs end def self.status_of_thread_with_uncaught_exception - t = Thread.new { raise "error" } + t = Thread.new { + Thread.current.report_on_exception = false + raise "error" + } begin t.join rescue RuntimeError @@ -159,6 +162,7 @@ module ThreadSpecs def self.dying_thread_ensures(kill_method_name=:kill) Thread.new do + Thread.current.report_on_exception = false begin Thread.current.send(kill_method_name) ensure @@ -169,6 +173,7 @@ module ThreadSpecs def self.dying_thread_with_outer_ensure(kill_method_name=:kill) Thread.new do + Thread.current.report_on_exception = false begin begin Thread.current.send(kill_method_name) diff --git a/spec/ruby/core/thread/join_spec.rb b/spec/ruby/core/thread/join_spec.rb index a6dd2da9a3..249b3d333e 100644 --- a/spec/ruby/core/thread/join_spec.rb +++ b/spec/ruby/core/thread/join_spec.rb @@ -46,7 +46,10 @@ describe "Thread#join" do end it "raises any exceptions encountered in the thread body" do - t = Thread.new { raise NotImplementedError.new("Just kidding") } + t = Thread.new { + Thread.current.report_on_exception = false + raise NotImplementedError.new("Just kidding") + } lambda { t.join }.should raise_error(NotImplementedError) end diff --git a/spec/ruby/core/thread/key_spec.rb b/spec/ruby/core/thread/key_spec.rb index 6cdfc3ca7f..d82a21ab39 100644 --- a/spec/ruby/core/thread/key_spec.rb +++ b/spec/ruby/core/thread/key_spec.rb @@ -9,7 +9,7 @@ describe "Thread#key?" do @th.join end - it "tests for existance of thread local variables using symbols or strings" do + it "tests for existence of thread local variables using symbols or strings" do @th.key?(:oliver).should == true @th.key?("oliver").should == true @th.key?(:stanley).should == false diff --git a/spec/ruby/core/thread/raise_spec.rb b/spec/ruby/core/thread/raise_spec.rb index 93e0f048b1..8724d26202 100644 --- a/spec/ruby/core/thread/raise_spec.rb +++ b/spec/ruby/core/thread/raise_spec.rb @@ -51,6 +51,7 @@ describe "Thread#raise on a sleeping thread" do it "is captured and raised by Thread#value" do t = Thread.new do + Thread.current.report_on_exception = false sleep end @@ -62,6 +63,7 @@ describe "Thread#raise on a sleeping thread" do it "raises a RuntimeError when called with no arguments inside rescue" do t = Thread.new do + Thread.current.report_on_exception = false begin 1/0 rescue ZeroDivisionError @@ -113,6 +115,7 @@ describe "Thread#raise on a running thread" do it "can go unhandled" do t = Thread.new do + Thread.current.report_on_exception = false loop { Thread.pass } end @@ -123,6 +126,7 @@ describe "Thread#raise on a running thread" do it "raises the given argument even when there is an active exception" do raised = false t = Thread.new do + Thread.current.report_on_exception = false begin 1/0 rescue ZeroDivisionError @@ -142,6 +146,7 @@ describe "Thread#raise on a running thread" do it "raises a RuntimeError when called with no arguments inside rescue" do raised = false t = Thread.new do + Thread.current.report_on_exception = false begin 1/0 rescue ZeroDivisionError @@ -164,6 +169,7 @@ describe "Thread#raise on same thread" do it "raises a RuntimeError when called with no arguments inside rescue" do t = Thread.new do + Thread.current.report_on_exception = false begin 1/0 rescue ZeroDivisionError diff --git a/spec/ruby/core/thread/report_on_exception_spec.rb b/spec/ruby/core/thread/report_on_exception_spec.rb new file mode 100644 index 0000000000..4128dad470 --- /dev/null +++ b/spec/ruby/core/thread/report_on_exception_spec.rb @@ -0,0 +1,102 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is "2.4" do + describe "Thread.report_on_exception" do + it "defaults to false" do + ruby_exe("p Thread.report_on_exception").should == "false\n" + end + end + + describe "Thread.report_on_exception=" do + before :each do + @report_on_exception = Thread.report_on_exception + end + + after :each do + Thread.report_on_exception = @report_on_exception + end + + it "changes the default value for new threads" do + Thread.report_on_exception = true + Thread.report_on_exception.should == true + t = Thread.new {} + t.join + t.report_on_exception.should == true + end + end + + describe "Thread#report_on_exception" do + it "returns whether the Thread will print a backtrace if it exits with an exception" do + t = Thread.new { Thread.current.report_on_exception = true } + t.join + t.report_on_exception.should == true + + t = Thread.new { Thread.current.report_on_exception = false } + t.join + t.report_on_exception.should == false + end + end + + describe "Thread#report_on_exception=" do + describe "when set to true" do + it "prints a backtrace on $stderr if it terminates with an exception" do + t = nil + -> { + t = Thread.new { + Thread.current.report_on_exception = true + raise RuntimeError, "Thread#report_on_exception specs" + } + Thread.pass while t.alive? + }.should output("", /Thread.+terminated with exception.+Thread#report_on_exception specs/m) + + -> { + t.join + }.should raise_error(RuntimeError, "Thread#report_on_exception specs") + end + end + + describe "when set to false" do + it "lets the thread terminates silently with an exception" do + t = nil + -> { + t = Thread.new { + Thread.current.report_on_exception = false + raise RuntimeError, "Thread#report_on_exception specs" + } + Thread.pass while t.alive? + }.should output("", "") + + -> { + t.join + }.should raise_error(RuntimeError, "Thread#report_on_exception specs") + end + end + + ruby_bug "#13163", "2.4"..."2.5" do + describe "when used in conjunction with Thread#abort_on_exception" do + it "first reports then send the exception back to the main Thread" do + t = nil + mutex = Mutex.new + mutex.lock + -> { + t = Thread.new { + Thread.current.abort_on_exception = true + Thread.current.report_on_exception = true + mutex.lock + mutex.unlock + raise RuntimeError, "Thread#report_on_exception specs" + } + + -> { + mutex.sleep(5) + }.should raise_error(RuntimeError, "Thread#report_on_exception specs") + }.should output("", /Thread.+terminated with exception.+Thread#report_on_exception specs/m) + + -> { + t.join + }.should raise_error(RuntimeError, "Thread#report_on_exception specs") + end + end + end + end +end diff --git a/spec/ruby/core/thread/shared/exit.rb b/spec/ruby/core/thread/shared/exit.rb index f15da360fd..0c9198c538 100644 --- a/spec/ruby/core/thread/shared/exit.rb +++ b/spec/ruby/core/thread/shared/exit.rb @@ -112,7 +112,7 @@ describe :thread_exit, shared: true do quarantine! do - it "propogates inner exception to Thread.join if there is an outer ensure clause" 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") end diff --git a/spec/ruby/core/thread/value_spec.rb b/spec/ruby/core/thread/value_spec.rb index 82c0cbf762..3d900959df 100644 --- a/spec/ruby/core/thread/value_spec.rb +++ b/spec/ruby/core/thread/value_spec.rb @@ -7,7 +7,10 @@ describe "Thread#value" do end it "re-raises an error for an uncaught exception" do - t = Thread.new { raise "Hello" } + t = Thread.new { + Thread.current.report_on_exception = false + raise "Hello" + } lambda { t.value }.should raise_error(RuntimeError, "Hello") end -- cgit v1.2.3