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
|
2011-10-05 20:21:43 -04:00
|
|
|
self.use_transactional_fixtures = false
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def teardown
|
2012-10-26 10:51:02 -04:00
|
|
|
ActiveRecord::Base.clear_all_connections!
|
|
|
|
ActiveRecord::Base.establish_connection(@connection)
|
2009-10-16 12:39:32 -04:00
|
|
|
@per_test_teardown.each {|td| td.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)
|
2012-10-26 10:51:02 -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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-10-16 12:39:32 -04:00
|
|
|
|
2009-05-01 09:58:10 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def add_record(name)
|
2012-10-26 10:51:02 -04:00
|
|
|
ActiveRecord::Base.connection_pool.with_connection { Project.create! :name => name }
|
2009-05-01 09:58:10 -04:00
|
|
|
end
|
2011-01-08 13:33:33 -05:00
|
|
|
end unless current_adapter?(:FrontBase) || in_memory_db?
|