diff options
author | Charles Oliver Nutter <[email protected]> | 2023-05-19 09:11:43 -0500 |
---|---|---|
committer | Charles Oliver Nutter <[email protected]> | 2023-05-22 20:49:53 -0400 |
commit | f890345c03dc48072167df4bce45bc506dba2a1d (patch) | |
tree | 6326c62d19480784d24996d49c3594f8626e4628 /test/ruby/test_thread_queue.rb | |
parent | ed7c7657ae0065a5007b443d146590e6e6a9dcbe (diff) |
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.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/7830
Diffstat (limited to 'test/ruby/test_thread_queue.rb')
-rw-r--r-- | test/ruby/test_thread_queue.rb | 13 |
1 files 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 |