Fix test-all tests to avoid creating report_on_exception warnings

* The warnings are shown by Thread.report_on_exception defaulting to
  true. [Feature #14143] [ruby-core:83979]
* Improves tests by narrowing down the scope where an exception
  is expected.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61188 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2017-12-12 18:44:49 +00:00
parent 967eab83e3
commit 15689ed778
19 changed files with 181 additions and 148 deletions

View File

@ -9,7 +9,9 @@ class TestThreadFdClose < Test::Unit::TestCase
IO.pipe do |r, w| IO.pipe do |r, w|
th = Thread.new do th = Thread.new do
begin begin
assert_raise(IOError) {
r.read(4) r.read(4)
}
ensure ensure
w.syswrite('done') w.syswrite('done')
end end
@ -17,7 +19,7 @@ class TestThreadFdClose < Test::Unit::TestCase
Thread.pass until th.stop? Thread.pass until th.stop?
IO.thread_fd_close(r.fileno) IO.thread_fd_close(r.fileno)
assert_equal 'done', r.read(4) assert_equal 'done', r.read(4)
assert_raise(IOError) { th.join } th.join
end end
end end
end end

View File

@ -13,12 +13,12 @@ module Fiddle
def test_syscall_with_tainted_string def test_syscall_with_tainted_string
f = Function.new(@libc['system'], [TYPE_VOIDP], TYPE_INT) f = Function.new(@libc['system'], [TYPE_VOIDP], TYPE_INT)
assert_raise(SecurityError) do
Thread.new { Thread.new {
$SAFE = 1 $SAFE = 1
assert_raise(SecurityError) do
f.call("uname -rs".dup.taint) f.call("uname -rs".dup.taint)
}.join
end end
}.join
end end
def test_sinf def test_sinf

View File

@ -9,20 +9,22 @@ module Fiddle
include Fiddle include Fiddle
def test_safe_handle_open def test_safe_handle_open
t = Thread.new do Thread.new do
$SAFE = 1 $SAFE = 1
assert_raise(SecurityError) {
Fiddle::Handle.new(LIBC_SO.dup.taint) Fiddle::Handle.new(LIBC_SO.dup.taint)
end }
assert_raise(SecurityError) { t.value } end.join
end end
def test_safe_function_lookup def test_safe_function_lookup
t = Thread.new do Thread.new do
h = Fiddle::Handle.new(LIBC_SO) h = Fiddle::Handle.new(LIBC_SO)
$SAFE = 1 $SAFE = 1
assert_raise(SecurityError) {
h["qsort".dup.taint] h["qsort".dup.taint]
end }
assert_raise(SecurityError) { t.value } end.join
end end
def test_to_i def test_to_i

View File

@ -235,6 +235,7 @@ class TestFileUtils < Test::Unit::TestCase
assert_raise(MiniTest::Assertion) { assert_raise(MiniTest::Assertion) {
Timeout.timeout(0.1) { Timeout.timeout(0.1) {
assert_output_lines([]) { assert_output_lines([]) {
Thread.current.report_on_exception = false
raise "ok" raise "ok"
} }
} }

View File

@ -660,6 +660,7 @@ EOF
} }
ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx) ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx)
ths = Thread.start do ths = Thread.start do
Thread.current.report_on_exception = false # always join-ed
begin begin
sock = ssl_server.accept sock = ssl_server.accept
begin begin

View File

@ -1340,13 +1340,15 @@ end
ctx2.enable_fallback_scsv ctx2.enable_fallback_scsv
ctx2.max_version = OpenSSL::SSL::TLS1_1_VERSION ctx2.max_version = OpenSSL::SSL::TLS1_1_VERSION
s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx2) s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx2)
t = Thread.new { s2.connect } t = Thread.new {
assert_raise_with_message(OpenSSL::SSL::SSLError, /inappropriate fallback/) {
s2.connect
}
}
assert_raise_with_message(OpenSSL::SSL::SSLError, /inappropriate fallback/) { assert_raise_with_message(OpenSSL::SSL::SSLError, /inappropriate fallback/) {
s1.accept s1.accept
} }
assert_raise_with_message(OpenSSL::SSL::SSLError, /inappropriate fallback/) {
t.join t.join
}
ensure ensure
sock1.close sock1.close
sock2.close sock2.close

