Skip to content

Commit

Permalink
[test] for reproducing mutex regression (GH-5520)
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Dec 21, 2018
1 parent cde58ad commit 64063fa
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions test/jruby/test_thread.rb
Expand Up @@ -335,20 +335,20 @@ def test_sleep_wakeup_interlacing
go = false
ret = []
t = Thread.new do
10000.times do
10_000.times do
Thread.pass until go
sleep
ret << 'ok'
end
end
10000.times do
10_000.times do
go = true
Thread.pass until t.status == 'sleep'
go = false
t.wakeup
end
t.join
assert_equal(10000, ret.size)
assert_equal(10_000, ret.size)
end

def test_inspect_and_to_s
Expand Down Expand Up @@ -402,6 +402,48 @@ def test_thread_name
end.join
end

CountDownLatch = Class.new do
def initialize(count)
@count = count
@mutex = Mutex.new
@conditional = ConditionVariable.new
end

def countdown!
@mutex.synchronize do
@count -= 1 if @count > 0
@conditional.broadcast if @count == 0
end
end

def count; @mutex.synchronize { @count } end

def wait(timeout = nil)
Timeout::timeout timeout do
@mutex.synchronize { @conditional.wait @mutex if @count > 0 }
true
end
end
end

def wait_for_latch(count = 5, timeout = 1.0)
latch = CountDownLatch.new count
count.times do
Thread.new { latch.countdown! }
end
latch.wait(timeout)
end
private :wait_for_latch

def test_count_down_latch
require 'timeout'

(ENV['TIMES'] || 10_000).to_i.times do |i|
print '*' if $VERBOSE
assert wait_for_latch # should not raise
end
end

private

def native_thread_name(thread)
Expand Down

0 comments on commit 64063fa

Please sign in to comment.