From 5dfded9e3350b1b58de7d629774b0bd2636ba080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 26 Sep 2023 15:58:51 +0000 Subject: [PATCH] Remove connection created in the test This will avoid those connection leaking to other tests. They cause problem because the fixtures loading will try to open a transaction on all the active connections and since those connections use a database pointing to a temp file that doesn't exist anymore if any transaction try to start on them it will fail. Fixes #49373. --- .../connection_handlers_sharding_db_test.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/activerecord/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb b/activerecord/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb index 1e887c0d8b4fe..bc7c755d6a817 100644 --- a/activerecord/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb @@ -404,6 +404,7 @@ def test_swapping_shards_globally_in_a_multi_threaded_environment thread.join ensure + remove_connections(SecondaryBase, roles: [:writing], shards: [:default, :one]) tf_shard_one.close tf_shard_one.unlink tf_default.close @@ -454,6 +455,7 @@ def test_swapping_shards_and_roles_in_a_multi_threaded_environment thread.join ensure + remove_connections(SecondaryBase, roles: [:writing, :secondary], shards: [:default, :one]) tf_shard_one.close tf_shard_one.unlink tf_default.close @@ -528,6 +530,8 @@ def test_swapping_granular_shards_and_roles_in_a_multi_threaded_environment thread.join ensure + remove_connections(SecondaryBase, roles: [:writing, :secondary], shards: [:default, :one]) + remove_connections(SomeOtherBase, roles: [:writing, :secondary], shards: [:default, :one]) tf_shard_one.close tf_shard_one.unlink tf_default.close @@ -545,6 +549,23 @@ def test_swapping_granular_shards_and_roles_in_a_multi_threaded_environment tf_default_reading2.close tf_default_reading2.unlink end + + private + def remove_connections(klass, roles:, shards:) + # TODO: Remove this helper when `remove_connection` for different shards is fixed. + # See https://github.com/rails/rails/pull/49382. + name = klass.name + + roles.each do |role| + shards.each do |shard| + ActiveRecord::Base.connection_handler.remove_connection_pool( + name, + role: role, + shard: shard + ) + end + end + end end end end