aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2017-11-18 00:25:52 +1030
committerGitHub <noreply@github.com>2017-11-18 00:25:52 +1030
commit56c1326abb11ed275f04b6e0592ca66975e37f24 (patch)
tree6a7718a66d73d0369a33caeb4b4cdd87c44ebbad /activesupport/test
parenteed3d3fff5ca6be00b2fe0fe020bd025ddbabbd5 (diff)
parent1f9f6f6cfc57020ccb35f77872c56f069f337075 (diff)
downloadrails-56c1326abb11ed275f04b6e0592ca66975e37f24.tar.gz
rails-56c1326abb11ed275f04b6e0592ca66975e37f24.tar.bz2
rails-56c1326abb11ed275f04b6e0592ca66975e37f24.zip
Merge pull request #31035 from BrentWheeldon/bmw-db-load-deadlock
Prevent deadlocks with load interlock and DB lock.
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/concurrency/load_interlock_aware_monitor_test.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/activesupport/test/concurrency/load_interlock_aware_monitor_test.rb b/activesupport/test/concurrency/load_interlock_aware_monitor_test.rb
new file mode 100644
index 0000000000..2d0f45ec5f
--- /dev/null
+++ b/activesupport/test/concurrency/load_interlock_aware_monitor_test.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal: true
+
+require "abstract_unit"
+require "concurrent/atomic/count_down_latch"
+require "active_support/concurrency/load_interlock_aware_monitor"
+
+module ActiveSupport
+ module Concurrency
+ class LoadInterlockAwareMonitorTest < ActiveSupport::TestCase
+ def setup
+ @monitor = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new
+ end
+
+ def test_entering_with_no_blocking
+ assert @monitor.mon_enter
+ end
+
+ def test_entering_with_blocking
+ load_interlock_latch = Concurrent::CountDownLatch.new
+ monitor_latch = Concurrent::CountDownLatch.new
+
+ able_to_use_monitor = false
+ able_to_load = false
+
+ thread_with_load_interlock = Thread.new do
+ ActiveSupport::Dependencies.interlock.running do
+ load_interlock_latch.count_down
+ monitor_latch.wait
+
+ @monitor.synchronize do
+ able_to_use_monitor = true
+ end
+ end
+ end
+
+ thread_with_monitor_lock = Thread.new do
+ @monitor.synchronize do
+ monitor_latch.count_down
+ load_interlock_latch.wait
+
+ ActiveSupport::Dependencies.interlock.loading do
+ able_to_load = true
+ end
+ end
+ end
+
+ thread_with_load_interlock.join
+ thread_with_monitor_lock.join
+
+ assert able_to_use_monitor
+ assert able_to_load
+ end
+ end
+ end
+end