diff options
Diffstat (limited to 'test/ruby/test_thread.rb')
-rw-r--r-- | test/ruby/test_thread.rb | 156 |
1 files changed, 143 insertions, 13 deletions
diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb index 19a3529005..da14c429e6 100644 --- a/test/ruby/test_thread.rb +++ b/test/ruby/test_thread.rb @@ -3,6 +3,7 @@ require 'test/unit' require "rbconfig/sizeof" require "timeout" +require "fiddle" class TestThread < Test::Unit::TestCase class Thread < ::Thread @@ -29,13 +30,19 @@ class TestThread < Test::Unit::TestCase end def test_inspect + m = Thread::Mutex.new + m.lock line = __LINE__+1 - th = Module.new {break module_eval("class C\u{30b9 30ec 30c3 30c9} < Thread; self; end")}.start{} + th = Module.new {break module_eval("class C\u{30b9 30ec 30c3 30c9} < Thread; self; end")}.start do + m.synchronize {} + end + Thread.pass until th.stop? s = th.inspect assert_include(s, "::C\u{30b9 30ec 30c3 30c9}:") assert_include(s, " #{__FILE__}:#{line} ") assert_equal(s, th.to_s) ensure + m.unlock th.join end @@ -235,6 +242,14 @@ class TestThread < Test::Unit::TestCase t3&.kill&.join end + def test_join_argument_conversion + t = Thread.new {} + assert_raise(TypeError) {t.join(:foo)} + + limit = Struct.new(:to_f, :count).new(0.05) + assert_same(t, t.join(limit)) + end + { 'FIXNUM_MAX' => RbConfig::LIMITS['FIXNUM_MAX'], 'UINT64_MAX' => RbConfig::LIMITS['UINT64_MAX'], 'INFINITY' => Float::INFINITY @@ -309,7 +324,7 @@ class TestThread < Test::Unit::TestCase s += 1 end Thread.pass until t.stop? - sleep 1 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # t.stop? behaves unexpectedly with --jit-wait + sleep 1 if defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled? # t.stop? behaves unexpectedly with --jit-wait assert_equal(1, s) t.wakeup Thread.pass while t.alive? @@ -381,7 +396,7 @@ class TestThread < Test::Unit::TestCase end INPUT - assert_in_out_err(%w(--disable-gems -d), <<-INPUT, %w(false 2), %r".+") + assert_in_out_err(%w(-d), <<-INPUT, %w(false 2), %r".+") p Thread.abort_on_exception begin t = Thread.new { raise } @@ -492,7 +507,7 @@ class TestThread < Test::Unit::TestCase def test_ignore_deadlock if /mswin|mingw/ =~ RUBY_PLATFORM - skip "can't trap a signal from another process on Windows" + omit "can't trap a signal from another process on Windows" end assert_in_out_err([], <<-INPUT, %w(false :sig), [], :signal=>:INT, timeout: 1, timeout_error: nil) p Thread.ignore_deadlock @@ -723,7 +738,7 @@ class TestThread < Test::Unit::TestCase end def test_no_valid_cfp - skip 'with win32ole, cannot run this testcase because win32ole redefines Thread#initialize' if defined?(WIN32OLE) + omit 'with win32ole, cannot run this testcase because win32ole redefines Thread#initialize' if defined?(WIN32OLE) bug5083 = '[ruby-dev:44208]' assert_equal([], Thread.new(&Module.method(:nesting)).value, bug5083) assert_instance_of(Thread, Thread.new(:to_s, &Class.new.method(:undef_method)).join, bug5083) @@ -958,6 +973,8 @@ _eom end def test_thread_timer_and_interrupt + omit "[Bug #18613]" if /freebsd/ =~ RUBY_PLATFORM + bug5757 = '[ruby-dev:44985]' pid = nil cmd = 'Signal.trap(:INT, "DEFAULT"); pipe=IO.pipe; Thread.start {Thread.pass until Thread.main.stop?; puts; STDOUT.flush}; pipe[0].read' @@ -1060,7 +1077,7 @@ q.pop puts mth.status Process.kill(:INT, $$) } - sleep 0.1 + sleep INPUT end @@ -1236,6 +1253,21 @@ q.pop assert_predicate(status, :success?, bug9751) end if Process.respond_to?(:fork) + def test_fork_value + bug18902 = "[Bug #18902]" + th = Thread.start { sleep 2 } + begin + pid = fork do + th.value + end + _, status = Process.wait2(pid) + assert_predicate(status, :success?, bug18902) + ensure + th.kill + th.join + end + end if Process.respond_to?(:fork) + def test_fork_while_locked m = Thread::Mutex.new thrs = [] @@ -1248,7 +1280,7 @@ q.pop end if Process.respond_to?(:fork) def test_fork_while_parent_locked - skip 'needs fork' unless Process.respond_to?(:fork) + omit 'needs fork' unless Process.respond_to?(:fork) m = Thread::Mutex.new nr = 1 thrs = [] @@ -1269,7 +1301,7 @@ q.pop end def test_fork_while_mutex_locked_by_forker - skip 'needs fork' unless Process.respond_to?(:fork) + omit 'needs fork' unless Process.respond_to?(:fork) m = Thread::Mutex.new m.synchronize do pid = fork do @@ -1330,6 +1362,61 @@ q.pop t.join end + def test_yield_across_thread_through_enum + bug18649 = '[ruby-core:107980] [Bug #18649]' + @log = [] + + def self.p(arg) + @log << arg + end + + def self.synchronize + yield + end + + def self.execute(task) + success = true + value = reason = nil + end_sync = false + + synchronize do + begin + p :before + value = task.call + p :never_reached + success = true + rescue StandardError => ex + ex = ex.class + p [:rescue, ex] + reason = ex + success = false + end + + end_sync = true + p :end_sync + end + + p :should_not_reach_here! unless end_sync + [success, value, reason] + end + + def self.foo + Thread.new do + result = execute(-> { yield 42 }) + p [:result, result] + end.join + end + + value = to_enum(:foo).first + expected = [:before, + [:rescue, LocalJumpError], + :end_sync, + [:result, [false, nil, LocalJumpError]]] + + assert_equal(expected, @log, bug18649) + assert_equal(42, value, bug18649) + end + def test_thread_setname_in_initialize bug12290 = '[ruby-core:74963] [Bug #12290]' c = Class.new(Thread) {def initialize() self.name = "foo"; super; end} @@ -1337,7 +1424,7 @@ q.pop end def test_thread_native_thread_id - skip "don't support native_thread_id" unless Thread.method_defined?(:native_thread_id) + omit "don't support native_thread_id" unless Thread.method_defined?(:native_thread_id) assert_instance_of Integer, Thread.main.native_thread_id th1 = Thread.start{sleep} @@ -1348,7 +1435,8 @@ q.pop Thread.pass until th1.stop? # After a thread starts (and execute `sleep`), it returns native_thread_id - assert_instance_of Integer, th1.native_thread_id + native_tid = th1.native_thread_id + assert_instance_of Integer, native_tid if native_tid # it can be nil th1.wakeup Thread.pass while th1.alive? @@ -1357,11 +1445,40 @@ q.pop assert_nil th1.native_thread_id end + def test_thread_native_thread_id_across_fork_on_linux + rtld_default = Fiddle.dlopen(nil) + omit "this test is only for Linux" unless rtld_default.sym_defined?('gettid') + + gettid = Fiddle::Function.new(rtld_default['gettid'], [], Fiddle::TYPE_INT) + + parent_thread_id = Thread.main.native_thread_id + real_parent_thread_id = gettid.call + + assert_equal real_parent_thread_id, parent_thread_id + + child_lines = nil + IO.popen('-') do |pipe| + if pipe + # parent + child_lines = pipe.read.lines + else + # child + puts Thread.main.native_thread_id + puts gettid.call + end + end + child_thread_id = child_lines[0].chomp.to_i + real_child_thread_id = child_lines[1].chomp.to_i + + assert_equal real_child_thread_id, child_thread_id + refute_equal parent_thread_id, child_thread_id + end + def test_thread_interrupt_for_killed_thread opts = { timeout: 5, timeout_error: nil } - # prevent SIGABRT from slow shutdown with MJIT - opts[:reprieve] = 3 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? + # prevent SIGABRT from slow shutdown with RJIT + opts[:reprieve] = 3 if defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled? assert_normal_exit(<<-_end, '[Bug #8996]', **opts) Thread.report_on_exception = false @@ -1376,9 +1493,14 @@ q.pop def test_signal_at_join if /mswin|mingw/ =~ RUBY_PLATFORM - skip "can't trap a signal from another process on Windows" + omit "can't trap a signal from another process on Windows" # opt = {new_pgroup: true} end + + if /freebsd/ =~ RUBY_PLATFORM + omit "[Bug #18613]" + end + assert_separately([], "#{<<~"{#"}\n#{<<~'};'}", timeout: 120) {# n = 1000 @@ -1425,4 +1547,12 @@ q.pop end }; end + + def test_pending_interrupt? + t = Thread.handle_interrupt(Exception => :never) { Thread.new { Thread.stop } } + t.raise(StandardError) + assert_equal(true, t.pending_interrupt?) + assert_equal(true, t.pending_interrupt?(Exception)) + assert_equal(false, t.pending_interrupt?(ArgumentError)) + end end |