Skip to content

Commit d0d7425

Browse files
committed
Speedup ConnectionPoolTests
Most of the time was spent waiting on the default 5 seconds checkout timeout. Reducing in for just these tests saves about 1 minute of run time but more importantly saves me from trying to figure out if my refactoring introduced a deadlock of some sort. Before: `real 1m4.448s` After: `real 0m6.395s`
1 parent 1ca8c8e commit d0d7425

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

activerecord/test/cases/connection_pool_test.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ def setup
1212
@previous_isolation_level = ActiveSupport::IsolatedExecutionState.isolation_level
1313

1414
# Keep a duplicate pool so we do not bother others
15-
@db_config = ActiveRecord::Base.connection_pool.db_config
15+
config = ActiveRecord::Base.connection_pool.db_config
16+
@db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(
17+
config.env_name,
18+
config.name,
19+
config.configuration_hash.merge(
20+
checkout_timeout: 0.2, # Reduce checkout_timeout to speedup tests
21+
)
22+
)
23+
1624
@pool_config = ActiveRecord::ConnectionAdapters::PoolConfig.new(ActiveRecord::Base, @db_config, :writing, :default)
1725
@pool = ConnectionPool.new(@pool_config)
1826

@@ -436,7 +444,7 @@ def test_checkout_fairness
436444
end
437445

438446
# this should wake up the waiting threads one by one in order
439-
conns.each { |conn| @pool.checkin(conn); sleep 0.1 }
447+
conns.each { |conn| @pool.checkin(conn); sleep 0.01 }
440448

441449
dispose_held_connections.set
442450
threads.each(&:join)
@@ -768,11 +776,11 @@ def test_connection_pool_stat
768776
with_single_connection_pool do |pool|
769777
pool.with_connection do |connection|
770778
stats = pool.stat
771-
assert_equal({ size: 1, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 5 }, stats)
779+
assert_equal({ size: 1, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 0.2 }, stats)
772780
end
773781

774782
stats = pool.stat
775-
assert_equal({ size: 1, connections: 1, busy: 0, dead: 0, idle: 1, waiting: 0, checkout_timeout: 5 }, stats)
783+
assert_equal({ size: 1, connections: 1, busy: 0, dead: 0, idle: 1, waiting: 0, checkout_timeout: 0.2 }, stats)
776784

777785
assert_raise(ThreadError) do
778786
new_thread do
@@ -782,7 +790,7 @@ def test_connection_pool_stat
782790
end
783791

784792
stats = pool.stat
785-
assert_equal({ size: 1, connections: 1, busy: 0, dead: 1, idle: 0, waiting: 0, checkout_timeout: 5 }, stats)
793+
assert_equal({ size: 1, connections: 1, busy: 0, dead: 1, idle: 0, waiting: 0, checkout_timeout: 0.2 }, stats)
786794
ensure
787795
Thread.report_on_exception = original_report_on_exception
788796
end

0 commit comments

Comments
 (0)