2012-12-21 02:35:47 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2007-08-27 12:48:14 -04:00
|
|
|
require 'test/unit'
|
2008-04-08 23:12:03 -04:00
|
|
|
require 'thread'
|
2008-04-23 11:22:13 -04:00
|
|
|
require_relative 'envutil'
|
2007-08-27 12:48:14 -04:00
|
|
|
|
|
|
|
class TestThread < Test::Unit::TestCase
|
2008-04-08 23:12:03 -04:00
|
|
|
class Thread < ::Thread
|
2008-05-11 06:06:58 -04:00
|
|
|
Threads = []
|
2008-04-08 23:12:03 -04:00
|
|
|
def self.new(*)
|
|
|
|
th = super
|
2008-05-11 06:06:58 -04:00
|
|
|
Threads << th
|
2008-04-08 23:12:03 -04:00
|
|
|
th
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-11 06:06:58 -04:00
|
|
|
def setup
|
|
|
|
Thread::Threads.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
Thread::Threads.each do |t|
|
|
|
|
t.kill if t.alive?
|
|
|
|
begin
|
|
|
|
t.join
|
|
|
|
rescue Exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-29 13:22:36 -04:00
|
|
|
def test_main_thread_variable_in_enumerator
|
|
|
|
assert_equal Thread.main, Thread.current
|
|
|
|
|
|
|
|
Thread.current.thread_variable_set :foo, "bar"
|
|
|
|
|
|
|
|
thread, value = Fiber.new {
|
|
|
|
Fiber.yield [Thread.current, Thread.current.thread_variable_get(:foo)]
|
|
|
|
}.resume
|
|
|
|
|
|
|
|
assert_equal Thread.current, thread
|
|
|
|
assert_equal Thread.current.thread_variable_get(:foo), value
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variable_in_enumerator
|
|
|
|
Thread.new {
|
|
|
|
Thread.current.thread_variable_set :foo, "bar"
|
|
|
|
|
|
|
|
thread, value = Fiber.new {
|
|
|
|
Fiber.yield [Thread.current, Thread.current.thread_variable_get(:foo)]
|
|
|
|
}.resume
|
|
|
|
|
|
|
|
assert_equal Thread.current, thread
|
|
|
|
assert_equal Thread.current.thread_variable_get(:foo), value
|
|
|
|
}.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variables
|
|
|
|
assert_equal [], Thread.new { Thread.current.thread_variables }.join.value
|
|
|
|
|
|
|
|
t = Thread.new {
|
|
|
|
Thread.current.thread_variable_set(:foo, "bar")
|
|
|
|
Thread.current.thread_variables
|
|
|
|
}
|
|
|
|
assert_equal [:foo], t.join.value
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variable?
|
|
|
|
refute Thread.new { Thread.current.thread_variable?("foo") }.join.value
|
|
|
|
t = Thread.new {
|
|
|
|
Thread.current.thread_variable_set("foo", "bar")
|
|
|
|
}.join
|
|
|
|
|
|
|
|
assert t.thread_variable?("foo")
|
|
|
|
assert t.thread_variable?(:foo)
|
|
|
|
refute t.thread_variable?(:bar)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variable_strings_and_symbols_are_the_same_key
|
|
|
|
t = Thread.new {}.join
|
|
|
|
t.thread_variable_set("foo", "bar")
|
|
|
|
assert_equal "bar", t.thread_variable_get(:foo)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variable_frozen
|
|
|
|
t = Thread.new { }.join
|
|
|
|
t.freeze
|
|
|
|
assert_raises(RuntimeError) do
|
|
|
|
t.thread_variable_set(:foo, "bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variable_security
|
|
|
|
t = Thread.new { sleep }
|
|
|
|
|
|
|
|
assert_raises(SecurityError) do
|
|
|
|
Thread.new { $SAFE = 4; t.thread_variable_get(:foo) }.join
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises(SecurityError) do
|
|
|
|
Thread.new { $SAFE = 4; t.thread_variable_set(:foo, :baz) }.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-27 12:48:14 -04:00
|
|
|
def test_mutex_synchronize
|
|
|
|
m = Mutex.new
|
|
|
|
r = 0
|
2009-11-04 14:01:41 -05:00
|
|
|
max = 10
|
2007-08-27 12:48:14 -04:00
|
|
|
(1..max).map{
|
|
|
|
Thread.new{
|
|
|
|
i=0
|
|
|
|
while i<max*max
|
|
|
|
i+=1
|
|
|
|
m.synchronize{
|
|
|
|
r += 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}.each{|e|
|
|
|
|
e.join
|
|
|
|
}
|
|
|
|
assert_equal(max * max * max, r)
|
|
|
|
end
|
2008-04-08 23:12:03 -04:00
|
|
|
|
|
|
|
def test_local_barrier
|
|
|
|
dir = File.dirname(__FILE__)
|
|
|
|
lbtest = File.join(dir, "lbtest.rb")
|
|
|
|
$:.unshift File.join(File.dirname(dir), 'ruby')
|
|
|
|
require 'envutil'
|
|
|
|
$:.shift
|
2009-11-04 14:01:41 -05:00
|
|
|
3.times {
|
2012-07-22 19:11:05 -04:00
|
|
|
`#{EnvUtil.rubybin} #{lbtest}`
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_not_predicate($?, :coredump?, '[ruby-dev:30653]')
|
2008-04-08 23:12:03 -04:00
|
|
|
}
|
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
def test_priority
|
|
|
|
c1 = c2 = 0
|
|
|
|
t1 = Thread.new { loop { c1 += 1 } }
|
2011-11-23 19:46:34 -05:00
|
|
|
t1.priority = 3
|
2008-04-23 11:22:13 -04:00
|
|
|
t2 = Thread.new { loop { c2 += 1 } }
|
|
|
|
t2.priority = -3
|
2011-11-23 19:46:34 -05:00
|
|
|
assert_equal(3, t1.priority)
|
2008-04-24 10:02:15 -04:00
|
|
|
assert_equal(-3, t2.priority)
|
2008-04-23 11:22:13 -04:00
|
|
|
sleep 0.5
|
2009-11-04 14:01:41 -05:00
|
|
|
5.times do
|
2011-09-10 06:34:15 -04:00
|
|
|
break if c1 > c2
|
2009-11-04 14:01:41 -05:00
|
|
|
sleep 0.1
|
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
t1.kill
|
|
|
|
t2.kill
|
2011-09-10 06:34:15 -04:00
|
|
|
assert_operator(c1, :>, c2, "[ruby-dev:33124]") # not guaranteed
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_new
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
Thread.new
|
|
|
|
end
|
|
|
|
|
|
|
|
t1 = Thread.new { sleep }
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
t1.instance_eval { initialize { } }
|
|
|
|
end
|
|
|
|
|
|
|
|
t2 = Thread.new(&method(:sleep).to_proc)
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
t2.instance_eval { initialize { } }
|
|
|
|
end
|
|
|
|
|
|
|
|
ensure
|
|
|
|
t1.kill if t1
|
|
|
|
t2.kill if t2
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_join
|
|
|
|
t = Thread.new { sleep }
|
|
|
|
assert_nil(t.join(0.5))
|
|
|
|
|
|
|
|
ensure
|
|
|
|
t.kill if t
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_join2
|
|
|
|
t1 = Thread.new { sleep(1.5) }
|
|
|
|
t2 = Thread.new do
|
|
|
|
t1.join(1)
|
|
|
|
end
|
|
|
|
t3 = Thread.new do
|
|
|
|
sleep 0.5
|
|
|
|
t1.join
|
|
|
|
end
|
|
|
|
assert_nil(t2.value)
|
|
|
|
assert_equal(t1, t3.value)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
t1.kill if t1
|
|
|
|
t2.kill if t2
|
|
|
|
t3.kill if t3
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_kill_main_thread
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(1), [])
|
|
|
|
p 1
|
|
|
|
Thread.kill Thread.current
|
|
|
|
p 2
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
2011-06-09 09:38:27 -04:00
|
|
|
def test_kill_wrong_argument
|
|
|
|
bug4367 = '[ruby-core:35086]'
|
|
|
|
assert_raise(TypeError, bug4367) {
|
|
|
|
Thread.kill(nil)
|
|
|
|
}
|
2011-06-09 09:58:09 -04:00
|
|
|
o = Object.new
|
|
|
|
assert_raise(TypeError, bug4367) {
|
|
|
|
Thread.kill(o)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_kill_thread_subclass
|
|
|
|
c = Class.new(Thread)
|
|
|
|
t = c.new { sleep 10 }
|
|
|
|
assert_nothing_raised { Thread.kill(t) }
|
|
|
|
assert_equal(nil, t.value)
|
2011-06-09 09:38:27 -04:00
|
|
|
end
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
def test_exit
|
|
|
|
s = 0
|
|
|
|
Thread.new do
|
|
|
|
s += 1
|
|
|
|
Thread.exit
|
|
|
|
s += 2
|
|
|
|
end.join
|
|
|
|
assert_equal(1, s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wakeup
|
|
|
|
s = 0
|
|
|
|
t = Thread.new do
|
|
|
|
s += 1
|
|
|
|
Thread.stop
|
|
|
|
s += 1
|
|
|
|
end
|
|
|
|
sleep 0.5
|
|
|
|
assert_equal(1, s)
|
|
|
|
t.wakeup
|
|
|
|
sleep 0.5
|
|
|
|
assert_equal(2, s)
|
|
|
|
assert_raise(ThreadError) { t.wakeup }
|
|
|
|
|
|
|
|
ensure
|
|
|
|
t.kill if t
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stop
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(2), [])
|
|
|
|
begin
|
|
|
|
Thread.stop
|
|
|
|
p 1
|
|
|
|
rescue ThreadError
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_list
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT) do |r, e|
|
|
|
|
t1 = Thread.new { sleep }
|
2008-08-13 10:22:48 -04:00
|
|
|
Thread.pass
|
2008-07-15 11:26:04 -04:00
|
|
|
t2 = Thread.new { loop { } }
|
2010-06-23 01:32:46 -04:00
|
|
|
Thread.new { }.join
|
2010-02-19 02:01:11 -05:00
|
|
|
p [Thread.current, t1, t2].map{|t| t.object_id }.sort
|
|
|
|
p Thread.list.map{|t| t.object_id }.sort
|
2008-07-15 11:26:04 -04:00
|
|
|
INPUT
|
|
|
|
assert_equal(r.first, r.last)
|
|
|
|
assert_equal([], e)
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_main
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(true false), [])
|
|
|
|
p Thread.main == Thread.current
|
|
|
|
Thread.new { p Thread.main == Thread.current }.join
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_abort_on_exception
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(false 1), [])
|
|
|
|
p Thread.abort_on_exception
|
|
|
|
begin
|
|
|
|
Thread.new { raise }
|
|
|
|
sleep 0.5
|
|
|
|
p 1
|
|
|
|
rescue
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(true 2), [])
|
|
|
|
Thread.abort_on_exception = true
|
|
|
|
p Thread.abort_on_exception
|
|
|
|
begin
|
|
|
|
Thread.new { raise }
|
|
|
|
sleep 0.5
|
|
|
|
p 1
|
|
|
|
rescue
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
|
2011-02-11 05:45:34 -05:00
|
|
|
assert_in_out_err(%w(--disable-gems -d), <<-INPUT, %w(false 2), %r".+")
|
2008-07-15 11:26:04 -04:00
|
|
|
p Thread.abort_on_exception
|
|
|
|
begin
|
|
|
|
Thread.new { raise }
|
|
|
|
sleep 0.5
|
|
|
|
p 1
|
|
|
|
rescue
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(false true 2), [])
|
|
|
|
p Thread.abort_on_exception
|
|
|
|
begin
|
|
|
|
t = Thread.new { sleep 0.5; raise }
|
|
|
|
t.abort_on_exception = true
|
|
|
|
p t.abort_on_exception
|
|
|
|
sleep 1
|
|
|
|
p 1
|
|
|
|
rescue
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_status_and_stop_p
|
|
|
|
a = ::Thread.new { raise("die now") }
|
|
|
|
b = Thread.new { Thread.stop }
|
|
|
|
c = Thread.new { Thread.exit }
|
|
|
|
e = Thread.current
|
|
|
|
sleep 0.5
|
|
|
|
|
|
|
|
assert_equal(nil, a.status)
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_predicate(a, :stop?)
|
2008-08-25 09:46:34 -04:00
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal("sleep", b.status)
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_predicate(b, :stop?)
|
2008-08-25 09:46:34 -04:00
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal(false, c.status)
|
|
|
|
assert_match(/^#<TestThread::Thread:.* dead>$/, c.inspect)
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_predicate(c, :stop?)
|
2008-08-25 09:46:34 -04:00
|
|
|
|
2012-04-21 21:46:45 -04:00
|
|
|
es1 = e.status
|
|
|
|
es2 = e.stop?
|
|
|
|
assert_equal(["run", false], [es1, es2])
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
ensure
|
|
|
|
a.kill if a
|
|
|
|
b.kill if b
|
|
|
|
c.kill if c
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_level
|
|
|
|
t = Thread.new { $SAFE = 3; sleep }
|
|
|
|
sleep 0.5
|
|
|
|
assert_equal(0, Thread.current.safe_level)
|
|
|
|
assert_equal(3, t.safe_level)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
t.kill if t
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_local
|
|
|
|
t = Thread.new { sleep }
|
|
|
|
|
|
|
|
assert_equal(false, t.key?(:foo))
|
|
|
|
|
|
|
|
t["foo"] = "foo"
|
|
|
|
t["bar"] = "bar"
|
|
|
|
t["baz"] = "baz"
|
|
|
|
|
|
|
|
assert_equal(true, t.key?(:foo))
|
|
|
|
assert_equal(true, t.key?("foo"))
|
|
|
|
assert_equal(false, t.key?(:qux))
|
|
|
|
assert_equal(false, t.key?("qux"))
|
|
|
|
|
|
|
|
assert_equal([:foo, :bar, :baz], t.keys)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
t.kill if t
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_local_security
|
|
|
|
t = Thread.new { sleep }
|
|
|
|
|
|
|
|
assert_raise(SecurityError) do
|
|
|
|
Thread.new { $SAFE = 4; t[:foo] }.join
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(SecurityError) do
|
|
|
|
Thread.new { $SAFE = 4; t[:foo] = :baz }.join
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
Thread.new do
|
|
|
|
Thread.current[:foo] = :bar
|
|
|
|
Thread.current.freeze
|
|
|
|
Thread.current[:foo] = :baz
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_select_wait
|
|
|
|
assert_nil(IO.select(nil, nil, nil, 1))
|
|
|
|
t = Thread.new do
|
2008-04-24 10:02:15 -04:00
|
|
|
IO.select(nil, nil, nil, nil)
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
sleep 0.5
|
|
|
|
t.kill
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mutex_deadlock
|
|
|
|
m = Mutex.new
|
|
|
|
m.synchronize do
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
m.synchronize do
|
|
|
|
assert(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mutex_interrupt
|
|
|
|
m = Mutex.new
|
|
|
|
m.lock
|
|
|
|
t = Thread.new do
|
|
|
|
m.lock
|
|
|
|
:foo
|
|
|
|
end
|
|
|
|
sleep 0.5
|
|
|
|
t.kill
|
|
|
|
assert_nil(t.value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mutex_illegal_unlock
|
|
|
|
m = Mutex.new
|
|
|
|
m.lock
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
Thread.new do
|
|
|
|
m.unlock
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-12 11:42:14 -04:00
|
|
|
def test_mutex_fifo_like_lock
|
|
|
|
m1 = Mutex.new
|
|
|
|
m2 = Mutex.new
|
|
|
|
m1.lock
|
|
|
|
m2.lock
|
|
|
|
m1.unlock
|
|
|
|
m2.unlock
|
|
|
|
assert_equal(false, m1.locked?)
|
|
|
|
assert_equal(false, m2.locked?)
|
|
|
|
|
|
|
|
m3 = Mutex.new
|
|
|
|
m1.lock
|
|
|
|
m2.lock
|
|
|
|
m3.lock
|
|
|
|
m1.unlock
|
|
|
|
m2.unlock
|
|
|
|
m3.unlock
|
|
|
|
assert_equal(false, m1.locked?)
|
|
|
|
assert_equal(false, m2.locked?)
|
|
|
|
assert_equal(false, m3.locked?)
|
|
|
|
end
|
|
|
|
|
2008-12-28 22:03:09 -05:00
|
|
|
def test_mutex_trylock
|
|
|
|
m = Mutex.new
|
|
|
|
assert_equal(true, m.try_lock)
|
|
|
|
assert_equal(false, m.try_lock, '[ruby-core:20943]')
|
|
|
|
|
|
|
|
Thread.new{
|
|
|
|
assert_equal(false, m.try_lock)
|
|
|
|
}.join
|
|
|
|
|
|
|
|
m.unlock
|
|
|
|
end
|
|
|
|
|
2009-09-15 17:30:50 -04:00
|
|
|
def test_recursive_outer
|
|
|
|
arr = []
|
|
|
|
obj = Struct.new(:foo, :visited).new(arr, false)
|
|
|
|
arr << obj
|
|
|
|
def obj.hash
|
|
|
|
self[:visited] = true
|
|
|
|
super
|
|
|
|
raise "recursive_outer should short circuit intermediate calls"
|
|
|
|
end
|
|
|
|
assert_nothing_raised {arr.hash}
|
2012-12-21 02:35:49 -05:00
|
|
|
assert(obj[:visited], "obj.hash was not called")
|
2009-09-15 17:30:50 -04:00
|
|
|
end
|
2011-02-11 05:45:34 -05:00
|
|
|
|
|
|
|
def test_thread_instance_variable
|
|
|
|
bug4389 = '[ruby-core:35192]'
|
|
|
|
assert_in_out_err([], <<-INPUT, %w(), [], bug4389)
|
|
|
|
class << Thread.current
|
|
|
|
@data = :data
|
|
|
|
end
|
|
|
|
INPUT
|
|
|
|
end
|
2011-07-30 22:32:48 -04:00
|
|
|
|
|
|
|
def test_no_valid_cfp
|
2011-09-01 01:09:29 -04:00
|
|
|
skip 'with win32ole, cannot run this testcase because win32ole redefines Thread#intialize' if defined?(WIN32OLE)
|
2011-07-30 22:32:48 -04:00
|
|
|
bug5083 = '[ruby-dev:44208]'
|
2012-08-02 07:34:19 -04:00
|
|
|
assert_equal([], Thread.new(&Module.method(:nesting)).value)
|
2011-07-30 22:32:48 -04:00
|
|
|
error = assert_raise(RuntimeError) do
|
|
|
|
Thread.new(:to_s, &Module.method(:undef_method)).join
|
|
|
|
end
|
|
|
|
assert_equal("Can't call on top of Fiber or Thread", error.message, bug5083)
|
|
|
|
end
|
2012-07-19 10:19:40 -04:00
|
|
|
|
2012-11-30 04:28:35 -05:00
|
|
|
def make_async_interrupt_timing_test_thread1 flag
|
2012-07-19 10:19:40 -04:00
|
|
|
r = []
|
2012-07-23 15:04:15 -04:00
|
|
|
ready_p = false
|
2012-07-19 10:19:40 -04:00
|
|
|
th = Thread.new{
|
|
|
|
begin
|
2012-11-30 04:28:35 -05:00
|
|
|
Thread.async_interrupt_timing(RuntimeError => flag){
|
2012-07-19 10:19:40 -04:00
|
|
|
begin
|
2012-07-23 15:04:15 -04:00
|
|
|
ready_p = true
|
2012-07-19 10:19:40 -04:00
|
|
|
sleep 0.5
|
|
|
|
rescue
|
|
|
|
r << :c1
|
|
|
|
end
|
|
|
|
}
|
|
|
|
rescue
|
|
|
|
r << :c2
|
|
|
|
end
|
|
|
|
}
|
2012-07-23 15:04:15 -04:00
|
|
|
Thread.pass until ready_p
|
2012-07-19 10:19:40 -04:00
|
|
|
th.raise
|
|
|
|
begin
|
|
|
|
th.join
|
|
|
|
rescue
|
|
|
|
r << :c3
|
|
|
|
end
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
2012-11-30 04:28:35 -05:00
|
|
|
def test_async_interrupt_timing
|
|
|
|
[[:defer, :c2],
|
2012-07-19 10:19:40 -04:00
|
|
|
[:immediate, :c1],
|
|
|
|
[:on_blocking, :c1]].each{|(flag, c)|
|
2012-11-30 04:28:35 -05:00
|
|
|
assert_equal([flag, c], [flag] + make_async_interrupt_timing_test_thread1(flag))
|
2012-07-19 10:19:40 -04:00
|
|
|
}
|
|
|
|
# TODO: complex cases are needed.
|
|
|
|
end
|
|
|
|
|
2012-11-30 12:39:48 -05:00
|
|
|
def test_async_interrupt_timing_invalid_argument
|
|
|
|
assert_raise(ArgumentError) {
|
|
|
|
Thread.async_interrupt_timing(RuntimeError => :immediate) # no block
|
|
|
|
}
|
|
|
|
assert_raise(ArgumentError) {
|
|
|
|
Thread.async_interrupt_timing(RuntimeError => :never) {} # never?
|
|
|
|
}
|
|
|
|
assert_raise(TypeError) {
|
|
|
|
Thread.async_interrupt_timing([]) {} # array
|
|
|
|
}
|
|
|
|
end
|
2012-12-05 04:54:58 -05:00
|
|
|
|
|
|
|
def for_test_async_interrupt_with_return
|
|
|
|
Thread.async_interrupt_timing(Object => :defer){
|
|
|
|
Thread.current.raise RuntimeError.new("have to be rescured")
|
2012-12-07 00:02:18 -05:00
|
|
|
return
|
2012-12-05 04:54:58 -05:00
|
|
|
}
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_async_interrupt_with_return
|
|
|
|
assert_nothing_raised do
|
|
|
|
for_test_async_interrupt_with_return
|
2012-12-05 10:12:58 -05:00
|
|
|
_dummy_for_check_ints=nil
|
2012-12-05 04:54:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_async_interrupt_with_break
|
|
|
|
assert_nothing_raised do
|
|
|
|
begin
|
|
|
|
Thread.async_interrupt_timing(Object => :defer){
|
|
|
|
Thread.current.raise RuntimeError.new("have to be rescured")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rescue
|
|
|
|
end
|
2012-12-05 10:12:58 -05:00
|
|
|
_dummy_for_check_ints=nil
|
2012-12-05 04:54:58 -05:00
|
|
|
end
|
|
|
|
end
|
2012-12-05 08:35:05 -05:00
|
|
|
|
2012-12-04 13:38:21 -05:00
|
|
|
def test_async_interrupt_blocking
|
2012-12-04 14:06:46 -05:00
|
|
|
r=:ng
|
2012-12-04 13:38:21 -05:00
|
|
|
e=Class.new(Exception)
|
|
|
|
th_s = Thread.current
|
|
|
|
begin
|
|
|
|
th = Thread.start{
|
|
|
|
Thread.async_interrupt_timing(Object => :on_blocking){
|
|
|
|
begin
|
|
|
|
Thread.current.raise RuntimeError
|
2012-12-04 14:06:46 -05:00
|
|
|
r=:ok
|
2012-12-04 13:38:21 -05:00
|
|
|
sleep
|
|
|
|
ensure
|
|
|
|
th_s.raise e
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sleep 1
|
|
|
|
r=:ng
|
|
|
|
th.raise RuntimeError
|
|
|
|
th.join
|
|
|
|
rescue e
|
|
|
|
end
|
|
|
|
assert_equal(:ok,r)
|
|
|
|
end
|
2012-11-30 12:39:48 -05:00
|
|
|
|
2012-12-05 14:37:37 -05:00
|
|
|
def test_async_interrupt_and_io
|
|
|
|
assert_in_out_err([], <<-INPUT, %w(ok), [])
|
|
|
|
th_waiting = true
|
|
|
|
|
|
|
|
t = Thread.new {
|
|
|
|
Thread.async_interrupt_timing(RuntimeError => :on_blocking) {
|
|
|
|
nil while th_waiting
|
|
|
|
# async interrupt should be raised _before_ writing puts arguments
|
|
|
|
puts "ng"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep 0.1
|
|
|
|
t.raise RuntimeError
|
|
|
|
th_waiting = false
|
|
|
|
t.join rescue nil
|
|
|
|
puts "ok"
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
2012-12-05 14:37:49 -05:00
|
|
|
def test_async_interrupt_and_p
|
|
|
|
assert_in_out_err([], <<-INPUT, %w(:ok :ok), [])
|
|
|
|
th_waiting = true
|
|
|
|
|
|
|
|
t = Thread.new {
|
|
|
|
Thread.async_interrupt_timing(RuntimeError => :on_blocking) {
|
|
|
|
nil while th_waiting
|
|
|
|
# p shouldn't provide interruptible point
|
|
|
|
p :ok
|
|
|
|
p :ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep 0.1
|
|
|
|
t.raise RuntimeError
|
|
|
|
th_waiting = false
|
|
|
|
t.join rescue nil
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
2012-11-30 04:28:35 -05:00
|
|
|
def test_async_interrupted?
|
2012-07-19 10:19:40 -04:00
|
|
|
q = Queue.new
|
2012-11-30 04:28:35 -05:00
|
|
|
Thread.async_interrupt_timing(RuntimeError => :defer){
|
2012-07-19 10:19:40 -04:00
|
|
|
th = Thread.new{
|
|
|
|
q.push :e
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
sleep 0.5
|
|
|
|
rescue => e
|
|
|
|
q.push :ng1
|
|
|
|
end
|
|
|
|
begin
|
2012-11-30 04:28:35 -05:00
|
|
|
Thread.async_interrupt_timing(Object => :immediate){} if Thread.async_interrupted?
|
2012-07-19 10:19:40 -04:00
|
|
|
rescue => e
|
|
|
|
q.push :ok
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
q.push :ng2
|
|
|
|
ensure
|
|
|
|
q.push :ng3
|
|
|
|
end
|
|
|
|
}
|
|
|
|
q.pop
|
|
|
|
th.raise
|
|
|
|
th.join
|
|
|
|
assert_equal(:ok, q.pop)
|
|
|
|
}
|
|
|
|
end
|
2012-07-22 12:48:49 -04:00
|
|
|
|
|
|
|
def test_thread_timer_and_ensure
|
|
|
|
assert_normal_exit(<<_eom, 'r36492', timeout: 3)
|
|
|
|
flag = false
|
|
|
|
t = Thread.new do
|
|
|
|
begin
|
|
|
|
sleep
|
|
|
|
ensure
|
|
|
|
1 until flag
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Thread.pass until t.status == "sleep"
|
|
|
|
|
|
|
|
t.kill
|
|
|
|
t.alive? == true
|
|
|
|
flag = true
|
|
|
|
t.join
|
|
|
|
_eom
|
|
|
|
end
|
2008-06-02 08:57:18 -04:00
|
|
|
|
|
|
|
def test_uninitialized
|
|
|
|
c = Class.new(Thread)
|
|
|
|
c.class_eval { def initialize; end }
|
|
|
|
assert_raise(ThreadError) { c.new.start }
|
|
|
|
end
|
2009-06-14 01:59:23 -04:00
|
|
|
|
|
|
|
def test_backtrace
|
|
|
|
Thread.new{
|
|
|
|
assert_equal(Array, Thread.main.backtrace.class)
|
|
|
|
}.join
|
|
|
|
|
|
|
|
t = Thread.new{}
|
|
|
|
t.join
|
|
|
|
assert_equal(nil, t.backtrace)
|
|
|
|
end
|
2011-12-13 21:26:20 -05:00
|
|
|
|
|
|
|
def test_thread_timer_and_interrupt
|
|
|
|
bug5757 = '[ruby-dev:44985]'
|
|
|
|
t0 = Time.now.to_f
|
2012-01-31 00:29:52 -05:00
|
|
|
pid = nil
|
|
|
|
cmd = 'r,=IO.pipe; Thread.start {Thread.pass until Thread.main.stop?; puts; STDOUT.flush}; r.read'
|
2012-04-07 10:10:30 -04:00
|
|
|
opt = {}
|
|
|
|
opt[:new_pgroup] = true if /mswin|mingw/ =~ RUBY_PLATFORM
|
2012-07-22 19:11:05 -04:00
|
|
|
s, _err = EnvUtil.invoke_ruby(['-e', cmd], "", true, true, opt) do |in_p, out_p, err_p, cpid|
|
2012-01-31 00:29:52 -05:00
|
|
|
out_p.gets
|
|
|
|
pid = cpid
|
|
|
|
Process.kill(:SIGINT, pid)
|
|
|
|
Process.wait(pid)
|
|
|
|
[$?, err_p.read]
|
2012-01-27 19:51:40 -05:00
|
|
|
end
|
2011-12-13 21:26:20 -05:00
|
|
|
t1 = Time.now.to_f
|
2012-07-22 19:11:05 -04:00
|
|
|
assert_equal(pid, s.pid, bug5757)
|
2012-01-31 00:29:52 -05:00
|
|
|
unless /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
# status of signal is not supported on Windows
|
|
|
|
assert_equal([false, true, false, Signal.list["INT"]],
|
|
|
|
[s.exited?, s.signaled?, s.stopped?, s.termsig],
|
|
|
|
"[s.exited?, s.signaled?, s.stopped?, s.termsig]")
|
|
|
|
end
|
2012-07-22 19:11:05 -04:00
|
|
|
assert_in_delta(t1 - t0, 1, 1, bug5757)
|
2011-12-13 21:26:20 -05:00
|
|
|
end
|
2012-11-26 03:05:49 -05:00
|
|
|
|
|
|
|
def test_thread_join_in_trap
|
2012-11-28 08:24:36 -05:00
|
|
|
assert_nothing_raised{
|
2012-11-26 03:05:49 -05:00
|
|
|
t = Thread.new{ sleep 0.2; Process.kill(:INT, $$) }
|
|
|
|
|
|
|
|
Signal.trap :INT do
|
|
|
|
t.join
|
|
|
|
end
|
|
|
|
|
|
|
|
t.join
|
|
|
|
}
|
2012-11-27 23:09:38 -05:00
|
|
|
|
2012-11-28 08:24:36 -05:00
|
|
|
assert_equal(:normal_end,
|
|
|
|
begin
|
|
|
|
t = Thread.new{ sleep 0.2; Process.kill(:INT, $$); :normal_end }
|
2012-11-28 21:58:31 -05:00
|
|
|
|
2012-11-28 08:24:36 -05:00
|
|
|
Signal.trap :INT do
|
|
|
|
t.value
|
|
|
|
end
|
|
|
|
t.value
|
|
|
|
end
|
|
|
|
)
|
2012-11-26 03:05:49 -05:00
|
|
|
end
|
|
|
|
|
2012-11-26 21:00:09 -05:00
|
|
|
def test_thread_join_current
|
|
|
|
assert_raises(ThreadError) do
|
|
|
|
Thread.current.join
|
|
|
|
end
|
|
|
|
end
|
2012-11-26 21:00:19 -05:00
|
|
|
|
|
|
|
def test_thread_join_main_thread
|
|
|
|
assert_raises(ThreadError) do
|
|
|
|
Thread.new(Thread.current) {|t|
|
|
|
|
t.join
|
|
|
|
}.join
|
|
|
|
end
|
|
|
|
end
|
2012-11-26 21:00:40 -05:00
|
|
|
|
|
|
|
def test_main_thread_status_at_exit
|
|
|
|
assert_in_out_err([], <<-INPUT, %w(false), [])
|
|
|
|
Thread.new(Thread.current) {|mth|
|
|
|
|
begin
|
|
|
|
sleep 0.1
|
|
|
|
ensure
|
|
|
|
p mth.alive?
|
|
|
|
end
|
|
|
|
}
|
|
|
|
INPUT
|
|
|
|
end
|
2012-11-28 03:31:03 -05:00
|
|
|
|
|
|
|
def test_thread_status_in_trap
|
|
|
|
# when running trap handler, Thread#status must show "run"
|
|
|
|
# Even though interrupted from sleeping function
|
|
|
|
assert_in_out_err([], <<-INPUT, %w(sleep run), [])
|
|
|
|
Signal.trap(:INT) {
|
|
|
|
puts Thread.current.status
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.new(Thread.current) {|mth|
|
|
|
|
sleep 0.01
|
|
|
|
puts mth.status
|
|
|
|
Process.kill(:INT, $$)
|
|
|
|
}
|
|
|
|
sleep 0.1
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
|
|
|
# Bug #7450
|
|
|
|
def test_thread_status_raise_after_kill
|
|
|
|
ary = []
|
|
|
|
|
|
|
|
t = Thread.new {
|
|
|
|
begin
|
|
|
|
ary << Thread.current.status
|
* test/ruby/test_thread.rb (test_uninitialized, test_backtrace,
test_thread_timer_and_interrupt, test_thread_join_in_trap,
test_thread_join_current, test_thread_join_main_thread,
test_main_thread_status_at_exit, test_thread_status_in_trap,
test_thread_status_raise_after_kill, test_mutex_owned,
test_mutex_owned2): move these tests from TestThreadGroup class
to TestThread becuase they are not thread group tests.
* test/ruby/test_thread.rb (test_thread_status_raise_after_kill):
add t.join.
* test/ruby/test_threadgroup.rb: new file. moved ThreadGroup test
form test_thread.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38388 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-14 18:11:36 -05:00
|
|
|
sleep #1
|
2012-11-28 03:31:03 -05:00
|
|
|
ensure
|
|
|
|
begin
|
|
|
|
ary << Thread.current.status
|
* test/ruby/test_thread.rb (test_uninitialized, test_backtrace,
test_thread_timer_and_interrupt, test_thread_join_in_trap,
test_thread_join_current, test_thread_join_main_thread,
test_main_thread_status_at_exit, test_thread_status_in_trap,
test_thread_status_raise_after_kill, test_mutex_owned,
test_mutex_owned2): move these tests from TestThreadGroup class
to TestThread becuase they are not thread group tests.
* test/ruby/test_thread.rb (test_thread_status_raise_after_kill):
add t.join.
* test/ruby/test_threadgroup.rb: new file. moved ThreadGroup test
form test_thread.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38388 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-14 18:11:36 -05:00
|
|
|
sleep #2
|
2012-11-28 03:31:03 -05:00
|
|
|
ensure
|
|
|
|
ary << Thread.current.status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
* test/ruby/test_thread.rb (test_uninitialized, test_backtrace,
test_thread_timer_and_interrupt, test_thread_join_in_trap,
test_thread_join_current, test_thread_join_main_thread,
test_main_thread_status_at_exit, test_thread_status_in_trap,
test_thread_status_raise_after_kill, test_mutex_owned,
test_mutex_owned2): move these tests from TestThreadGroup class
to TestThread becuase they are not thread group tests.
* test/ruby/test_thread.rb (test_thread_status_raise_after_kill):
add t.join.
* test/ruby/test_threadgroup.rb: new file. moved ThreadGroup test
form test_thread.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38388 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-14 18:11:36 -05:00
|
|
|
begin
|
|
|
|
sleep 0.01
|
|
|
|
t.kill # wake up sleep #1
|
|
|
|
sleep 0.01
|
|
|
|
t.raise "wakeup" # wake up sleep #2
|
|
|
|
sleep 0.01
|
|
|
|
assert_equal(ary, ["run", "aborting", "aborting"])
|
|
|
|
ensure
|
|
|
|
t.join rescue nil
|
|
|
|
end
|
2012-11-28 03:31:03 -05:00
|
|
|
end
|
2012-12-04 11:02:07 -05:00
|
|
|
|
|
|
|
def test_mutex_owned
|
|
|
|
mutex = Mutex.new
|
|
|
|
|
|
|
|
assert_equal(mutex.owned?, false)
|
|
|
|
mutex.synchronize {
|
|
|
|
# Now, I have the mutex
|
|
|
|
assert_equal(mutex.owned?, true)
|
|
|
|
}
|
|
|
|
assert_equal(mutex.owned?, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mutex_owned2
|
|
|
|
begin
|
|
|
|
mutex = Mutex.new
|
|
|
|
th = Thread.new {
|
|
|
|
# lock forever
|
|
|
|
mutex.lock
|
|
|
|
sleep
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep 0.01 until th.status == "sleep"
|
|
|
|
# acquired another thread.
|
|
|
|
assert_equal(mutex.locked?, true)
|
|
|
|
assert_equal(mutex.owned?, false)
|
|
|
|
ensure
|
|
|
|
th.kill if th
|
|
|
|
end
|
|
|
|
end
|
2012-12-19 17:29:18 -05:00
|
|
|
|
|
|
|
def invoke_rec script, vm_stack_size, machine_stack_size, use_length = true
|
|
|
|
env = {}
|
|
|
|
env['RUBY_THREAD_VM_STACK_SIZE'] = vm_stack_size.to_s if vm_stack_size
|
|
|
|
env['RUBY_THREAD_MACHINE_STACK_SIZE'] = machine_stack_size.to_s if machine_stack_size
|
|
|
|
out, = EnvUtil.invoke_ruby([env, '-e', script], '', true, true)
|
|
|
|
use_length ? out.length : out
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stack_size
|
|
|
|
h_default = eval(invoke_rec('p RubyVM::DEFAULT_PARAMS', nil, nil, false))
|
|
|
|
h_0 = eval(invoke_rec('p RubyVM::DEFAULT_PARAMS', 0, 0, false))
|
|
|
|
h_large = eval(invoke_rec('p RubyVM::DEFAULT_PARAMS', 1024 * 1024 * 10, 1024 * 1024 * 10, false))
|
|
|
|
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(h_default[:thread_vm_stack_size], :>, h_0[:thread_vm_stack_size],
|
|
|
|
"0 thread_vm_stack_size")
|
|
|
|
assert_operator(h_default[:thread_vm_stack_size], :<, h_large[:thread_vm_stack_size],
|
|
|
|
"large thread_vm_stack_size")
|
|
|
|
assert_operator(h_default[:thread_machine_stack_size], :>=, h_0[:thread_machine_stack_size],
|
|
|
|
"0 thread_machine_stack_size")
|
|
|
|
assert_operator(h_default[:thread_machine_stack_size], :<=, h_large[:thread_machine_stack_size],
|
|
|
|
"large thread_machine_stack_size")
|
2012-12-19 17:29:18 -05:00
|
|
|
|
|
|
|
# check VM machine stack size
|
2012-12-21 02:35:47 -05:00
|
|
|
script = 'def rec; print "."; STDOUT.flush; rec; end; rec'
|
2012-12-19 17:29:18 -05:00
|
|
|
size_default = invoke_rec script, nil, nil
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :>, 0, "default size")
|
2012-12-19 17:29:18 -05:00
|
|
|
size_0 = invoke_rec script, 0, nil
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :>, size_0, "0 size")
|
2012-12-19 17:29:18 -05:00
|
|
|
size_large = invoke_rec script, 1024 * 1024 * 10, nil
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :<, size_large, "large size")
|
2012-12-19 17:29:18 -05:00
|
|
|
|
|
|
|
return if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
|
|
|
|
# check machine stack size
|
|
|
|
# Note that machine stack size may not change size (depend on OSs)
|
2012-12-21 02:35:47 -05:00
|
|
|
script = 'def rec; print "."; STDOUT.flush; 1.times{1.times{1.times{rec}}}; end; Thread.new{rec}.join'
|
2012-12-19 17:29:18 -05:00
|
|
|
vm_stack_size = 1024 * 1024
|
|
|
|
size_default = invoke_rec script, vm_stack_size, nil
|
|
|
|
size_0 = invoke_rec script, vm_stack_size, 0
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :>=, size_0, "0 size")
|
2012-12-19 17:29:18 -05:00
|
|
|
size_large = invoke_rec script, vm_stack_size, 1024 * 1024 * 10
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :<=, size_large, "large size")
|
2012-12-19 17:29:18 -05:00
|
|
|
end
|
2008-01-18 13:48:12 -05:00
|
|
|
end
|