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

View File

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

View File

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

View File

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

View File

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

View File

@ -1340,13 +1340,15 @@ end
ctx2.enable_fallback_scsv
ctx2.max_version = OpenSSL::SSL::TLS1_1_VERSION
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/) {
s1.accept
}
assert_raise_with_message(OpenSSL::SSL::SSLError, /inappropriate fallback/) {
t.join
}
t.join
ensure
sock1.close
sock2.close

View File

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

View File

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

View File

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

View File

@ -183,12 +183,12 @@ class TestException < Test::Unit::TestCase
def test_throw_false
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
}.join
}
assert_same(false, e.tag, bug12743)
}
assert_same(false, e.tag, bug12743)
}.join
end
def test_else_no_exception

View File

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

View File

@ -3392,12 +3392,16 @@ __END__
str = ""
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?
t.raise
sleep 0.1 while t.alive?
assert_nothing_raised(RuntimeError, bug8669) { str.clear }
assert_raise(RuntimeError) { t.join }
t.join
}
end if /cygwin/ !~ RUBY_PLATFORM
@ -3406,12 +3410,16 @@ __END__
str = ""
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?
t.raise
sleep 0.1 while t.alive?
assert_nothing_raised(RuntimeError, bug8669) { str.clear }
assert_raise(RuntimeError) { t.join }
t.join
}
end if /cygwin/ !~ RUBY_PLATFORM
@ -3431,12 +3439,16 @@ __END__
str = ""
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?
t.raise
sleep 0.1 while t.alive?
assert_nothing_raised(RuntimeError, bug8669) { str.clear }
assert_raise(RuntimeError) { t.join }
t.join
}
end if /cygwin/ !~ RUBY_PLATFORM

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -373,7 +373,12 @@ class TestQueue < Test::Unit::TestCase
def test_blocked_pushers
q = SizedQueue.new 3
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
# 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
[->{Queue.new}, ->{SizedQueue.new 3}].each do |qcreate|
prod_threads = nil
q = qcreate[]
synq = Queue.new
producers_start = Thread.new do
prod_threads = 20.times.map do |i|
Thread.new{ synq.pop; q << i }
end
prod_threads = 20.times.map do |i|
Thread.new {
synq.pop
assert_raise(ClosedQueueError) {
q << i
}
}
end
q.close
synq.close # start producer threads
# wait for all threads to be finished, because of exceptions
# 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
prod_threads.each(&:join)
end
end
@ -451,7 +451,10 @@ class TestQueue < Test::Unit::TestCase
def test_blocked_pushers_empty
q = SizedQueue.new 3
prod_threads = 6.times.map do |i|
Thread.new{ q << i}
Thread.new{
Thread.current.report_on_exception = false
q << i
}
end
# 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.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?
t.raise
sleep 0.1 while t.alive?
t.join
assert_equal(tester.sync_waiting.uniq, tester.sync_waiting)
assert_equal(tester.sync_waiting, [])