2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2012-12-01 03:29:09 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'tmpdir'
|
|
|
|
|
2018-07-30 23:49:38 -04:00
|
|
|
class TestThreadConditionVariable < Test::Unit::TestCase
|
2016-08-30 02:22:30 -04:00
|
|
|
ConditionVariable = Thread::ConditionVariable
|
|
|
|
Mutex = Thread::Mutex
|
|
|
|
|
2014-08-18 21:13:24 -04:00
|
|
|
def test_initialized
|
|
|
|
assert_raise(TypeError) {
|
|
|
|
ConditionVariable.allocate.wait(nil)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-12-01 03:29:09 -05:00
|
|
|
def test_condvar_signal_and_wait
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
result = []
|
2020-02-05 19:14:40 -05:00
|
|
|
woken = nil
|
2012-12-01 03:29:09 -05:00
|
|
|
mutex.synchronize do
|
|
|
|
t = Thread.new do
|
|
|
|
mutex.synchronize do
|
|
|
|
result << 1
|
|
|
|
condvar.signal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result << 0
|
2020-02-05 19:14:40 -05:00
|
|
|
woken = condvar.wait(mutex)
|
2012-12-01 03:29:09 -05:00
|
|
|
result << 2
|
|
|
|
t.join
|
|
|
|
end
|
|
|
|
assert_equal([0, 1, 2], result)
|
2020-02-05 19:14:40 -05:00
|
|
|
assert(woken)
|
2012-12-01 03:29:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_wait_exception_handling
|
|
|
|
# Calling wait in the only thread running should raise a ThreadError of
|
|
|
|
# 'stopping only thread'
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
|
|
|
|
locked = false
|
|
|
|
thread = Thread.new do
|
|
|
|
Thread.current.abort_on_exception = false
|
|
|
|
mutex.synchronize do
|
2017-12-12 13:44:49 -05:00
|
|
|
assert_raise(Interrupt) {
|
2012-12-01 03:29:09 -05:00
|
|
|
condvar.wait(mutex)
|
2017-12-12 13:44:49 -05:00
|
|
|
}
|
|
|
|
locked = mutex.locked?
|
2012-12-01 03:29:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
until thread.stop?
|
|
|
|
sleep(0.1)
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.raise Interrupt, "interrupt a dead condition variable"
|
2017-12-12 13:44:49 -05:00
|
|
|
thread.join
|
2012-12-01 03:29:09 -05:00
|
|
|
assert(locked)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_wait_and_broadcast
|
|
|
|
nr_threads = 3
|
|
|
|
threads = Array.new
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
result = []
|
|
|
|
|
|
|
|
nr_threads.times do |i|
|
|
|
|
threads[i] = Thread.new do
|
|
|
|
mutex.synchronize do
|
|
|
|
result << "C1"
|
|
|
|
condvar.wait mutex
|
|
|
|
result << "C2"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
sleep 0.1
|
|
|
|
mutex.synchronize do
|
|
|
|
result << "P1"
|
|
|
|
condvar.broadcast
|
|
|
|
result << "P2"
|
|
|
|
end
|
2014-06-30 23:16:33 -04:00
|
|
|
Timeout.timeout(5) do
|
|
|
|
nr_threads.times do |i|
|
|
|
|
threads[i].join
|
|
|
|
end
|
2012-12-01 03:29:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal ["C1", "C1", "C1", "P1", "P2", "C2", "C2", "C2"], result
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_wait_deadlock
|
2016-06-13 08:46:44 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, /\Afatal\nNo live threads left\. Deadlock/, [])
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
cv = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
|
2013-09-07 00:58:38 -04:00
|
|
|
klass = nil
|
2012-12-01 03:29:09 -05:00
|
|
|
mesg = nil
|
|
|
|
begin
|
|
|
|
mutex.lock
|
|
|
|
cv.wait mutex
|
|
|
|
mutex.unlock
|
|
|
|
rescue Exception => e
|
2013-09-07 00:58:38 -04:00
|
|
|
klass = e.class
|
2012-12-01 03:29:09 -05:00
|
|
|
mesg = e.message
|
|
|
|
end
|
2013-09-07 00:58:38 -04:00
|
|
|
puts klass
|
2012-12-01 03:29:09 -05:00
|
|
|
print mesg
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_wait_deadlock_2
|
|
|
|
nr_threads = 3
|
|
|
|
threads = Array.new
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
|
|
|
|
nr_threads.times do |i|
|
|
|
|
if (i != 0)
|
|
|
|
mutex.unlock
|
|
|
|
end
|
|
|
|
threads[i] = Thread.new do
|
|
|
|
mutex.synchronize do
|
|
|
|
condvar.wait mutex
|
|
|
|
end
|
|
|
|
end
|
|
|
|
mutex.lock
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(Timeout::Error) do
|
|
|
|
Timeout.timeout(0.1) { condvar.wait mutex }
|
|
|
|
end
|
|
|
|
mutex.unlock
|
|
|
|
threads.each(&:kill)
|
|
|
|
threads.each(&:join)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_timed_wait
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
timeout = 0.3
|
|
|
|
locked = false
|
2020-02-05 19:14:40 -05:00
|
|
|
woken = true
|
2012-12-01 03:29:09 -05:00
|
|
|
|
|
|
|
t0 = Time.now
|
|
|
|
mutex.synchronize do
|
|
|
|
begin
|
2020-02-05 19:14:40 -05:00
|
|
|
woken = condvar.wait(mutex, timeout)
|
2012-12-01 03:29:09 -05:00
|
|
|
ensure
|
|
|
|
locked = mutex.locked?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
t1 = Time.now
|
|
|
|
t = t1-t0
|
|
|
|
|
|
|
|
assert_operator(timeout*0.9, :<, t)
|
|
|
|
assert(locked)
|
2020-02-05 19:14:40 -05:00
|
|
|
assert_nil(woken)
|
2012-12-01 03:29:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_nolock
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
|
|
|
|
assert_raise(ThreadError) {condvar.wait(mutex)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_nolock_2
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
assert_raise(ThreadError) {condvar.wait(mutex)}
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_nolock_3
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
assert_raise(ThreadError) {condvar.wait(mutex, 0.1)}
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_empty_signal
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
|
|
|
|
assert_nothing_raised(Exception) { mutex.synchronize {condvar.signal} }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_condvar_empty_broadcast
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2012-12-01 03:29:09 -05:00
|
|
|
|
|
|
|
assert_nothing_raised(Exception) { mutex.synchronize {condvar.broadcast} }
|
|
|
|
end
|
2014-01-22 01:59:41 -05:00
|
|
|
|
|
|
|
def test_dup
|
|
|
|
bug9440 = '[ruby-core:59961] [Bug #9440]'
|
2021-06-28 10:01:53 -04:00
|
|
|
condvar = Thread::ConditionVariable.new
|
2014-01-22 01:59:41 -05:00
|
|
|
assert_raise(NoMethodError, bug9440) do
|
|
|
|
condvar.dup
|
|
|
|
end
|
|
|
|
end
|
2014-03-25 22:20:26 -04:00
|
|
|
|
2014-03-25 22:20:52 -04:00
|
|
|
(DumpableCV = ConditionVariable.dup).class_eval {remove_method :marshal_dump}
|
|
|
|
|
2014-03-25 22:20:26 -04:00
|
|
|
def test_dump
|
|
|
|
bug9674 = '[ruby-core:61677] [Bug #9674]'
|
2021-06-28 10:01:53 -04:00
|
|
|
condvar = Thread::ConditionVariable.new
|
2014-03-25 22:20:26 -04:00
|
|
|
assert_raise_with_message(TypeError, /#{ConditionVariable}/, bug9674) do
|
|
|
|
Marshal.dump(condvar)
|
|
|
|
end
|
2014-03-25 22:20:52 -04:00
|
|
|
|
|
|
|
condvar = DumpableCV.new
|
2017-05-09 15:52:10 -04:00
|
|
|
assert_raise(TypeError, bug9674) do
|
2014-03-25 22:20:52 -04:00
|
|
|
Marshal.dump(condvar)
|
|
|
|
end
|
2014-03-25 22:20:26 -04:00
|
|
|
end
|
2018-03-27 05:28:37 -04:00
|
|
|
|
|
|
|
def test_condvar_fork
|
2021-06-28 10:01:53 -04:00
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condvar = Thread::ConditionVariable.new
|
2018-03-27 05:28:37 -04:00
|
|
|
thrs = (1..10).map do
|
|
|
|
Thread.new { mutex.synchronize { condvar.wait(mutex) } }
|
|
|
|
end
|
|
|
|
thrs.each { 3.times { Thread.pass } }
|
|
|
|
pid = fork do
|
2018-04-30 19:53:55 -04:00
|
|
|
th = Thread.new do
|
|
|
|
mutex.synchronize { condvar.wait(mutex) }
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
until th.join(0.01)
|
|
|
|
mutex.synchronize { condvar.broadcast }
|
|
|
|
end
|
|
|
|
exit!(th.value == :ok ? 0 : 1)
|
2018-03-27 05:28:37 -04:00
|
|
|
end
|
|
|
|
_, s = Process.waitpid2(pid)
|
|
|
|
assert_predicate s, :success?, 'no segfault [ruby-core:86316] [Bug #14634]'
|
|
|
|
until thrs.empty?
|
|
|
|
mutex.synchronize { condvar.broadcast }
|
|
|
|
thrs.delete_if { |t| t.join(0.01) }
|
|
|
|
end
|
|
|
|
end if Process.respond_to?(:fork)
|
2012-12-01 03:29:09 -05:00
|
|
|
end
|