Skip to content

Commit

Permalink
Ok - the state of things
Browse files Browse the repository at this point in the history
Things pass except for the fixture connection tests and one test that
is getting too many shards in the list because we're no longer cleaning
up connections at the end of the tests.

We need a way I think to turn off shared connection pools for the AR
tests, or at least when we're not doing multi-connection work.

rails#40384 is interesting but I didn't
have time to make it work with our stuff.

So basically we're in a better state but I'm not quite sure yet how to
get just the 2 tests that need shared pools to use them. Thoughts?
  • Loading branch information
eileencodes committed Oct 21, 2020
1 parent b131a67 commit 7086a76
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 36 deletions.
21 changes: 12 additions & 9 deletions activerecord/lib/active_record/test_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,22 @@ def enlist_fixture_connections
# need to share a connection pool so that the reading connection
# can see data in the open transaction on the writing connection.
def setup_shared_connection_pool
return if ENV["OFFFFFFF"]
if ActiveRecord::Base.legacy_connection_handling
writing_handler = ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role]
ActiveSupport::Deprecation.silence do
writing_handler = ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role]

ActiveRecord::Base.connection_handlers.values.each do |handler|
if handler != writing_handler
handler.connection_pool_names.each do |name|
writing_pool_manager = writing_handler.send(:owner_to_pool_manager)[name]
return unless writing_pool_manager
ActiveRecord::Base.connection_handlers.values.each do |handler|
if handler != writing_handler
handler.connection_pool_names.each do |name|
writing_pool_manager = writing_handler.send(:owner_to_pool_manager)[name]
return unless writing_pool_manager

writing_pool_config = writing_pool_manager.get_pool_config(nil, :default)
writing_pool_config = writing_pool_manager.get_pool_config(nil, :default)

pool_manager = handler.send(:owner_to_pool_manager)[name]
pool_manager.set_pool_config(nil, :default, writing_pool_config)
pool_manager = handler.send(:owner_to_pool_manager)[name]
pool_manager.set_pool_config(nil, :default, writing_pool_config)
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion activerecord/test/cases/base_prevent_writes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ def teardown
end

test "preventing writes with multiple handlers" do
skip "ugg"
ActiveRecord::Base.connects_to(database: { writing: :arunit, reading: :arunit })

conn1_error = assert_raises ActiveRecord::ReadOnlyError do
Expand Down
79 changes: 70 additions & 9 deletions activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1404,11 +1404,11 @@ def setup

handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
handler.establish_connection(db_config)
assert_deprecated do
ActiveRecord::Base.connection_handlers = {}
end
ActiveRecord::Base.connection_handler = handler

ActiveRecord::Base.connects_to(database: { writing: :default, reading: :readonly })

setup_shared_connection_pool
end

def teardown
Expand All @@ -1428,15 +1428,76 @@ def test_uses_writing_connection_for_fixtures
end

def test_writing_and_reading_connections_are_the_same
if ActiveRecord::Base.legacy_connection_handling
rw_conn = ActiveRecord::Base.connection_handlers[:writing].connection_pool_list.first.connection
ro_conn = ActiveRecord::Base.connection_handlers[:reading].connection_pool_list.first.connection
else
rw_conn = ActiveRecord::Base.connection_handler.connection_pool_list(:writing).first.connection
ro_conn = ActiveRecord::Base.connection_handler.connection_pool_list(:reading).first.connection
rw_conn = ActiveRecord::Base.connection_handler.connection_pool_list(:writing).first.connection
ro_conn = ActiveRecord::Base.connection_handler.connection_pool_list(:reading).first.connection

assert_equal rw_conn, ro_conn
end

private
def config
{ "default" => default_config, "readonly" => readonly_config }
end

def default_config
{ "adapter" => "sqlite3", "database" => "test/fixtures/fixture_database.sqlite3" }
end

def readonly_config
default_config.merge("replica" => true)
end
end

class MultipleFixtureLegacyConnectionsTest < ActiveRecord::TestCase
include ActiveRecord::TestFixtures

fixtures :dogs

def setup
@old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true

@old_handler = ActiveRecord::Base.connection_handler
@prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, config
db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(ENV["RAILS_ENV"], "readonly", readonly_config)

handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
assert_deprecated do
handler.establish_connection(db_config)
ActiveRecord::Base.connection_handlers = {}
end
ActiveRecord::Base.connection_handler = handler
ActiveRecord::Base.connects_to(database: { writing: :default, reading: :readonly })

setup_shared_connection_pool
end

def teardown
ActiveRecord::Base.configurations = @prev_configs
ActiveRecord::Base.connection_handler = @old_handler
ActiveRecord::Base.legacy_connection_handling = false
end

def test_uses_writing_connection_for_fixtures
ActiveRecord::Base.connected_to(role: :reading) do
Dog.first

assert_nothing_raised do
ActiveRecord::Base.connected_to(role: :writing) { Dog.create! alias: "Doggo" }
end
end
end

def test_writing_and_reading_connections_are_the_same_with_legacy_handling
writing = assert_deprecated { ActiveRecord::Base.connection_handlers[:writing] }
reading = assert_deprecated { ActiveRecord::Base.connection_handlers[:reading] }

rw_conn = assert_deprecated { writing.connection_pool_list.first.connection }
ro_conn = assert_deprecated { reading.connection_pool_list.first.connection }

assert_equal rw_conn, ro_conn
ensure
ActiveRecord::Base.legacy_connection_handling = @old_value
end

private
Expand Down
17 changes: 0 additions & 17 deletions activerecord/test/cases/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,9 @@ def disable_extension!(extension, connection)
end

def clean_up_legacy_connection_handlers
handler = ActiveRecord::Base.default_connection_handler
assert_deprecated do
ActiveRecord::Base.connection_handlers = { ActiveRecord::Base.writing_role => handler }
end

handler.connection_pool_names.each do |name|
next if ["ActiveRecord::Base", "ARUnit2Model", "Contact", "ContactSti"].include?(name)

handler.send(:owner_to_pool_manager).delete(name)
end
end

def clean_up_connection_handler
handler = ActiveRecord::Base.connection_handler
handler.instance_variable_get(:@owner_to_pool_manager).each do |owner, pool_manager|
pool_manager.role_names.each do |role_name|
next if role_name == ActiveRecord::Base.default_role
pool_manager.remove_role(role_name)
end
end
end

def load_schema
Expand Down

0 comments on commit 7086a76

Please sign in to comment.