ruby-changes:49073
From: eregon <ko1@a...>
Date: Wed, 13 Dec 2017 04:02:03 +0900 (JST)
Subject: [ruby-changes:49073] eregon:r61188 (trunk): Fix test-all tests to avoid creating report_on_exception warnings
eregon 2017-12-13 03:44:49 +0900 (Wed, 13 Dec 2017) New Revision: 61188 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61188 Log: 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. Modified files: trunk/test/-ext-/thread_fd_close/test_thread_fd_close.rb trunk/test/fiddle/test_func.rb trunk/test/fiddle/test_handle.rb trunk/test/fileutils/test_fileutils.rb trunk/test/net/imap/test_imap.rb trunk/test/openssl/test_ssl.rb trunk/test/readline/test_readline.rb trunk/test/rinda/test_rinda.rb trunk/test/ruby/test_continuation.rb trunk/test/ruby/test_exception.rb trunk/test/ruby/test_fiber.rb trunk/test/ruby/test_io.rb trunk/test/ruby/test_process.rb trunk/test/ruby/test_thread.rb trunk/test/rubygems/test_gem_request_connection_pools.rb trunk/test/socket/test_socket.rb trunk/test/thread/test_cv.rb trunk/test/thread/test_queue.rb trunk/test/thread/test_sync.rb Index: test/ruby/test_continuation.rb =================================================================== --- test/ruby/test_continuation.rb (revision 61187) +++ test/ruby/test_continuation.rb (revision 61188) @@ -37,9 +37,11 @@ class TestContinuation < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/ruby/test_continuation.rb#L37 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::Tes https://github.com/ruby/ruby/blob/trunk/test/ruby/test_continuation.rb#L134 assert_equal 3, @memo end end - Index: test/ruby/test_process.rb =================================================================== --- test/ruby/test_process.rb (revision 61187) +++ test/ruby/test_process.rb (revision 61188) @@ -1580,7 +1580,10 @@ class TestProcess < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_process.rb#L1580 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 Index: test/ruby/test_exception.rb =================================================================== --- test/ruby/test_exception.rb (revision 61187) +++ test/ruby/test_exception.rb (revision 61188) @@ -183,12 +183,12 @@ class TestException < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L183 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 Index: test/ruby/test_fiber.rb =================================================================== --- test/ruby/test_fiber.rb (revision 61187) +++ test/ruby/test_fiber.rb (revision 61188) @@ -70,10 +70,12 @@ class TestFiber < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_fiber.rb#L70 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_fiber.rb#L201 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_fiber.rb#L379 assert_match(/resumed/, Fiber.current.to_s) end end - Index: test/ruby/test_io.rb =================================================================== --- test/ruby/test_io.rb (revision 61187) +++ test/ruby/test_io.rb (revision 61188) @@ -3392,12 +3392,16 @@ __END__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L3392 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__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L3410 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__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L3439 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 Index: test/ruby/test_thread.rb =================================================================== --- test/ruby/test_thread.rb (revision 61187) +++ test/ruby/test_thread.rb (revision 61188) @@ -448,7 +448,10 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L448 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L563 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L618 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L770 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L783 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L972 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L1020 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 Index: test/-ext-/thread_fd_close/test_thread_fd_close.rb =================================================================== --- test/-ext-/thread_fd_close/test_thread_fd_close.rb (revision 61187) +++ test/-ext-/thread_fd_close/test_thread_fd_close.rb (revision 61188) @@ -9,7 +9,9 @@ class TestThreadFdClose < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/-ext-/thread_fd_close/test_thread_fd_close.rb#L9 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::Te https://github.com/ruby/ruby/blob/trunk/test/-ext-/thread_fd_close/test_thread_fd_close.rb#L19 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 Index: test/fileutils/test_fileutils.rb =================================================================== --- test/fileutils/test_fileutils.rb (revision 61187) +++ test/fileutils/test_fileutils.rb (revision 61188) @@ -235,6 +235,7 @@ class TestFileUtils < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L235 assert_raise(MiniTest::Assertion) { Timeout.timeout(0.1) { assert_output_lines([]) { + Thread.current.report_on_exception = false raise "ok" } } Index: test/thread/test_queue.rb =================================================================== --- test/thread/test_queue.rb (revision 61187) +++ test/thread/test_queue.rb (revision 61188) @@ -373,7 +373,12 @@ class TestQueue < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/thread/test_queue.rb#L373 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 https://github.com/ruby/ruby/blob/trunk/test/thread/test_queue.rb#L418 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 https://github.com/ruby/ruby/blob/trunk/test/thread/test_queue.rb#L451 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 Index: test/thread/test_cv.rb =================================================================== --- test/thread/test_cv.rb (revision 61187) +++ test/thread/test_cv.rb (revision 61188) @@ -42,12 +42,10 @@ class TestConditionVariable < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/thread/test_cv.rb#L42 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 https://github.com/ruby/ruby/blob/trunk/test/thread/test_cv.rb#L54 end thread.raise Interrupt, "interrupt a dead condition variable" - assert_raise(Interrupt) { thread.value } + thread.join assert(locked) end Index: test/thread/test_sync.rb =================================================================== --- test/thread/test_sync.rb (revision 61187) +++ test/thread/test_sync.rb (revision 61188) @@ -52,11 +52,15 @@ class SyncTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/thread/test_sync.rb#L52 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, []) Index: test/openssl/test_ssl.rb =================================================================== --- test/openssl/test_ssl.rb (revision 61187) +++ test/openssl/test_ssl.rb (revision 61188) @@ -1340,13 +1340,15 @@ end https://github.com/ruby/ruby/blob/trunk/test/openssl/test_ssl.rb#L1340 ctx2.enable_fallback_scsv ctx2.max_version = OpenSSL::SSL::TLS1_1_VERSION s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx2) - t = Thread.new { s2.connect } - assert_raise_with_message(OpenSSL::SSL::SSLError, /inappropriate fallback/) { - s1.accept + t = Thread.new { + assert_raise_with_message(OpenSSL::SSL::SSLError, /inappropriate fallback/) { + s2.connect + } } assert_raise_with_message(OpenSSL::SSL::SSLError, /inappropriate fallback/) { - t.join + s1.accept } + t.join ensure sock1.close sock2.close Index: test/fiddle/test_func.rb =================================================================== --- test/fiddle/test_func.rb (revision 61187) +++ test/fiddle/test_func.rb (revision 61188) @@ -13,12 +13,12 @@ module Fiddle https://github.com/ruby/ruby/blob/trunk/test/fiddle/test_func.rb#L13 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 Index: test/fiddle/test_handle.rb =================================================================== --- test/fiddle/test_handle.rb (revision 61187) +++ test/fiddle/test_handle.rb (revision 61188) @@ -9,20 +9,22 @@ module Fiddle https://github.com/ruby/ruby/blob/trunk/test/fiddle/test_handle.rb#L9 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 Index: test/rubygems/test_gem_request_connection_pools.rb =================================================================== --- test/rubygems/test_gem_request_connection_pools.rb (revision 61187) +++ test/rubygems/test_gem_request_connection_pools.rb (revision 61188) @@ -118,13 +118,12 @@ class TestGemRequestConnectionPool < Gem https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_request_connection_pools.rb#L118 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 Index: test/readline/test_readline.rb =================================================================== --- test/readline/test_readline.rb (revision 61187) +++ test/readline/test_readline.rb (revision 61188) @@ -45,14 +45,14 @@ class TestReadline < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/readline/test_readline.rb#L45 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 Index: test/net/imap/test_imap.rb =================================================================== --- test/net/imap/test_imap.rb (revision 61187) +++ test/net/imap/test_imap.rb (revision 61188) @@ -660,6 +660,7 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/net/imap/test_imap.rb#L660 } 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 Index: test/socket/test_socket.rb =================================================================== --- test/socket/test_socket.rb (revision 61187) +++ test/socket/test_socket.rb (revision 61188) @@ -536,13 +536,15 @@ class TestSocke (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/