2011-12-30 18:19:07 -05:00
|
|
|
require "cases/helper"
|
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
module ConnectionAdapters
|
|
|
|
class ReaperTest < ActiveRecord::TestCase
|
|
|
|
attr_reader :pool
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@pool = ConnectionPool.new ActiveRecord::Base.connection_pool.spec
|
|
|
|
end
|
|
|
|
|
2014-03-14 00:35:58 -04:00
|
|
|
teardown do
|
2011-12-30 18:19:07 -05:00
|
|
|
@pool.connections.each(&:close)
|
|
|
|
end
|
|
|
|
|
2011-12-30 19:10:53 -05:00
|
|
|
class FakePool
|
|
|
|
attr_reader :reaped
|
2011-12-30 18:19:07 -05:00
|
|
|
|
2011-12-30 19:10:53 -05:00
|
|
|
def initialize
|
|
|
|
@reaped = false
|
|
|
|
end
|
2011-12-30 18:19:07 -05:00
|
|
|
|
2011-12-30 19:10:53 -05:00
|
|
|
def reap
|
|
|
|
@reaped = true
|
|
|
|
end
|
2011-12-30 18:19:07 -05:00
|
|
|
end
|
|
|
|
|
2011-12-30 19:10:53 -05:00
|
|
|
# A reaper with nil time should never reap connections
|
|
|
|
def test_nil_time
|
|
|
|
fp = FakePool.new
|
|
|
|
assert !fp.reaped
|
|
|
|
reaper = ConnectionPool::Reaper.new(fp, nil)
|
|
|
|
reaper.run
|
|
|
|
assert !fp.reaped
|
|
|
|
end
|
2011-12-30 18:19:07 -05:00
|
|
|
|
2011-12-30 19:10:53 -05:00
|
|
|
def test_some_time
|
|
|
|
fp = FakePool.new
|
|
|
|
assert !fp.reaped
|
|
|
|
|
|
|
|
reaper = ConnectionPool::Reaper.new(fp, 0.0001)
|
|
|
|
reaper.run
|
|
|
|
until fp.reaped
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
assert fp.reaped
|
2011-12-30 18:19:07 -05:00
|
|
|
end
|
2011-12-30 18:27:41 -05:00
|
|
|
|
|
|
|
def test_pool_has_reaper
|
|
|
|
assert pool.reaper
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reaping_frequency_configuration
|
2011-12-30 18:36:48 -05:00
|
|
|
spec = ActiveRecord::Base.connection_pool.spec.dup
|
2011-12-30 18:27:41 -05:00
|
|
|
spec.config[:reaping_frequency] = 100
|
|
|
|
pool = ConnectionPool.new spec
|
|
|
|
assert_equal 100, pool.reaper.frequency
|
|
|
|
end
|
2011-12-30 18:39:39 -05:00
|
|
|
|
|
|
|
def test_connection_pool_starts_reaper
|
|
|
|
spec = ActiveRecord::Base.connection_pool.spec.dup
|
|
|
|
spec.config[:reaping_frequency] = 0.0001
|
|
|
|
|
|
|
|
pool = ConnectionPool.new spec
|
|
|
|
|
2014-03-07 08:36:09 -05:00
|
|
|
conn = nil
|
|
|
|
child = Thread.new do
|
|
|
|
conn = pool.checkout
|
|
|
|
Thread.stop
|
|
|
|
end
|
|
|
|
Thread.pass while conn.nil?
|
|
|
|
|
|
|
|
assert conn.in_use?
|
2011-12-30 18:39:39 -05:00
|
|
|
|
2014-03-07 08:36:09 -05:00
|
|
|
child.terminate
|
2011-12-30 18:39:39 -05:00
|
|
|
|
2014-03-07 08:36:09 -05:00
|
|
|
while conn.in_use?
|
2011-12-30 19:10:53 -05:00
|
|
|
Thread.pass
|
|
|
|
end
|
2014-03-07 08:36:09 -05:00
|
|
|
assert !conn.in_use?
|
2011-12-30 18:39:39 -05:00
|
|
|
end
|
2011-12-30 18:19:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|