View File

@ -45,14 +45,14 @@ class TestReadline < Test::Unit::TestCase
assert_equal("> ", stdout.read(2)) assert_equal("> ", stdout.read(2))
assert_equal(1, Readline::HISTORY.length) assert_equal(1, Readline::HISTORY.length)
assert_equal("hello", Readline::HISTORY[0]) assert_equal("hello", Readline::HISTORY[0])
assert_raise(SecurityError) do
Thread.start { Thread.start {
$SAFE = 1 $SAFE = 1
assert_raise(SecurityError) do
replace_stdio(stdin.path, stdout.path) do replace_stdio(stdin.path, stdout.path) do
Readline.readline("> ".taint) Readline.readline("> ".taint)
end end
}.join
end end
}.join
end end
end end

View File

@ -241,17 +241,21 @@ module TupleSpaceTestModule
end end
def test_ruby_talk_264062 def test_ruby_talk_264062
th = Thread.new { @ts.take([:empty], 1) } th = Thread.new {
sleep(10)
assert_raise(Rinda::RequestExpiredError) do assert_raise(Rinda::RequestExpiredError) do
thread_join(th) @ts.take([:empty], 1)
end end
}
sleep(10)
thread_join(th)
th = Thread.new { @ts.read([:empty], 1) } th = Thread.new {
sleep(10)
assert_raise(Rinda::RequestExpiredError) do assert_raise(Rinda::RequestExpiredError) do
thread_join(th) @ts.read([:empty], 1)
end end
}
sleep(10)
thread_join(th)
end end
def test_symbol_tuple def test_symbol_tuple
@ -348,6 +352,7 @@ module TupleSpaceTestModule
template = nil template = nil
taker = Thread.new do taker = Thread.new do
assert_raise(Rinda::RequestCanceledError) do
@ts.take([:take, nil], 10) do |t| @ts.take([:take, nil], 10) do |t|
template = t template = t
Thread.new do Thread.new do
@ -355,12 +360,10 @@ module TupleSpaceTestModule
end end
end end
end end
end
sleep(2) sleep(2)
assert_raise(Rinda::RequestCanceledError) do
thread_join(taker) thread_join(taker)
end
assert(template.canceled?) assert(template.canceled?)
@ -377,6 +380,7 @@ module TupleSpaceTestModule
template = nil template = nil
reader = Thread.new do reader = Thread.new do
assert_raise(Rinda::RequestCanceledError) do
@ts.read([:take, nil], 10) do |t| @ts.read([:take, nil], 10) do |t|
template = t template = t
Thread.new do Thread.new do
@ -384,12 +388,10 @@ module TupleSpaceTestModule
end end
end end
end end
end
sleep(2) sleep(2)
assert_raise(Rinda::RequestCanceledError) do
thread_join(reader) thread_join(reader)
end
assert(template.canceled?) assert(template.canceled?)

View File

@ -37,9 +37,11 @@ class TestContinuation < Test::Unit::TestCase
def test_error def test_error
cont = callcc{|c| c} cont = callcc{|c| c}
Thread.new{
assert_raise(RuntimeError){ assert_raise(RuntimeError){
Thread.new{cont.call}.join cont.call
} }
}.join
assert_raise(LocalJumpError){ assert_raise(LocalJumpError){
callcc callcc
} }
@ -132,4 +134,3 @@ class TestContinuation < Test::Unit::TestCase
assert_equal 3, @memo assert_equal 3, @memo
end end
end end

View File

@ -183,12 +183,12 @@ class TestException < Test::Unit::TestCase
def test_throw_false def test_throw_false
bug12743 = '[ruby-core:77229] [Bug #12743]' bug12743 = '[ruby-core:77229] [Bug #12743]'
e = assert_raise_with_message(UncaughtThrowError, /false/, bug12743) {
Thread.start { Thread.start {
e = assert_raise_with_message(UncaughtThrowError, /false/, bug12743) {
throw false throw false
}.join
} }
assert_same(false, e.tag, bug12743) assert_same(false, e.tag, bug12743)
}.join
end end
def test_else_no_exception def test_else_no_exception

View File

