2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2009-05-01 09:58:10 -04:00
|
|
|
require "models/project"
|
|
|
|
require "timeout"
|
2008-08-22 15:11:44 -04:00
|
|
|
|
|
|
|
class PooledConnectionsTest < ActiveRecord::TestCase
|
2015-03-10 22:21:19 -04:00
|
|
|
self.use_transactional_tests = false
|
2011-10-05 20:21:43 -04:00
|
|
|
|
2008-08-22 15:11:44 -04:00
|
|
|
def setup
|
2009-10-16 12:39:32 -04:00
|
|
|
@per_test_teardown = []
|
2020-01-17 17:08:14 -05:00
|
|
|
@connection = ActiveRecord::Base.remove_connection.configuration_hash
|
2008-08-22 15:11:44 -04:00
|
|
|
end
|
|
|
|
|
2014-03-14 00:35:58 -04:00
|
|
|
teardown do
|
2012-10-26 10:51:02 -04:00
|
|
|
ActiveRecord::Base.clear_all_connections!
|
|
|
|
ActiveRecord::Base.establish_connection(@connection)
|
2014-10-27 12:28:53 -04:00
|
|
|
@per_test_teardown.each(&:call)
|
2008-08-22 15:11:44 -04:00
|
|
|
end
|
|
|
|
|
2008-11-08 00:24:36 -05:00
|
|
|
# Will deadlock due to lack of Monitor timeouts in 1.9
|
2008-08-22 15:11:44 -04:00
|
|
|
def checkout_checkin_connections(pool_size, threads)
|
2016-08-06 13:44:11 -04:00
|
|
|
ActiveRecord::Base.establish_connection(@connection.merge(pool: pool_size, checkout_timeout: 0.5))
|
2008-08-22 15:11:44 -04:00
|
|
|
@connection_count = 0
|
|
|
|
@timed_out = 0
|
|
|
|
threads.times do
|
|
|
|
Thread.new do
|
2018-12-20 12:44:01 -05:00
|
|
|
conn = ActiveRecord::Base.connection_pool.checkout
|
|
|
|
sleep 0.1
|
|
|
|
ActiveRecord::Base.connection_pool.checkin conn
|
|
|
|
@connection_count += 1
|
|
|
|
rescue ActiveRecord::ConnectionTimeoutError
|
|
|
|
@timed_out += 1
|
2008-08-22 15:11:44 -04:00
|
|
|
end.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-23 09:54:52 -05:00
|
|
|
def checkout_checkin_connections_loop(pool_size, loops)
|
2016-08-06 13:44:11 -04:00
|
|
|
ActiveRecord::Base.establish_connection(@connection.merge(pool: pool_size, checkout_timeout: 0.5))
|
2014-12-23 09:54:52 -05:00
|
|
|
@connection_count = 0
|
|
|
|
@timed_out = 0
|
|
|
|
loops.times do
|
2018-12-20 12:44:01 -05:00
|
|
|
conn = ActiveRecord::Base.connection_pool.checkout
|
|
|
|
ActiveRecord::Base.connection_pool.checkin conn
|
|
|
|
@connection_count += 1
|
|
|
|
ActiveRecord::Base.connection.data_sources
|
|
|
|
rescue ActiveRecord::ConnectionTimeoutError
|
|
|
|
@timed_out += 1
|
2014-12-23 09:54:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-22 15:11:44 -04:00
|
|
|
def test_pooled_connection_checkin_one
|
|
|
|
checkout_checkin_connections 1, 2
|
|
|
|
assert_equal 2, @connection_count
|
|
|
|
assert_equal 0, @timed_out
|
2012-10-26 10:51:02 -04:00
|
|
|
assert_equal 1, ActiveRecord::Base.connection_pool.connections.size
|
2008-08-22 15:11:44 -04:00
|
|
|
end
|
|
|
|
|
2014-12-23 09:54:52 -05:00
|
|
|
def test_pooled_connection_checkin_two
|
|
|
|
checkout_checkin_connections_loop 2, 3
|
|
|
|
assert_equal 3, @connection_count
|
|
|
|
assert_equal 0, @timed_out
|
|
|
|
assert_equal 2, ActiveRecord::Base.connection_pool.connections.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pooled_connection_remove
|
2016-08-06 13:44:11 -04:00
|
|
|
ActiveRecord::Base.establish_connection(@connection.merge(pool: 2, checkout_timeout: 0.5))
|
2014-12-23 09:54:52 -05:00
|
|
|
old_connection = ActiveRecord::Base.connection
|
|
|
|
extra_connection = ActiveRecord::Base.connection_pool.checkout
|
|
|
|
ActiveRecord::Base.connection_pool.remove(extra_connection)
|
|
|
|
assert_equal ActiveRecord::Base.connection, old_connection
|
|
|
|
end
|
2014-05-16 14:05:45 -04:00
|
|
|
end unless in_memory_db?
|