aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/share_lock_test.rb
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2015-07-21 10:55:54 +0930
committerMatthew Draper <matthew@trebex.net>2015-07-21 12:00:52 +0930
commitef4d3342728ad29a8ecf0f4eebf39e4c91aaabee (patch)
treea9d65dcd6ee50a669dfdbe0297c2de5d246c7533 /activesupport/test/share_lock_test.rb
parent649d8173c31ab0b74ef359e692eadf46b5da911d (diff)
downloadrails-ef4d3342728ad29a8ecf0f4eebf39e4c91aaabee.tar.gz
rails-ef4d3342728ad29a8ecf0f4eebf39e4c91aaabee.tar.bz2
rails-ef4d3342728ad29a8ecf0f4eebf39e4c91aaabee.zip
Add some meta-assertions for the custom assertions
I accidentally discovered `assert_threads_not_stuck` couldn't fail, so the simplest solution was to prove they're all now working in both directions.
Diffstat (limited to 'activesupport/test/share_lock_test.rb')
-rw-r--r--activesupport/test/share_lock_test.rb74
1 files changed, 63 insertions, 11 deletions
diff --git a/activesupport/test/share_lock_test.rb b/activesupport/test/share_lock_test.rb
index 87cd116429..8ca2a46a6c 100644
--- a/activesupport/test/share_lock_test.rb
+++ b/activesupport/test/share_lock_test.rb
@@ -158,23 +158,75 @@ class ShareLockTest < ActiveSupport::TestCase
end
private
- SUFFICIENT_TIMEOUT = 0.2
- def assert_threads_stuck_but_releasable_by_latch(threads, latch)
- assert_threads_stuck threads
- latch.count_down
- assert_threads_not_stuck threads
- end
+ module CustomAssertions
+ SUFFICIENT_TIMEOUT = 0.2
+
+ private
+
+ def assert_threads_stuck_but_releasable_by_latch(threads, latch)
+ assert_threads_stuck threads
+ latch.count_down
+ assert_threads_not_stuck threads
+ end
- 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?})
+ 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
+
+ def assert_threads_not_stuck(threads)
+ assert(Array(threads).all? { |t| t.join(SUFFICIENT_TIMEOUT) })
+ end
end
- def assert_threads_not_stuck(threads)
- assert_not_nil(Array(threads).all? {|t| t.join(SUFFICIENT_TIMEOUT)})
+ class CustomAssertionsTest < ActiveSupport::TestCase
+ include CustomAssertions
+
+ def setup
+ @latch = Concurrent::CountDownLatch.new
+ @thread = Thread.new { @latch.wait }
+ end
+
+ def teardown
+ @latch.count_down
+ @thread.join
+ end
+
+ def test_happy_path
+ assert_threads_stuck_but_releasable_by_latch @thread, @latch
+ end
+
+ def test_detects_stuck_thread
+ assert_raises(Minitest::Assertion) do
+ assert_threads_not_stuck @thread
+ end
+ end
+
+ def test_detects_free_thread
+ @latch.count_down
+ assert_raises(Minitest::Assertion) do
+ assert_threads_stuck @thread
+ end
+ end
+
+ def test_detects_already_released
+ @latch.count_down
+ assert_raises(Minitest::Assertion) do
+ assert_threads_stuck_but_releasable_by_latch @thread, @latch
+ end
+ end
+
+ 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
+ end
end
+ include CustomAssertions
+
def with_thread_waiting_in_lock_section(lock_section)
in_section = Concurrent::CountDownLatch.new
section_release = Concurrent::CountDownLatch.new