Skip to content

Commit

Permalink
Avoid possible reaping during connection pool fairness test
Browse files Browse the repository at this point in the history
This test is occasionally showing a flaky result in CI... I'm not sure
this is the cause, but it seems like it could be a factor?
  • Loading branch information
matthewd committed Feb 5, 2023
1 parent 4b560ab commit 71a825c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions activerecord/test/cases/connection_pool_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,18 @@ def test_checkout_fairness
mutex = Mutex.new
order = []
errors = []
dispose_held_connections = Concurrent::Event.new

threads = expected.map do |i|
t = new_thread {
begin
@pool.checkout # never checked back in
mutex.synchronize { order << i }

# if the thread terminates, its connection may be
# reclaimed by the pool, so we need to hold on to it
# until we're done trickling in connections
dispose_held_connections.wait
rescue => e
mutex.synchronize { errors << e }
end
Expand All @@ -395,6 +401,7 @@ def test_checkout_fairness
# this should wake up the waiting threads one by one in order
conns.each { |conn| @pool.checkin(conn); sleep 0.1 }

dispose_held_connections.set
threads.each(&:join)

raise errors.first if errors.any?
Expand All @@ -417,12 +424,15 @@ def test_checkout_fairness_by_group
mutex = Mutex.new
successes = [] # threads that successfully got a connection
errors = []
dispose_held_connections = Concurrent::Event.new

make_thread = proc do |i|
t = new_thread {
begin
@pool.checkout # never checked back in
mutex.synchronize { successes << i }

dispose_held_connections.wait
rescue => e
mutex.synchronize { errors << e }
end
Expand Down Expand Up @@ -453,6 +463,7 @@ def test_checkout_fairness_by_group
winners = mutex.synchronize { successes.dup }
checkin.call(group2.size) # should wake up everyone remaining

dispose_held_connections.set
group1.each(&:join)
group2.each(&:join)

Expand Down

0 comments on commit 71a825c

Please sign in to comment.