diff options
Diffstat (limited to 'test/ruby/test_thread_cv.rb')
-rw-r--r-- | test/ruby/test_thread_cv.rb | 69 |
1 files changed, 35 insertions, 34 deletions
diff --git a/test/ruby/test_thread_cv.rb b/test/ruby/test_thread_cv.rb index 38bcc3b8fa..eb88b9606c 100644 --- a/test/ruby/test_thread_cv.rb +++ b/test/ruby/test_thread_cv.rb @@ -6,16 +6,11 @@ class TestThreadConditionVariable < Test::Unit::TestCase ConditionVariable = Thread::ConditionVariable Mutex = Thread::Mutex - def test_initialized - assert_raise(TypeError) { - ConditionVariable.allocate.wait(nil) - } - end - def test_condvar_signal_and_wait - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new result = [] + woken = nil mutex.synchronize do t = Thread.new do mutex.synchronize do @@ -25,18 +20,19 @@ class TestThreadConditionVariable < Test::Unit::TestCase end result << 0 - condvar.wait(mutex) + woken = condvar.wait(mutex) result << 2 t.join end assert_equal([0, 1, 2], result) + assert(woken) 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 + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new locked = false thread = Thread.new do @@ -61,8 +57,8 @@ class TestThreadConditionVariable < Test::Unit::TestCase def test_condvar_wait_and_broadcast nr_threads = 3 threads = Array.new - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new result = [] nr_threads.times do |i| @@ -87,12 +83,15 @@ class TestThreadConditionVariable < Test::Unit::TestCase end assert_equal ["C1", "C1", "C1", "P1", "P2", "C2", "C2", "C2"], result + ensure + threads.each(&:kill) + threads.each(&:join) end def test_condvar_wait_deadlock assert_in_out_err([], <<-INPUT, /\Afatal\nNo live threads left\. Deadlock/, []) - mutex = Mutex.new - cv = ConditionVariable.new + mutex = Thread::Mutex.new + cv = Thread::ConditionVariable.new klass = nil mesg = nil @@ -112,8 +111,8 @@ INPUT def test_condvar_wait_deadlock_2 nr_threads = 3 threads = Array.new - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new nr_threads.times do |i| if (i != 0) @@ -136,15 +135,16 @@ INPUT end def test_condvar_timed_wait - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new timeout = 0.3 locked = false + woken = true t0 = Time.now mutex.synchronize do begin - condvar.wait(mutex, timeout) + woken = condvar.wait(mutex, timeout) ensure locked = mutex.locked? end @@ -154,18 +154,19 @@ INPUT assert_operator(timeout*0.9, :<, t) assert(locked) + assert_nil(woken) end def test_condvar_nolock - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new assert_raise(ThreadError) {condvar.wait(mutex)} end def test_condvar_nolock_2 - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new Thread.new do assert_raise(ThreadError) {condvar.wait(mutex)} @@ -173,8 +174,8 @@ INPUT end def test_condvar_nolock_3 - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new Thread.new do assert_raise(ThreadError) {condvar.wait(mutex, 0.1)} @@ -182,22 +183,22 @@ INPUT end def test_condvar_empty_signal - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new assert_nothing_raised(Exception) { mutex.synchronize {condvar.signal} } end def test_condvar_empty_broadcast - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new assert_nothing_raised(Exception) { mutex.synchronize {condvar.broadcast} } end def test_dup bug9440 = '[ruby-core:59961] [Bug #9440]' - condvar = ConditionVariable.new + condvar = Thread::ConditionVariable.new assert_raise(NoMethodError, bug9440) do condvar.dup end @@ -207,7 +208,7 @@ INPUT def test_dump bug9674 = '[ruby-core:61677] [Bug #9674]' - condvar = ConditionVariable.new + condvar = Thread::ConditionVariable.new assert_raise_with_message(TypeError, /#{ConditionVariable}/, bug9674) do Marshal.dump(condvar) end @@ -219,8 +220,8 @@ INPUT end def test_condvar_fork - mutex = Mutex.new - condvar = ConditionVariable.new + mutex = Thread::Mutex.new + condvar = Thread::ConditionVariable.new thrs = (1..10).map do Thread.new { mutex.synchronize { condvar.wait(mutex) } } end |