2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-12-30 18:19:07 -05:00
|
|
|
require "cases/helper"
|
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
module ConnectionAdapters
|
|
|
|
class ReaperTest < ActiveRecord::TestCase
|
2011-12-30 19:10:53 -05:00
|
|
|
class FakePool
|
|
|
|
attr_reader :reaped
|
2017-11-25 00:35:13 -05:00
|
|
|
attr_reader :flushed
|
2011-12-30 18:19:07 -05:00
|
|
|
|
2019-08-21 03:34:47 -04:00
|
|
|
def initialize(discarded: false)
|
2011-12-30 19:10:53 -05:00
|
|
|
@reaped = false
|
2019-08-21 03:34:47 -04:00
|
|
|
@discarded = discarded
|
2011-12-30 19:10:53 -05:00
|
|
|
end
|
2011-12-30 18:19:07 -05:00
|
|
|
|
2011-12-30 19:10:53 -05:00
|
|
|
def reap
|
|
|
|
@reaped = true
|
|
|
|
end
|
2017-11-25 00:35:13 -05:00
|
|
|
|
|
|
|
def flush
|
|
|
|
@flushed = true
|
|
|
|
end
|
2019-08-21 03:34:47 -04:00
|
|
|
|
|
|
|
def discard!
|
|
|
|
@discarded = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def discarded?
|
|
|
|
@discarded
|
|
|
|
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
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not fp.reaped
|
2011-12-30 19:10:53 -05:00
|
|
|
reaper = ConnectionPool::Reaper.new(fp, nil)
|
|
|
|
reaper.run
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not fp.reaped
|
2019-08-21 03:34:47 -04:00
|
|
|
ensure
|
|
|
|
fp.discard!
|
2011-12-30 19:10:53 -05:00
|
|
|
end
|
2011-12-30 18:19:07 -05:00
|
|
|
|
2011-12-30 19:10:53 -05:00
|
|
|
def test_some_time
|
|
|
|
fp = FakePool.new
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not fp.reaped
|
2011-12-30 19:10:53 -05:00
|
|
|
|
|
|
|
reaper = ConnectionPool::Reaper.new(fp, 0.0001)
|
|
|
|
reaper.run
|
2019-01-02 17:55:48 -05:00
|
|
|
until fp.flushed
|
2011-12-30 19:10:53 -05:00
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
assert fp.reaped
|
2017-11-25 00:35:13 -05:00
|
|
|
assert fp.flushed
|
2019-08-21 03:34:47 -04:00
|
|
|
ensure
|
|
|
|
fp.discard!
|
2011-12-30 18:19:07 -05:00
|
|
|
end
|
2011-12-30 18:27:41 -05:00
|
|
|
|
|
|
|
def test_pool_has_reaper
|
2019-09-13 17:44:32 -04:00
|
|
|
config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", spec_name: "primary")
|
2019-11-05 17:05:54 -05:00
|
|
|
pool_config = PoolConfig.new("primary", config)
|
|
|
|
pool = ConnectionPool.new(pool_config)
|
2019-08-21 03:34:47 -04:00
|
|
|
|
2011-12-30 18:27:41 -05:00
|
|
|
assert pool.reaper
|
2019-08-21 03:34:47 -04:00
|
|
|
ensure
|
|
|
|
pool.discard!
|
2011-12-30 18:27:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reaping_frequency_configuration
|
2019-11-05 17:05:54 -05:00
|
|
|
pool_config = duplicated_pool_config
|
|
|
|
pool_config.db_config.configuration_hash[:reaping_frequency] = "10.01"
|
2019-09-13 17:44:32 -04:00
|
|
|
|
2019-11-05 17:05:54 -05:00
|
|
|
pool = ConnectionPool.new(pool_config)
|
2019-09-13 17:44:32 -04:00
|
|
|
|
2018-08-18 22:29:02 -04:00
|
|
|
assert_equal 10.01, pool.reaper.frequency
|
2019-08-21 03:34:47 -04:00
|
|
|
ensure
|
|
|
|
pool.discard!
|
2011-12-30 18:27:41 -05:00
|
|
|
end
|
2011-12-30 18:39:39 -05:00
|
|
|
|
|
|
|
def test_connection_pool_starts_reaper
|
2019-11-05 17:05:54 -05:00
|
|
|
pool_config = duplicated_pool_config
|
|
|
|
pool_config.db_config.configuration_hash[:reaping_frequency] = "0.0001"
|
2011-12-30 18:39:39 -05:00
|
|
|
|
2019-11-05 17:05:54 -05:00
|
|
|
pool = ConnectionPool.new(pool_config)
|
2011-12-30 18:39:39 -05:00
|
|
|
|
2019-08-19 21:38:09 -04:00
|
|
|
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?
|
2019-08-21 03:34:47 -04:00
|
|
|
ensure
|
|
|
|
pool.discard!
|
2019-08-19 21:38:09 -04:00
|
|
|
end
|
|
|
|
|
2019-08-20 14:45:12 -04:00
|
|
|
def test_reaper_works_after_pool_discard
|
2019-11-05 17:05:54 -05:00
|
|
|
pool_config = duplicated_pool_config
|
|
|
|
pool_config.db_config.configuration_hash[:reaping_frequency] = "0.0001"
|
2019-08-20 14:45:12 -04:00
|
|
|
|
|
|
|
2.times do
|
2019-11-05 17:05:54 -05:00
|
|
|
pool = ConnectionPool.new(pool_config)
|
2019-08-20 14:45:12 -04:00
|
|
|
|
|
|
|
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
|
2019-11-05 17:05:54 -05:00
|
|
|
pool_config = duplicated_pool_config
|
|
|
|
pool = ConnectionPool.new(pool_config)
|
2019-08-20 14:45:12 -04:00
|
|
|
|
|
|
|
pool.discard!
|
|
|
|
pool.reap
|
|
|
|
pool.flush
|
|
|
|
end
|
|
|
|
|
2019-08-19 21:38:09 -04:00
|
|
|
def test_connection_pool_starts_reaper_in_fork
|
2019-11-05 17:05:54 -05:00
|
|
|
pool_config = duplicated_pool_config
|
|
|
|
pool_config.db_config.configuration_hash[:reaping_frequency] = "0.0001"
|
2019-08-19 21:38:09 -04:00
|
|
|
|
2019-11-05 17:05:54 -05:00
|
|
|
pool = ConnectionPool.new(pool_config)
|
2019-08-19 21:38:09 -04:00
|
|
|
pool.checkout
|
|
|
|
|
|
|
|
pid = fork do
|
2019-11-05 17:05:54 -05:00
|
|
|
pool = ConnectionPool.new(pool_config)
|
2019-08-19 21:38:09 -04:00
|
|
|
|
|
|
|
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?
|
2019-08-21 03:34:47 -04:00
|
|
|
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!
|
2019-08-19 21:38:09 -04:00
|
|
|
end
|
|
|
|
|
2019-09-13 17:44:32 -04:00
|
|
|
private
|
2019-11-05 17:05:54 -05:00
|
|
|
def duplicated_pool_config
|
2019-09-18 06:26:59 -04:00
|
|
|
old_config = ActiveRecord::Base.connection_pool.db_config.configuration_hash
|
2019-09-13 17:44:32 -04:00
|
|
|
db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new("arunit", "primary", old_config.dup)
|
2019-11-05 17:05:54 -05:00
|
|
|
PoolConfig.new("primary", db_config)
|
2014-03-07 08:36:09 -05:00
|
|
|
end
|
2011-12-30 18:39:39 -05:00
|
|
|
|
2019-09-13 17:44:32 -04:00
|
|
|
def new_conn_in_thread(pool)
|
|
|
|
event = Concurrent::Event.new
|
|
|
|
conn = nil
|
2011-12-30 18:39:39 -05:00
|
|
|
|
2019-09-13 17:44:32 -04:00
|
|
|
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
|
2011-12-30 19:10:53 -05:00
|
|
|
end
|
2011-12-30 18:19:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|