@ -70,10 +70,12 @@ class TestFiber < Test::Unit::TestCase
assert_raise(ArgumentError){ assert_raise(ArgumentError){
Fiber.new # Fiber without block Fiber.new # Fiber without block
} }
assert_raise(FiberError){
f = Fiber.new{} f = Fiber.new{}
Thread.new{f.resume}.join # Fiber yielding across thread Thread.new{
assert_raise(FiberError){ # Fiber yielding across thread
f.resume
} }
}.join
assert_raise(FiberError){ assert_raise(FiberError){
f = Fiber.new{} f = Fiber.new{}
f.resume f.resume
@ -199,11 +201,11 @@ class TestFiber < Test::Unit::TestCase
end end
def test_resume_root_fiber def test_resume_root_fiber
assert_raise(FiberError) do
Thread.new do Thread.new do
assert_raise(FiberError) do
Fiber.current.resume Fiber.current.resume
end.join
end end
end.join
end end
def test_gc_root_fiber def test_gc_root_fiber
@ -377,4 +379,3 @@ class TestFiber < Test::Unit::TestCase
assert_match(/resumed/, Fiber.current.to_s) assert_match(/resumed/, Fiber.current.to_s)
end end
end end

View File

@ -3392,12 +3392,16 @@ __END__
str = "" str = ""
IO.pipe {|r,| IO.pipe {|r,|
t = Thread.new { r.read(nil, str) } t = Thread.new {
assert_raise(RuntimeError) {
r.read(nil, str)
}
}
sleep 0.1 until t.stop? sleep 0.1 until t.stop?
t.raise t.raise
sleep 0.1 while t.alive? sleep 0.1 while t.alive?
assert_nothing_raised(RuntimeError, bug8669) { str.clear } assert_nothing_raised(RuntimeError, bug8669) { str.clear }
assert_raise(RuntimeError) { t.join } t.join
} }
end if /cygwin/ !~ RUBY_PLATFORM end if /cygwin/ !~ RUBY_PLATFORM
@ -3406,12 +3410,16 @@ __END__
str = "" str = ""
IO.pipe {|r, w| IO.pipe {|r, w|
t = Thread.new { r.readpartial(4096, str) } t = Thread.new {
assert_raise(RuntimeError) {
r.readpartial(4096, str)
}
}
sleep 0.1 until t.stop? sleep 0.1 until t.stop?
t.raise t.raise
sleep 0.1 while t.alive? sleep 0.1 while t.alive?
assert_nothing_raised(RuntimeError, bug8669) { str.clear } assert_nothing_raised(RuntimeError, bug8669) { str.clear }
assert_raise(RuntimeError) { t.join } t.join
} }
end if /cygwin/ !~ RUBY_PLATFORM end if /cygwin/ !~ RUBY_PLATFORM
@ -3431,12 +3439,16 @@ __END__
str = "" str = ""
IO.pipe {|r, w| IO.pipe {|r, w|
t = Thread.new { r.sysread(4096, str) } t = Thread.new {
assert_raise(RuntimeError) {
r.sysread(4096, str)
}
}
sleep 0.1 until t.stop? sleep 0.1 until t.stop?
t.raise t.raise
sleep 0.1 while t.alive? sleep 0.1 while t.alive?
assert_nothing_raised(RuntimeError, bug8669) { str.clear } assert_nothing_raised(RuntimeError, bug8669) { str.clear }
assert_raise(RuntimeError) { t.join } t.join
} }
end if /cygwin/ !~ RUBY_PLATFORM end if /cygwin/ !~ RUBY_PLATFORM

View File

@ -1580,7 +1580,10 @@ class TestProcess < Test::Unit::TestCase
pid = nil pid = nil
IO.pipe do |r, w| IO.pipe do |r, w|
pid = fork { r.read(1); exit } pid = fork { r.read(1); exit }
Thread.start { raise } Thread.start {
Thread.current.report_on_exception = false
raise
}
w.puts w.puts
end end
Process.wait pid Process.wait pid

View File

@ -448,7 +448,10 @@ class TestThread < Test::Unit::TestCase
end end
def test_status_and_stop_p def test_status_and_stop_p
a = ::Thread.new { raise("die now") } a = ::Thread.new {
Thread.current.report_on_exception = false
raise("die now")
}
b = Thread.new { Thread.stop } b = Thread.new { Thread.stop }
c = Thread.new { Thread.exit } c = Thread.new { Thread.exit }
e = Thread.current e = Thread.current
@ -560,13 +563,13 @@ class TestThread < Test::Unit::TestCase
end end
def test_thread_local_security def test_thread_local_security
assert_raise(FrozenError) do
Thread.new do Thread.new do
Thread.current[:foo] = :bar Thread.current[:foo] = :bar
Thread.current.freeze Thread.current.freeze
assert_raise(FrozenError) do
Thread.current[:foo] = :baz Thread.current[:foo] = :baz
end.join
end end
end.join
end end
def test_thread_local_dynamic_symbol def test_thread_local_dynamic_symbol
@ -615,11 +618,11 @@ class TestThread < Test::Unit::TestCase
def test_mutex_illegal_unlock def test_mutex_illegal_unlock
m = Thread::Mutex.new m = Thread::Mutex.new
m.lock m.lock
assert_raise(ThreadError) do
Thread.new do Thread.new do
assert_raise(ThreadError) do
m.unlock m.unlock
end.join
end end
end.join
end end
def test_mutex_fifo_like_lock def test_mutex_fifo_like_lock
@ -767,12 +770,12 @@ class TestThread < Test::Unit::TestCase
r=:ng r=:ng
e=Class.new(Exception) e=Class.new(Exception)
th_s = Thread.current th_s = Thread.current
begin
th = Thread.start{ th = Thread.start{
assert_raise(RuntimeError) {
Thread.handle_interrupt(Object => :on_blocking){ Thread.handle_interrupt(Object => :on_blocking){
begin begin
Thread.pass until r == :wait Thread.pass until r == :wait
Thread.current.raise RuntimeError Thread.current.raise RuntimeError, "will raise in sleep"
r = :ok r = :ok
sleep sleep
ensure ensure
@ -780,11 +783,9 @@ class TestThread < Test::Unit::TestCase
end end
} }
} }
}
assert_raise(e) {r = :wait; sleep 0.2} assert_raise(e) {r = :wait; sleep 0.2}
assert_raise(RuntimeError) {th.join(0.2)} assert_not_equal nil, th.join
ensure
th.kill
end
assert_equal(:ok,r) assert_equal(:ok,r)
end end
@ -971,11 +972,11 @@ _eom
end end
def test_thread_join_main_thread def test_thread_join_main_thread
assert_raise(ThreadError) do
Thread.new(Thread.current) {|t| Thread.new(Thread.current) {|t|
assert_raise(ThreadError) do
t.join t.join
}.join
end end
}.join
end end
def test_main_thread_status_at_exit def test_main_thread_status_at_exit
@ -1019,6 +1020,7 @@ q.pop
ary = [] ary = []
t = Thread.new { t = Thread.new {
assert_raise(RuntimeError) do
begin begin
ary << Thread.current.status ary << Thread.current.status
sleep #1 sleep #1
@ -1030,9 +1032,9 @@ q.pop
ary << Thread.current.status ary << Thread.current.status
end end
end end
end
} }
begin
Thread.pass until ary.size >= 1 Thread.pass until ary.size >= 1
Thread.pass until t.stop? Thread.pass until t.stop?
t.kill # wake up sleep #1 t.kill # wake up sleep #1
@ -1041,9 +1043,7 @@ q.pop
t.raise "wakeup" # wake up sleep #2 t.raise "wakeup" # wake up sleep #2
Thread.pass while t.alive? Thread.pass while t.alive?
assert_equal(ary, ["run", "aborting", "aborting"]) assert_equal(ary, ["run", "aborting", "aborting"])
ensure t.join
t.join rescue nil
end
end end
def test_mutex_owned def test_mutex_owned

View File

@ -118,13 +118,12 @@ class TestGemRequestConnectionPool < Gem::TestCase
pool.checkout pool.checkout
t1 = Thread.new { Thread.new {
assert_raises(Timeout::Error) do
Timeout.timeout(1) do Timeout.timeout(1) do
pool.checkout pool.checkout
end end
} end
assert_raises(Timeout::Error) do }.join
t1.join
end
end end
end end

View File

@ -536,13 +536,15 @@ class TestSocket < Test::Unit::TestCase
begin sleep(0.1) end until serv_thread.stop? begin sleep(0.1) end until serv_thread.stop?
sock = TCPSocket.new("localhost", server.addr[1]) sock = TCPSocket.new("localhost", server.addr[1])
client_thread = Thread.new do client_thread = Thread.new do
assert_raise(IOError, bug4390) {
sock.readline sock.readline
}
end end
begin sleep(0.1) end until client_thread.stop? begin sleep(0.1) end until client_thread.stop?
Timeout.timeout(1) do Timeout.timeout(1) do
sock.close sock.close
sock = nil sock = nil
assert_raise(IOError, bug4390) {client_thread.join} client_thread.join
end end
ensure ensure
serv_thread.value.close serv_thread.value.close

View File

@ -42,12 +42,10 @@ class TestConditionVariable < Test::Unit::TestCase
thread = Thread.new do thread = Thread.new do
Thread.current.abort_on_exception = false Thread.current.abort_on_exception = false
mutex.synchronize do mutex.synchronize do
begin assert_raise(Interrupt) {
condvar.wait(mutex) condvar.wait(mutex)
rescue Exception }
locked = mutex.locked? locked = mutex.locked?
raise
end
end end
end end
@ -56,7 +54,7 @@ class TestConditionVariable < Test::Unit::TestCase
end end
thread.raise Interrupt, "interrupt a dead condition variable" thread.raise Interrupt, "interrupt a dead condition variable"
assert_raise(Interrupt) { thread.value } thread.join
assert(locked) assert(locked)
end end

View File

@ -373,7 +373,12 @@ class TestQueue < Test::Unit::TestCase
def test_blocked_pushers def test_blocked_pushers
q = SizedQueue.new 3 q = SizedQueue.new 3
prod_threads = 6.times.map do |i| prod_threads = 6.times.map do |i|
thr = Thread.new{q << i}; thr[:pc] = i; thr thr = Thread.new{
Thread.current.report_on_exception = false
q << i
}
thr[:pc] = i
thr
end end
# wait until some producer threads have finished, and the other 3 are blocked # wait until some producer threads have finished, and the other 3 are blocked
@ -413,25 +418,20 @@ class TestQueue < Test::Unit::TestCase
def test_deny_pushers def test_deny_pushers
[->{Queue.new}, ->{SizedQueue.new 3}].each do |qcreate| [->{Queue.new}, ->{SizedQueue.new 3}].each do |qcreate|
prod_threads = nil
q = qcreate[] q = qcreate[]
synq = Queue.new synq = Queue.new
producers_start = Thread.new do
prod_threads = 20.times.map do |i| prod_threads = 20.times.map do |i|
Thread.new{ synq.pop; q << i } Thread.new {
end synq.pop
assert_raise(ClosedQueueError) {
q << i
}
}
end end
q.close q.close
synq.close # start producer threads synq.close # start producer threads
# wait for all threads to be finished, because of exceptions prod_threads.each(&:join)
# NOTE: thr.status will be nil (raised) or false (terminated)
sleep 0.01 until prod_threads&.all?{|thr| !thr.status}
# check that all threads failed to call push
prod_threads.each do |thr|
assert_kind_of ClosedQueueError, (thr.value rescue $!)
end
end end
end end
@ -451,7 +451,10 @@ class TestQueue < Test::Unit::TestCase
def test_blocked_pushers_empty def test_blocked_pushers_empty
q = SizedQueue.new 3 q = SizedQueue.new 3
prod_threads = 6.times.map do |i| prod_threads = 6.times.map do |i|
Thread.new{ q << i} Thread.new{
Thread.current.report_on_exception = false
q << i
}
end end
# this ensures that all producer threads call push before close # this ensures that all producer threads call push before close

View File

@ -52,11 +52,15 @@ class SyncTest < Test::Unit::TestCase
tester= Tester.new tester= Tester.new
tester.sync_lock(:EX) tester.sync_lock(:EX)
t = Thread.new { tester.sync_lock(:EX) } t = Thread.new {
assert_raise(RuntimeError) {
tester.sync_lock(:EX)
}
}
sleep 0.1 until t.stop? sleep 0.1 until t.stop?
t.raise t.raise
sleep 0.1 while t.alive? t.join
assert_equal(tester.sync_waiting.uniq, tester.sync_waiting) assert_equal(tester.sync_waiting.uniq, tester.sync_waiting)
assert_equal(tester.sync_waiting, []) assert_equal(tester.sync_waiting, [])