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
|
|
|
|
th.abort_on_exception = true
|
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
|
|
|
|
|
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_condvar
|
|
|
|
mutex = Mutex.new
|
|
|
|
condvar = ConditionVariable.new
|
|
|
|
result = []
|
|
|
|
mutex.synchronize do
|
|
|
|
t = Thread.new do
|
|
|
|
mutex.synchronize do
|
|
|
|
result << 1
|
|
|
|
condvar.signal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result << 0
|
|
|
|
condvar.wait(mutex)
|
|
|
|
result << 2
|
|
|
|
t.join
|
|
|
|
end
|
|
|
|
assert_equal([0, 1, 2], result)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_wait_not_owner
|
|
|
|
mutex = Mutex.new
|
|
|
|
condvar = ConditionVariable.new
|
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ThreadError) { condvar.wait(mutex) }
|
2008-04-08 23:12:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_wait_exception_handling
|
|
|
|
# Calling wait in the only thread running should raise a ThreadError of
|
|
|
|
# 'stopping only thread'
|
|
|
|
mutex = Mutex.new
|
|
|
|
condvar = ConditionVariable.new
|
|
|
|
|
|
|
|
locked = false
|
|
|
|
thread = Thread.new do
|
|
|
|
Thread.current.abort_on_exception = false
|
|
|
|
mutex.synchronize do
|
|
|
|
begin
|
|
|
|
condvar.wait(mutex)
|
|
|
|
rescue Exception
|
|
|
|
locked = mutex.locked?
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
until thread.stop?
|
|
|
|
sleep(0.1)
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.raise Interrupt, "interrupt a dead condition variable"
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Interrupt) { thread.value }
|
2008-04-08 23:12:03 -04:00
|
|
|
assert(locked)
|
|
|
|
end
|
|
|
|
|
|
|
|
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 {
|
2008-04-08 23:12:03 -04:00
|
|
|
result = `#{EnvUtil.rubybin} #{lbtest}`
|
|
|
|
assert(!$?.coredump?, '[ruby-dev:30653]')
|
|
|
|
assert_equal("exit.", result[/.*\Z/], '[ruby-dev:30653]')
|
|
|
|
}
|
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
def test_priority
|
|
|
|
c1 = c2 = 0
|
|
|
|
t1 = Thread.new { loop { c1 += 1 } }
|
|
|
|
t1.priority = -1
|
|
|
|
t2 = Thread.new { loop { c2 += 1 } }
|
|
|
|
t2.priority = -3
|
2008-04-24 10:02:15 -04:00
|
|
|
assert_equal(-1, t1.priority)
|
|
|
|
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
|
|
|
|
break if c1 > c2
|
|
|
|
sleep 0.1
|
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
t1.kill
|
|
|
|
t2.kill
|
2009-12-22 20:24:33 -05: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
|
|
|
|
|
|
|
|
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 { } }
|
|
|
|
t3 = 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
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-d), <<-INPUT, %w(false 2), /.+/)
|
|
|
|
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 }
|
|
|
|
d = Thread.new { sleep }
|
|
|
|
e = Thread.current
|
|
|
|
sleep 0.5
|
|
|
|
|
|
|
|
assert_equal(nil, a.status)
|
2008-08-25 09:46:34 -04:00
|
|
|
assert(a.stop?)
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal("sleep", b.status)
|
2008-08-25 09:46:34 -04:00
|
|
|
assert(b.stop?)
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal(false, c.status)
|
|
|
|
assert_match(/^#<TestThread::Thread:.* dead>$/, c.inspect)
|
|
|
|
assert(c.stop?)
|
2008-08-25 09:46:34 -04:00
|
|
|
|
|
|
|
d.kill
|
2009-10-15 23:10:31 -04:00
|
|
|
assert_equal(["aborting", false], [d.status, d.stop?])
|
2008-08-25 09:46:34 -04:00
|
|
|
|
2009-10-15 23:10:31 -04:00
|
|
|
assert_equal(["run", false], [e.status, e.stop?])
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
ensure
|
|
|
|
a.kill if a
|
|
|
|
b.kill if b
|
|
|
|
c.kill if c
|
|
|
|
d.kill if d
|
|
|
|
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}
|
|
|
|
assert(obj[:visited])
|
|
|
|
end
|
2007-08-27 12:48:14 -04:00
|
|
|
end
|
|
|
|
|
2008-01-18 13:48:12 -05:00
|
|
|
class TestThreadGroup < Test::Unit::TestCase
|
|
|
|
def test_thread_init
|
|
|
|
thgrp = ThreadGroup.new
|
|
|
|
Thread.new{
|
|
|
|
thgrp.add(Thread.current)
|
|
|
|
assert_equal(thgrp, Thread.new{sleep 1}.group)
|
|
|
|
}.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_frozen_thgroup
|
|
|
|
thgrp = ThreadGroup.new
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
t = Thread.new{1}
|
2008-01-18 13:48:12 -05:00
|
|
|
Thread.new{
|
|
|
|
thgrp.add(Thread.current)
|
|
|
|
thgrp.freeze
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
Thread.new{1}.join
|
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_raise(ThreadError) do
|
|
|
|
thgrp.add(t)
|
|
|
|
end
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
ThreadGroup.new.add Thread.current
|
|
|
|
end
|
2008-01-18 13:48:12 -05:00
|
|
|
}.join
|
2008-04-23 11:22:13 -04:00
|
|
|
t.join
|
2008-01-18 13:48:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_enclosed_thgroup
|
|
|
|
thgrp = ThreadGroup.new
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal(false, thgrp.enclosed?)
|
|
|
|
|
|
|
|
t = Thread.new{1}
|
2008-01-18 13:48:12 -05:00
|
|
|
Thread.new{
|
2008-04-23 11:22:13 -04:00
|
|
|
thgrp.add(Thread.current)
|
|
|
|
thgrp.enclose
|
|
|
|
assert_equal(true, thgrp.enclosed?)
|
2008-01-18 13:48:12 -05:00
|
|
|
assert_nothing_raised do
|
|
|
|
Thread.new{1}.join
|
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_raise(ThreadError) do
|
|
|
|
thgrp.add t
|
|
|
|
end
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
ThreadGroup.new.add Thread.current
|
|
|
|
end
|
2008-01-18 13:48:12 -05:00
|
|
|
}.join
|
2008-04-23 11:22:13 -04:00
|
|
|
t.join
|
2008-01-18 13:48:12 -05:00
|
|
|
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
|
2008-01-18 13:48:12 -05:00
|
|
|
end
|