|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | +require 'concurrent' |
| 5 | + |
| 6 | +class PostgreSQLRaceConditionTest < GemTestCase |
| 7 | + self.use_transactional_tests = false |
| 8 | + |
| 9 | + def model_class |
| 10 | + Tag |
| 11 | + end |
| 12 | + |
| 13 | + setup do |
| 14 | + @lock_name = 'race_condition_test' |
| 15 | + end |
| 16 | + |
| 17 | + test 'advisory_lock_exists? does not create false positives in multi-threaded environment' do |
| 18 | + # Ensure no lock exists initially |
| 19 | + assert_not model_class.advisory_lock_exists?(@lock_name) |
| 20 | + |
| 21 | + results = Concurrent::Array.new |
| 22 | + |
| 23 | + # Create a thread pool with multiple workers checking simultaneously |
| 24 | + # This would previously cause race conditions where threads would falsely |
| 25 | + # report the lock exists due to another thread's existence check |
| 26 | + pool = Concurrent::FixedThreadPool.new(20) |
| 27 | + promises = 20.times.map do |
| 28 | + Concurrent::Promise.execute(executor: pool) do |
| 29 | + model_class.connection_pool.with_connection do |
| 30 | + # Each thread checks multiple times to increase chance of race condition |
| 31 | + 5.times do |
| 32 | + result = model_class.advisory_lock_exists?(@lock_name) |
| 33 | + results << result |
| 34 | + sleep(0.001) # Small delay to encourage interleaving |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + # Wait for all promises to complete |
| 41 | + Concurrent::Promise.zip(*promises).wait! |
| 42 | + pool.shutdown |
| 43 | + pool.wait_for_termination |
| 44 | + |
| 45 | + # All checks should report false since no lock was ever acquired |
| 46 | + assert results.all? { |r| r == false }, |
| 47 | + "Race condition detected: #{results.count(true)} false positives out of #{results.size} checks" |
| 48 | + end |
| 49 | + |
| 50 | + test 'advisory_lock_exists? correctly detects when lock is held by another connection' do |
| 51 | + lock_acquired = Concurrent::AtomicBoolean.new(false) |
| 52 | + lock_released = Concurrent::AtomicBoolean.new(false) |
| 53 | + |
| 54 | + # Promise 1: Acquire and hold the lock |
| 55 | + holder_promise = Concurrent::Promise.execute do |
| 56 | + model_class.connection_pool.with_connection do |
| 57 | + model_class.with_advisory_lock(@lock_name) do |
| 58 | + lock_acquired.make_true |
| 59 | + |
| 60 | + # Wait until we've confirmed the lock is detected |
| 61 | + sleep(0.01) until lock_released.true? |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + # Wait for lock to be acquired |
| 67 | + sleep(0.01) until lock_acquired.true? |
| 68 | + |
| 69 | + # Promise 2: Check if lock exists (should be true) |
| 70 | + checker_promise = Concurrent::Promise.execute do |
| 71 | + model_class.connection_pool.with_connection do |
| 72 | + # Check multiple times to ensure consistency |
| 73 | + 10.times do |
| 74 | + assert model_class.advisory_lock_exists?(@lock_name), |
| 75 | + 'Failed to detect existing lock' |
| 76 | + sleep(0.01) |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + # Let the checker run |
| 82 | + checker_promise.wait! |
| 83 | + |
| 84 | + # Release the lock |
| 85 | + lock_released.make_true |
| 86 | + holder_promise.wait! |
| 87 | + |
| 88 | + # Verify lock is released |
| 89 | + assert_not model_class.advisory_lock_exists?(@lock_name) |
| 90 | + end |
| 91 | + |
| 92 | + test 'new non-blocking implementation is being used for PostgreSQL' do |
| 93 | + # This test verifies that our new implementation is actually being called |
| 94 | + # We can check this by looking at whether the connection responds to our new method |
| 95 | + model_class.connection_pool.with_connection do |conn| |
| 96 | + assert conn.respond_to?(:advisory_lock_exists_for?), |
| 97 | + 'PostgreSQL connection should have advisory_lock_exists_for? method' |
| 98 | + |
| 99 | + # Test the method directly |
| 100 | + conn.lock_keys_for(@lock_name) |
| 101 | + result = conn.advisory_lock_exists_for?(@lock_name) |
| 102 | + assert_not_nil result, 'advisory_lock_exists_for? should return true/false, not nil' |
| 103 | + assert [true, false].include?(result), 'advisory_lock_exists_for? should return boolean' |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + test 'fallback works if pg_locks access fails' do |
| 108 | + # Test that the system gracefully falls back to the old implementation |
| 109 | + # if pg_locks query fails (e.g., due to permissions) |
| 110 | + model_class.connection_pool.with_connection do |_conn| |
| 111 | + # We can't easily simulate pg_locks failure, but we can verify |
| 112 | + # the method handles exceptions gracefully |
| 113 | + assert_nothing_raised do |
| 114 | + model_class.advisory_lock_exists?('test_lock_fallback') |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments