2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2016-08-06 12:03:25 -04:00
|
|
|
require "concurrent/atomic/count_down_latch"
|
|
|
|
require "active_support/concurrency/share_lock"
|
2015-07-20 12:58:20 -04:00
|
|
|
|
|
|
|
class ShareLockTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@lock = ActiveSupport::Concurrency::ShareLock.new
|
|
|
|
end
|
|
|
|
|
2015-07-22 16:59:01 -04:00
|
|
|
def test_reentrancy
|
|
|
|
thread = Thread.new do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.sharing { @lock.sharing { } }
|
|
|
|
@lock.exclusive { @lock.exclusive { } }
|
2015-07-22 16:59:01 -04:00
|
|
|
end
|
|
|
|
assert_threads_not_stuck thread
|
|
|
|
end
|
|
|
|
|
2015-07-20 12:58:20 -04:00
|
|
|
def test_sharing_doesnt_block
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_latch|
|
2018-09-25 13:18:20 -04:00
|
|
|
assert_threads_not_stuck(Thread.new { @lock.sharing { } })
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sharing_blocks_exclusive
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
|
|
|
@lock.exclusive(no_wait: true) { flunk } # polling should fail
|
2018-09-25 13:18:20 -04:00
|
|
|
exclusive_thread = Thread.new { @lock.exclusive { } }
|
2015-07-20 12:58:20 -04:00
|
|
|
assert_threads_stuck_but_releasable_by_latch exclusive_thread, sharing_thread_release_latch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exclusive_blocks_sharing
|
|
|
|
with_thread_waiting_in_lock_section(:exclusive) do |exclusive_thread_release_latch|
|
2018-09-25 13:18:20 -04:00
|
|
|
sharing_thread = Thread.new { @lock.sharing { } }
|
2015-07-20 12:58:20 -04:00
|
|
|
assert_threads_stuck_but_releasable_by_latch sharing_thread, exclusive_thread_release_latch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-01 11:47:10 -05:00
|
|
|
def test_multiple_exclusives_are_able_to_progress
|
2015-07-20 12:58:20 -04:00
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
|
|
|
exclusive_threads = (1..2).map do
|
|
|
|
Thread.new do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive { }
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_threads_stuck_but_releasable_by_latch exclusive_threads, sharing_thread_release_latch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sharing_is_upgradeable_to_exclusive
|
|
|
|
upgrading_thread = Thread.new do
|
|
|
|
@lock.sharing do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive { }
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_threads_not_stuck upgrading_thread
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exclusive_upgrade_waits_for_other_sharers_to_leave
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
|
|
|
in_sharing = Concurrent::CountDownLatch.new
|
|
|
|
|
|
|
|
upgrading_thread = Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
in_sharing.count_down
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive { }
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
in_sharing.wait
|
|
|
|
assert_threads_stuck_but_releasable_by_latch upgrading_thread, sharing_thread_release_latch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exclusive_matching_purpose
|
|
|
|
[true, false].each do |use_upgrading|
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
|
|
|
exclusive_threads = (1..2).map do
|
|
|
|
Thread.new do
|
2020-10-02 02:44:03 -04:00
|
|
|
@lock.public_send(use_upgrading ? :sharing : :tap) do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive(purpose: :load, compatible: [:load, :unload]) { }
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_threads_stuck_but_releasable_by_latch exclusive_threads, sharing_thread_release_latch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-20 22:18:21 -04:00
|
|
|
def test_killed_thread_loses_lock
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
|
|
|
thread = Thread.new do
|
|
|
|
@lock.sharing do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive { }
|
2015-07-20 22:18:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_threads_stuck thread
|
|
|
|
thread.kill
|
|
|
|
|
|
|
|
sharing_thread_release_latch.count_down
|
|
|
|
|
|
|
|
thread = Thread.new do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive { }
|
2015-07-20 22:18:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_threads_not_stuck thread
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-20 12:58:20 -04:00
|
|
|
def test_exclusive_conflicting_purpose
|
|
|
|
[true, false].each do |use_upgrading|
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
2018-12-20 12:44:01 -05:00
|
|
|
together = Concurrent::CyclicBarrier.new(2)
|
|
|
|
conflicting_exclusive_threads = [
|
|
|
|
Thread.new do
|
2020-10-02 02:44:03 -04:00
|
|
|
@lock.public_send(use_upgrading ? :sharing : :tap) do
|
2018-12-20 12:44:01 -05:00
|
|
|
together.wait
|
|
|
|
@lock.exclusive(purpose: :red, compatible: [:green, :purple]) { }
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Thread.new do
|
2020-10-02 02:44:03 -04:00
|
|
|
@lock.public_send(use_upgrading ? :sharing : :tap) do
|
2018-12-20 12:44:01 -05:00
|
|
|
together.wait
|
|
|
|
@lock.exclusive(purpose: :blue, compatible: [:green]) { }
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
2018-12-20 12:44:01 -05:00
|
|
|
]
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
assert_threads_stuck conflicting_exclusive_threads # wait for threads to get into their respective `exclusive {}` blocks
|
2015-07-20 22:17:16 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
# This thread will be stuck as long as any other thread is in
|
|
|
|
# a sharing block. While it's blocked, it holds no lock, so it
|
|
|
|
# doesn't interfere with any other attempts.
|
|
|
|
no_purpose_thread = Thread.new do
|
|
|
|
@lock.exclusive { }
|
|
|
|
end
|
|
|
|
assert_threads_stuck no_purpose_thread
|
|
|
|
|
|
|
|
# This thread is compatible with both of the "primary"
|
|
|
|
# attempts above. It's initially stuck on the outer share
|
|
|
|
# lock, but as soon as that's released, it can run --
|
|
|
|
# regardless of whether those threads hold share locks.
|
|
|
|
compatible_thread = Thread.new do
|
|
|
|
@lock.exclusive(purpose: :green, compatible: []) { }
|
|
|
|
end
|
|
|
|
assert_threads_stuck compatible_thread
|
2015-07-20 22:17:16 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
assert_threads_stuck conflicting_exclusive_threads
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
sharing_thread_release_latch.count_down
|
2015-07-20 22:17:16 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
assert_threads_not_stuck compatible_thread # compatible thread is now able to squeak through
|
2015-07-20 22:17:16 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
if use_upgrading
|
|
|
|
# The "primary" threads both each hold a share lock, and are
|
|
|
|
# mutually incompatible; they're still stuck.
|
|
|
|
assert_threads_stuck conflicting_exclusive_threads
|
2015-07-20 22:17:16 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
# The thread without a specified purpose is also stuck; it's
|
|
|
|
# not compatible with anything.
|
|
|
|
assert_threads_stuck no_purpose_thread
|
|
|
|
else
|
|
|
|
# As the primaries didn't hold a share lock, as soon as the
|
|
|
|
# outer one was released, all the exclusive locks are free
|
|
|
|
# to be acquired in turn.
|
|
|
|
|
|
|
|
assert_threads_not_stuck conflicting_exclusive_threads
|
|
|
|
assert_threads_not_stuck no_purpose_thread
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
2018-12-20 12:44:01 -05:00
|
|
|
ensure
|
|
|
|
conflicting_exclusive_threads.each(&:kill)
|
|
|
|
no_purpose_thread.kill
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exclusive_ordering
|
2015-07-22 16:59:01 -04:00
|
|
|
scratch_pad = []
|
|
|
|
scratch_pad_mutex = Mutex.new
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2015-07-22 16:59:01 -04:00
|
|
|
load_params = [:load, [:load]]
|
|
|
|
unload_params = [:unload, [:unload, :load]]
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2016-02-01 08:11:07 -05:00
|
|
|
all_sharing = Concurrent::CyclicBarrier.new(4)
|
|
|
|
|
2015-07-22 16:59:01 -04:00
|
|
|
[load_params, load_params, unload_params, unload_params].permutation do |thread_params|
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
|
|
|
threads = thread_params.map do |purpose, compatible|
|
|
|
|
Thread.new do
|
|
|
|
@lock.sharing do
|
2016-02-01 08:11:07 -05:00
|
|
|
all_sharing.wait
|
2015-07-22 16:59:01 -04:00
|
|
|
@lock.exclusive(purpose: purpose, compatible: compatible) do
|
|
|
|
scratch_pad_mutex.synchronize { scratch_pad << purpose }
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-07-22 16:59:01 -04:00
|
|
|
end
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2015-07-22 16:59:01 -04:00
|
|
|
sleep(0.01)
|
|
|
|
scratch_pad_mutex.synchronize { assert_empty scratch_pad }
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2015-07-22 16:59:01 -04:00
|
|
|
sharing_thread_release_latch.count_down
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2015-07-22 16:59:01 -04:00
|
|
|
assert_threads_not_stuck threads
|
|
|
|
scratch_pad_mutex.synchronize do
|
|
|
|
assert_equal [:load, :load, :unload, :unload], scratch_pad
|
|
|
|
scratch_pad.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-01 08:11:07 -05:00
|
|
|
def test_new_share_attempts_block_on_waiting_exclusive
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
|
|
|
release_exclusive = Concurrent::CountDownLatch.new
|
|
|
|
|
|
|
|
waiting_exclusive = Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
@lock.exclusive do
|
|
|
|
release_exclusive.wait
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_threads_stuck waiting_exclusive
|
|
|
|
|
|
|
|
late_share_attempt = Thread.new do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.sharing { }
|
2016-02-01 08:11:07 -05:00
|
|
|
end
|
|
|
|
assert_threads_stuck late_share_attempt
|
|
|
|
|
|
|
|
sharing_thread_release_latch.count_down
|
|
|
|
assert_threads_stuck late_share_attempt
|
|
|
|
|
|
|
|
release_exclusive.count_down
|
|
|
|
assert_threads_not_stuck late_share_attempt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-01 08:28:36 -05:00
|
|
|
def test_share_remains_reentrant_ignoring_a_waiting_exclusive
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
|
|
|
ready = Concurrent::CyclicBarrier.new(2)
|
|
|
|
attempt_reentrancy = Concurrent::CountDownLatch.new
|
|
|
|
|
|
|
|
sharer = Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
|
|
|
attempt_reentrancy.wait
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.sharing { }
|
2016-02-01 08:28:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
exclusive = Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive { }
|
2016-02-01 08:28:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_threads_stuck exclusive
|
|
|
|
|
|
|
|
attempt_reentrancy.count_down
|
|
|
|
|
|
|
|
assert_threads_not_stuck sharer
|
|
|
|
assert_threads_stuck exclusive
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-01 10:12:20 -05:00
|
|
|
def test_compatible_exclusives_cooperate_to_both_proceed
|
|
|
|
ready = Concurrent::CyclicBarrier.new(2)
|
|
|
|
done = Concurrent::CyclicBarrier.new(2)
|
|
|
|
|
|
|
|
threads = 2.times.map do
|
|
|
|
Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive(purpose: :x, compatible: [:x], after_compatible: [:x]) { }
|
2016-02-01 10:12:20 -05:00
|
|
|
done.wait
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_threads_not_stuck threads
|
|
|
|
end
|
|
|
|
|
2016-02-06 16:54:57 -05:00
|
|
|
def test_manual_yield
|
|
|
|
ready = Concurrent::CyclicBarrier.new(2)
|
|
|
|
done = Concurrent::CyclicBarrier.new(2)
|
|
|
|
|
|
|
|
threads = [
|
|
|
|
Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive(purpose: :x) { }
|
2016-02-06 16:54:57 -05:00
|
|
|
done.wait
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
|
|
|
@lock.yield_shares(compatible: [:x]) do
|
|
|
|
done.wait
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
]
|
|
|
|
|
|
|
|
assert_threads_not_stuck threads
|
|
|
|
end
|
|
|
|
|
2016-02-06 18:57:37 -05:00
|
|
|
def test_manual_incompatible_yield
|
|
|
|
ready = Concurrent::CyclicBarrier.new(2)
|
|
|
|
done = Concurrent::CyclicBarrier.new(2)
|
|
|
|
|
|
|
|
threads = [
|
|
|
|
Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive(purpose: :x) { }
|
2016-02-06 18:57:37 -05:00
|
|
|
done.wait
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
|
|
|
@lock.yield_shares(compatible: [:y]) do
|
|
|
|
done.wait
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
]
|
|
|
|
|
|
|
|
assert_threads_stuck threads
|
|
|
|
ensure
|
|
|
|
threads.each(&:kill) if threads
|
|
|
|
end
|
|
|
|
|
2016-02-07 13:43:05 -05:00
|
|
|
def test_manual_recursive_yield
|
|
|
|
ready = Concurrent::CyclicBarrier.new(2)
|
|
|
|
done = Concurrent::CyclicBarrier.new(2)
|
|
|
|
do_nesting = Concurrent::CountDownLatch.new
|
|
|
|
|
|
|
|
threads = [
|
|
|
|
Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive(purpose: :x) { }
|
2016-02-07 13:43:05 -05:00
|
|
|
done.wait
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
@lock.yield_shares(compatible: [:x]) do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
|
|
|
do_nesting.wait
|
|
|
|
@lock.yield_shares(compatible: [:x, :y]) do
|
|
|
|
done.wait
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
]
|
|
|
|
|
|
|
|
assert_threads_stuck threads
|
|
|
|
do_nesting.count_down
|
|
|
|
|
|
|
|
assert_threads_not_stuck threads
|
|
|
|
end
|
|
|
|
|
2016-02-07 13:02:37 -05:00
|
|
|
def test_manual_recursive_yield_cannot_expand_outer_compatible
|
2016-02-06 18:57:37 -05:00
|
|
|
ready = Concurrent::CyclicBarrier.new(2)
|
|
|
|
do_compatible_nesting = Concurrent::CountDownLatch.new
|
2016-02-07 13:02:37 -05:00
|
|
|
in_compatible_nesting = Concurrent::CountDownLatch.new
|
2016-02-06 18:57:37 -05:00
|
|
|
|
2016-02-07 13:02:37 -05:00
|
|
|
incompatible_thread = Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive(purpose: :x) { }
|
2016-02-07 13:02:37 -05:00
|
|
|
end
|
|
|
|
end
|
2016-02-06 18:57:37 -05:00
|
|
|
|
2016-02-07 13:02:37 -05:00
|
|
|
yield_shares_thread = Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
|
|
|
@lock.yield_shares(compatible: [:y]) do
|
|
|
|
do_compatible_nesting.wait
|
|
|
|
@lock.sharing do
|
|
|
|
@lock.yield_shares(compatible: [:x, :y]) do
|
|
|
|
in_compatible_nesting.wait
|
2016-02-06 18:57:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-07 13:02:37 -05:00
|
|
|
end
|
2016-02-06 18:57:37 -05:00
|
|
|
|
2016-02-07 13:02:37 -05:00
|
|
|
assert_threads_stuck incompatible_thread
|
2016-02-06 18:57:37 -05:00
|
|
|
do_compatible_nesting.count_down
|
2016-02-07 13:02:37 -05:00
|
|
|
assert_threads_stuck incompatible_thread
|
|
|
|
in_compatible_nesting.count_down
|
|
|
|
assert_threads_not_stuck [yield_shares_thread, incompatible_thread]
|
2016-02-06 18:57:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_manual_recursive_yield_restores_previous_compatible
|
|
|
|
ready = Concurrent::CyclicBarrier.new(2)
|
|
|
|
do_nesting = Concurrent::CountDownLatch.new
|
|
|
|
after_nesting = Concurrent::CountDownLatch.new
|
|
|
|
|
|
|
|
incompatible_thread = Thread.new do
|
|
|
|
ready.wait
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive(purpose: :z) { }
|
2016-02-06 18:57:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
recursive_yield_shares_thread = Thread.new do
|
|
|
|
@lock.sharing do
|
|
|
|
ready.wait
|
|
|
|
@lock.yield_shares(compatible: [:y]) do
|
|
|
|
do_nesting.wait
|
|
|
|
@lock.sharing do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.yield_shares(compatible: [:x, :y]) { }
|
2016-02-06 18:57:37 -05:00
|
|
|
end
|
|
|
|
after_nesting.wait
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_threads_stuck incompatible_thread
|
|
|
|
do_nesting.count_down
|
|
|
|
assert_threads_stuck incompatible_thread
|
|
|
|
|
|
|
|
compatible_thread = Thread.new do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive(purpose: :y) { }
|
2016-02-06 18:57:37 -05:00
|
|
|
end
|
|
|
|
assert_threads_not_stuck compatible_thread
|
|
|
|
|
|
|
|
post_nesting_incompatible_thread = Thread.new do
|
2018-09-25 13:18:20 -04:00
|
|
|
@lock.exclusive(purpose: :x) { }
|
2016-02-06 18:57:37 -05:00
|
|
|
end
|
|
|
|
assert_threads_stuck post_nesting_incompatible_thread
|
|
|
|
|
|
|
|
after_nesting.count_down
|
|
|
|
assert_threads_not_stuck recursive_yield_shares_thread
|
|
|
|
# post_nesting_incompatible_thread can now proceed
|
|
|
|
assert_threads_not_stuck post_nesting_incompatible_thread
|
|
|
|
# assert_threads_not_stuck can now proceed
|
|
|
|
assert_threads_not_stuck incompatible_thread
|
|
|
|
end
|
|
|
|
|
2015-07-22 16:59:01 -04:00
|
|
|
def test_in_shared_section_incompatible_non_upgrading_threads_cannot_preempt_upgrading_threads
|
|
|
|
scratch_pad = []
|
|
|
|
scratch_pad_mutex = Mutex.new
|
|
|
|
|
|
|
|
upgrading_load_params = [:load, [:load], true]
|
|
|
|
non_upgrading_unload_params = [:unload, [:load, :unload], false]
|
|
|
|
|
|
|
|
[upgrading_load_params, non_upgrading_unload_params].permutation do |thread_params|
|
|
|
|
with_thread_waiting_in_lock_section(:sharing) do |sharing_thread_release_latch|
|
|
|
|
threads = thread_params.map do |purpose, compatible, use_upgrading|
|
|
|
|
Thread.new do
|
2020-10-02 02:44:03 -04:00
|
|
|
@lock.public_send(use_upgrading ? :sharing : :tap) do
|
2015-07-22 16:59:01 -04:00
|
|
|
@lock.exclusive(purpose: purpose, compatible: compatible) do
|
|
|
|
scratch_pad_mutex.synchronize { scratch_pad << purpose }
|
|
|
|
end
|
2015-07-20 19:20:50 -04:00
|
|
|
end
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
2015-07-22 16:59:01 -04:00
|
|
|
|
|
|
|
assert_threads_stuck threads
|
|
|
|
scratch_pad_mutex.synchronize { assert_empty scratch_pad }
|
|
|
|
|
|
|
|
sharing_thread_release_latch.count_down
|
|
|
|
|
|
|
|
assert_threads_not_stuck threads
|
|
|
|
scratch_pad_mutex.synchronize do
|
|
|
|
assert_equal [:load, :unload], scratch_pad
|
|
|
|
scratch_pad.clear
|
|
|
|
end
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
module CustomAssertions
|
|
|
|
SUFFICIENT_TIMEOUT = 0.2
|
2015-07-20 21:25:54 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
private
|
|
|
|
def assert_threads_stuck_but_releasable_by_latch(threads, latch)
|
|
|
|
assert_threads_stuck threads
|
|
|
|
latch.count_down
|
|
|
|
assert_threads_not_stuck threads
|
|
|
|
end
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def assert_threads_stuck(threads)
|
|
|
|
sleep(SUFFICIENT_TIMEOUT) # give threads time to do their business
|
|
|
|
assert(Array(threads).all? { |t| t.join(0.001).nil? })
|
|
|
|
end
|
2015-07-20 21:25:54 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def assert_threads_not_stuck(threads)
|
|
|
|
assert(Array(threads).all? { |t| t.join(SUFFICIENT_TIMEOUT) })
|
|
|
|
end
|
2015-07-20 21:25:54 -04:00
|
|
|
end
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
class CustomAssertionsTest < ActiveSupport::TestCase
|
|
|
|
include CustomAssertions
|
2015-07-20 21:25:54 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def setup
|
|
|
|
@latch = Concurrent::CountDownLatch.new
|
|
|
|
@thread = Thread.new { @latch.wait }
|
|
|
|
end
|
2015-07-20 21:25:54 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def teardown
|
|
|
|
@latch.count_down
|
|
|
|
@thread.join
|
|
|
|
end
|
2015-07-20 21:25:54 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_happy_path
|
|
|
|
assert_threads_stuck_but_releasable_by_latch @thread, @latch
|
|
|
|
end
|
2015-07-20 21:25:54 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_detects_stuck_thread
|
|
|
|
assert_raises(Minitest::Assertion) do
|
|
|
|
assert_threads_not_stuck @thread
|
|
|
|
end
|
2015-07-20 21:25:54 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_detects_free_thread
|
|
|
|
@latch.count_down
|
|
|
|
assert_raises(Minitest::Assertion) do
|
|
|
|
assert_threads_stuck @thread
|
|
|
|
end
|
2015-07-20 21:25:54 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_detects_already_released
|
|
|
|
@latch.count_down
|
|
|
|
assert_raises(Minitest::Assertion) do
|
|
|
|
assert_threads_stuck_but_releasable_by_latch @thread, @latch
|
|
|
|
end
|
2015-07-20 21:25:54 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_detects_remains_latched
|
|
|
|
another_latch = Concurrent::CountDownLatch.new
|
|
|
|
assert_raises(Minitest::Assertion) do
|
|
|
|
assert_threads_stuck_but_releasable_by_latch @thread, another_latch
|
|
|
|
end
|
2015-07-20 21:25:54 -04:00
|
|
|
end
|
|
|
|
end
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
include CustomAssertions
|
2015-07-20 21:25:54 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def with_thread_waiting_in_lock_section(lock_section)
|
|
|
|
in_section = Concurrent::CountDownLatch.new
|
|
|
|
section_release = Concurrent::CountDownLatch.new
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
stuck_thread = Thread.new do
|
2020-10-02 02:46:11 -04:00
|
|
|
@lock.public_send(lock_section) do
|
2016-08-06 13:55:02 -04:00
|
|
|
in_section.count_down
|
|
|
|
section_release.wait
|
|
|
|
end
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
in_section.wait
|
2015-07-20 12:58:20 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
yield section_release
|
|
|
|
ensure
|
|
|
|
section_release.count_down
|
|
|
|
stuck_thread.join # clean up
|
|
|
|
end
|
2015-07-20 12:58:20 -04:00
|
|
|
end
|