rails/activerecord/test/cases/reaper_test.rb

195 lines
4.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class ReaperTest < ActiveRecord::TestCase
class FakePool
attr_reader :reaped
2017-11-25 05:35:13 +00:00
attr_reader :flushed
def initialize(discarded: false)
@reaped = false
@discarded = discarded
end
def reap
@reaped = true
end
2017-11-25 05:35:13 +00:00
def flush
@flushed = true
end
def discard!
@discarded = true
end
def discarded?
@discarded
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
ensure
fp.discard!
end
def test_some_time
fp = FakePool.new
assert_not fp.reaped
reaper = ConnectionPool::Reaper.new(fp, 0.0001)
reaper.run
2019-01-02 22:55:48 +00:00
until fp.flushed
Thread.pass
end
assert fp.reaped
2017-11-25 05:35:13 +00:00
assert fp.flushed
ensure
fp.discard!
end
2011-12-30 23:27:41 +00:00
def test_pool_has_reaper
spec = ActiveRecord::Base.connection_pool.spec.dup
pool = ConnectionPool.new spec
2011-12-30 23:27:41 +00:00
assert pool.reaper
ensure
pool.discard!
2011-12-30 23:27:41 +00:00
end
def test_reaping_frequency_configuration
spec = ActiveRecord::Base.connection_pool.spec.dup
spec.underlying_configuration_hash[:reaping_frequency] = "10.01"
2011-12-30 23:27:41 +00:00
pool = ConnectionPool.new spec
assert_equal 10.01, pool.reaper.frequency
ensure
pool.discard!
2011-12-30 23:27:41 +00:00
end
2011-12-30 23:39:39 +00:00
def test_connection_pool_starts_reaper
spec = ActiveRecord::Base.connection_pool.spec.dup
spec.underlying_configuration_hash[:reaping_frequency] = "0.0001"
2011-12-30 23:39:39 +00:00
pool = ConnectionPool.new spec
conn, child = new_conn_in_thread(pool)
assert_predicate conn, :in_use?
child.terminate
wait_for_conn_idle(conn)
assert_not_predicate conn, :in_use?
ensure
pool.discard!
end
def test_reaper_works_after_pool_discard
spec = ActiveRecord::Base.connection_pool.spec.dup
spec.underlying_configuration_hash[:reaping_frequency] = "0.0001"
2.times do
pool = ConnectionPool.new spec
conn, child = new_conn_in_thread(pool)
assert_predicate conn, :in_use?
child.terminate
wait_for_conn_idle(conn)
assert_not_predicate conn, :in_use?
pool.discard!
end
end
# This doesn't test the reaper directly, but we want to test the action
# it would take on a discarded pool
def test_reap_flush_on_discarded_pool
spec = ActiveRecord::Base.connection_pool.spec.dup
pool = ConnectionPool.new spec
pool.discard!
pool.reap
pool.flush
end
def test_connection_pool_starts_reaper_in_fork
spec = ActiveRecord::Base.connection_pool.spec.dup
spec.underlying_configuration_hash[:reaping_frequency] = "0.0001"
pool = ConnectionPool.new spec
pool.checkout
pid = fork do
pool = ConnectionPool.new spec
conn, child = new_conn_in_thread(pool)
child.terminate
wait_for_conn_idle(conn)
if conn.in_use?
exit!(1)
else
exit!(0)
end
end
Process.waitpid(pid)
assert $?.success?
ensure
pool.discard!
end
def test_reaper_does_not_reap_discarded_connection_pools
discarded_pool = FakePool.new(discarded: true)
pool = FakePool.new
frequency = 0.001
ConnectionPool::Reaper.new(discarded_pool, frequency).run
ConnectionPool::Reaper.new(pool, frequency).run
until pool.flushed
Thread.pass
end
assert_not discarded_pool.reaped
assert pool.reaped
ensure
pool.discard!
end
def new_conn_in_thread(pool)
event = Concurrent::Event.new
conn = nil
child = Thread.new do
conn = pool.checkout
event.set
Thread.stop
end
2011-12-30 23:39:39 +00:00
event.wait
[conn, child]
end
2011-12-30 23:39:39 +00:00
def wait_for_conn_idle(conn, timeout = 5)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
while conn.in_use? && Process.clock_gettime(Process::CLOCK_MONOTONIC) - start < timeout
Thread.pass
end
2011-12-30 23:39:39 +00:00
end
end
end
end