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.
This commit is contained in:
parent
ed7c7657ae
commit
f890345c03
Notes:
git
2023-05-23 00:50:15 +00:00
@ -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 =~ /\A(?:run|sleep)\z/}, 'no threads running'
|
||||
assert (consumers + producers).all?{|thr| thr.status.to_s =~ /\A(?:run|sleep)\z/}, 'no threads running'
|
||||
|
||||
mutex.unlock
|
||||
|
||||
# just exercising the concurrency of the support methods.
|
||||
counter = Thread.new do
|
||||
|
Loading…
x
Reference in New Issue
Block a user