Project

General

Profile

Patch #44116 » 0002-Fix-flaky-IssueNestedSetConcurrencyTest.patch

Katsuya HIDAKA, 2026-05-23 04:39

View differences:

test/unit/issue_nested_set_concurrency_test.rb
self.use_transactional_tests = false
def setup
@connection_pool_mutex = Mutex.new
skip if sqlite?
if mysql?
connection = ActiveRecord::Base.connection_db_config.configuration_hash.deep_dup
......
threads = []
ids_to_remove.each_slice(10) do |ids|
threads << Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
begin
begin
with_thread_connection do
ids.each do |id|
Issue.find(id).update(parent_id: nil)
end
rescue => e
Thread.current[:exception] = e.message
end
rescue => e
Thread.current[:exception] = e.message
end
end
end
......
private
def threaded(count, &)
def threaded(count, &block)
with_settings :notified_events => [] do
threads = []
count.times do |i|
threads << Thread.new(i) do
ActiveRecord::Base.connection_pool.with_connection do
begin
yield
rescue => e
Thread.current[:exception] = e.message
end
begin
with_thread_connection(&block)
rescue => e
Thread.current[:exception] = e.message
end
end
end
......
end
end
end
def with_thread_connection(&)
# Active Record updates its connection lease registry when a thread obtains a
# connection for the first time, which can race when test threads start
# together.
@connection_pool_mutex.synchronize do
# Initialize this thread's connection lease before concurrent updates.
ActiveRecord::Base.connection_pool.with_connection { nil }
end
ActiveRecord::Base.connection_pool.with_connection(&)
end
end
(2-2/4)