rails/activerecord/test/cases/reaper_test.rb
eileencodes 79ce7d9af6
Deprecate spec_name and use name for configurations
I have so. many. regrets. about using `spec_name` for database
configurations and now I'm finally putting this mistake to an end.

Back when I started multi-db work I assumed that eventually
`connection_specification_name` (sometimes called `spec_name`) and
`spec_name` for configurations would one day be the same thing. After
2 years I no longer believe they will ever be the same thing.

This PR deprecates `spec_name` on database configurations in favor of
`name`. It's the same behavior, just a better name, or at least a
less confusing name.

`connection_specification_name` refers to the parent class name (ie
ActiveRecord::Base, AnimalsBase, etc) that holds the connection for it's
models. In some places like ConnectionHandler it shortens this to
`spec_name`, hence the major confusion.

Recently I've been working with some new folks on database stuff and
connection management and realize how confusing it was to explain that
`db_config.spec_name` was not `spec_name` and
`connection_specification_name`. Worse than that one is a symbole while
the other is a class name. This was made even more complicated by the
fact that `ActiveRecord::Base` used `primary` as the
`connection_specification_name` until #38190.

After spending 2 years with connection management I don't believe that
we can ever use the symbols from the database configs as a way to
connect the database without the class name being _somewhere_ because
a db_config does not know who it's owner class is until it's been
connected and a model has no idea what db_config belongs to it until
it's connected. The model is the only way to tie a primary/writer config
to a replica/reader config. This could change in the future but I don't
see value in adding a class name to the db_configs before connection or
telling a model what config belongs to it before connection. That would
probably break a lot of application assumptions. If we do ever end up in
that world, we can use name, because tbh `spec_name` and
`connection_specification_name` were always confusing to me.
2020-02-24 13:27:07 -05:00

200 lines
4.9 KiB
Ruby

# frozen_string_literal: true
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class ReaperTest < ActiveRecord::TestCase
class FakePool
attr_reader :reaped
attr_reader :flushed
def initialize(discarded: false)
@reaped = false
@discarded = discarded
end
def reap
@reaped = true
end
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
until fp.flushed
Thread.pass
end
assert fp.reaped
assert fp.flushed
ensure
fp.discard!
end
def test_pool_has_reaper
config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
pool_config = PoolConfig.new("primary", config)
pool = ConnectionPool.new(pool_config)
assert pool.reaper
ensure
pool.discard!
end
def test_reaping_frequency_configuration
pool_config = duplicated_pool_config(reaping_frequency: "10.01")
pool = ConnectionPool.new(pool_config)
assert_equal 10.01, pool.reaper.frequency
ensure
pool.discard!
end
def test_connection_pool_starts_reaper
pool_config = duplicated_pool_config(reaping_frequency: "0.0001")
pool = ConnectionPool.new(pool_config)
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
pool_config = duplicated_pool_config(reaping_frequency: "0.0001")
2.times do
pool = ConnectionPool.new(pool_config)
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
pool_config = duplicated_pool_config
pool = ConnectionPool.new(pool_config)
pool.discard!
pool.reap
pool.flush
end
if Process.respond_to?(:fork)
def test_connection_pool_starts_reaper_in_fork
pool_config = duplicated_pool_config(reaping_frequency: "0.0001")
pool = ConnectionPool.new(pool_config)
pool.checkout
pid = fork do
pool = ConnectionPool.new(pool_config)
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
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
private
def duplicated_pool_config(merge_config_options = {})
old_config = ActiveRecord::Base.connection_pool.db_config.configuration_hash.merge(merge_config_options)
db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new("arunit", "primary", old_config.dup)
PoolConfig.new("primary", db_config)
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
event.wait
[conn, child]
end
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
end
end
end
end