1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/reaper_test.rb

94 lines
2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
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
teardown do
@pool.connections.each(&:close)
end
class FakePool
attr_reader :reaped
2017-11-25 00:35:13 -05:00
attr_reader :flushed
def initialize
@reaped = false
end
def reap
@reaped = true
end
2017-11-25 00:35:13 -05:00
def flush
@flushed = true
end
end
# A reaper with nil time should never reap connections
def test_nil_time
fp = FakePool.new
assert_not fp.reaped
reaper = ConnectionPool::Reaper.new(fp, nil)
reaper.run
assert_not fp.reaped
end
def test_some_time
fp = FakePool.new
assert_not fp.reaped
reaper = ConnectionPool::Reaper.new(fp, 0.0001)
reaper.run
2019-01-02 17:55:48 -05:00
until fp.flushed
Thread.pass
end
assert fp.reaped
2017-11-25 00:35:13 -05:00
assert fp.flushed
end
2011-12-30 18:27:41 -05:00
def test_pool_has_reaper
assert pool.reaper
end
def test_reaping_frequency_configuration
spec = ActiveRecord::Base.connection_pool.spec.dup
spec.config[:reaping_frequency] = "10.01"
2011-12-30 18:27:41 -05:00
pool = ConnectionPool.new spec
assert_equal 10.01, pool.reaper.frequency
2011-12-30 18:27:41 -05:00
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"
2011-12-30 18:39:39 -05:00
pool = ConnectionPool.new spec
conn = nil
child = Thread.new do
conn = pool.checkout
Thread.stop
end
Thread.pass while conn.nil?
assert_predicate conn, :in_use?
2011-12-30 18:39:39 -05:00
child.terminate
2011-12-30 18:39:39 -05:00
while conn.in_use?
Thread.pass
end
assert_not_predicate conn, :in_use?
2011-12-30 18:39:39 -05:00
end
end
end
end