Skip to content

Commit

Permalink
Merge pull request #34773 from eileencodes/share-fixture-connections-…
Browse files Browse the repository at this point in the history
…with-multiple-handlers

For fixtures share the connection pool when there are multiple handlers
  • Loading branch information
eileencodes committed Jan 4, 2019
2 parents a5a22c4 + b24bfcc commit 725c642
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
23 changes: 23 additions & 0 deletions activerecord/lib/active_record/test_fixtures.rb
Expand Up @@ -173,10 +173,33 @@ def teardown_fixtures
end

def enlist_fixture_connections
setup_shared_connection_pool

ActiveRecord::Base.connection_handler.connection_pool_list.map(&:connection)
end

private

# Shares the writing connection pool with connections on
# other handlers.
#
# In an application with a primary and replica the test fixtures
# 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
writing_handler = ActiveRecord::Base.connection_handler

ActiveRecord::Base.connection_handlers.values.each do |handler|
if handler != writing_handler
handler.connection_pool_list.each do |pool|
name = pool.spec.name
writing_connection = writing_handler.retrieve_connection_pool(name)
handler.send(:owner_to_pool)[name] = writing_connection
end
end
end
end

def load_fixtures(config)
fixtures = ActiveRecord::FixtureSet.create_fixtures(fixture_path, fixture_table_names, fixture_class_names, config)
Hash[fixtures.map { |f| [f.name, f] }]
Expand Down
34 changes: 34 additions & 0 deletions activerecord/test/cases/fixtures_test.rb
Expand Up @@ -1362,3 +1362,37 @@ class NilFixturePathTest < ActiveRecord::TestCase
MSG
end
end

class MultipleDatabaseFixturesTest < ActiveRecord::TestCase
test "enlist_fixture_connections ensures multiple databases share a connection pool" do
with_temporary_connection_pool do
ActiveRecord::Base.connects_to database: { writing: :arunit, reading: :arunit2 }

rw_conn = ActiveRecord::Base.connection
ro_conn = ActiveRecord::Base.connection_handlers[:reading].connection_pool_list.first.connection

assert_not_equal rw_conn, ro_conn

enlist_fixture_connections

rw_conn = ActiveRecord::Base.connection
ro_conn = ActiveRecord::Base.connection_handlers[:reading].connection_pool_list.first.connection

assert_equal rw_conn, ro_conn
end
ensure
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.connection_handler }
end

private

def with_temporary_connection_pool
old_pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(ActiveRecord::Base.connection_specification_name)
new_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new ActiveRecord::Base.connection_pool.spec
ActiveRecord::Base.connection_handler.send(:owner_to_pool)["primary"] = new_pool

yield
ensure
ActiveRecord::Base.connection_handler.send(:owner_to_pool)["primary"] = old_pool
end
end

0 comments on commit 725c642

Please sign in to comment.