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 = []
|
2012-10-26 10:51:02 -04:00
|
|
|
@connection = ActiveRecord::Base.remove_connection
|
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
|
|
|
|
begin
|
2012-10-26 10:51:02 -04:00
|
|
|
conn = ActiveRecord::Base.connection_pool.checkout
|
2008-08-22 15:11:44 -04:00
|
|
|
sleep 0.1
|
2012-10-26 10:51:02 -04:00
|
|
|
ActiveRecord::Base.connection_pool.checkin conn
|
2008-08-22 15:11:44 -04:00
|
|
|
@connection_count += 1
|
|
|
|
rescue ActiveRecord::ConnectionTimeoutError
|
|
|
|
@timed_out += 1
|
|
|
|
end
|
|
|
|
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
|
|
|
|
begin
|
|
|
|
conn = ActiveRecord::Base.connection_pool.checkout
|
|
|
|
ActiveRecord::Base.connection_pool.checkin conn
|
|
|
|
@connection_count += 1
|
2015-09-12 10:00:57 -04:00
|
|
|
ActiveRecord::Base.connection.data_sources
|
2014-12-23 09:54:52 -05:00
|
|
|
rescue ActiveRecord::ConnectionTimeoutError
|
|
|
|
@timed_out += 1
|
|
|
|
end
|
|
|
|
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
|
2009-10-16 12:39:32 -04:00
|
|
|
|
2009-05-01 09:58:10 -04:00
|
|
|
private
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def add_record(name)
|
|
|
|
ActiveRecord::Base.connection_pool.with_connection { Project.create! name: name }
|
|
|
|
end
|
2014-05-16 14:05:45 -04:00
|
|
|
end unless in_memory_db?
|