From f890345c03dc48072167df4bce45bc506dba2a1d Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Fri, 19 May 2023 09:11:43 -0500 Subject: Ensure producer threads do not start running early This test caused issues occasionally on JRuby because it's possible for a producer thread to run to completion before the status checks begin. This results in Thread#status returning false and the =~ call triggering a warning (3.1) or error (3.2) and either emitting thousands of deprecation warnings or failing outright. The patch here introduces a mutex that remains locked until all producer threads are seen to have started. The lock is then released and the test proceeds. This prevents any producers from running to completion, which in turn prevents consumers running to completion, avoiding the warnings or error calling =~ on false. This patch also modifies the status checks to to_s the thread status, preventing any prematurely terminated threads from triggering similar deprecation warnings or missing method errors when =~ is called on nil. --- test/ruby/test_thread_queue.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/ruby/test_thread_queue.rb b/test/ruby/test_thread_queue.rb index 72b0f557b6..fd77853f0e 100644 --- a/test/ruby/test_thread_queue.rb +++ b/test/ruby/test_thread_queue.rb @@ -585,9 +585,14 @@ class TestThreadQueue < Test::Unit::TestCase count_items = rand(3000..5000) count_producers = rand(10..20) + # ensure threads do not start running too soon and complete before we check status + mutex = Mutex.new + mutex.lock + producers = count_producers.times.map do Thread.new do - sleep(rand / 100) + mutex.lock + mutex.unlock count_items.times{|i| q << [i,"#{i} for #{Thread.current.inspect}"]} end end @@ -605,9 +610,11 @@ class TestThreadQueue < Test::Unit::TestCase # No dead or finished threads, give up to 10 seconds to start running t = Time.now - Thread.pass until Time.now - t > 10 || (consumers + producers).all?{|thr| thr.status =~ /\A(?:run|sleep)\z/} + Thread.pass until Time.now - t > 10 || (consumers + producers).all?{|thr| thr.status.to_s =~ /\A(?:run|sleep)\z/} + + assert (consumers + producers).all?{|thr| thr.status.to_s =~ /\A(?:run|sleep)\z/}, 'no threads running' - assert (consumers + producers).all?{|thr| thr.status =~ /\A(?:run|sleep)\z/}, 'no threads running' + mutex.unlock # just exercising the concurrency of the support methods. counter = Thread.new do -- cgit v1.2.3