Skip to content

Commit

Permalink
Remove connection created in the test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rafaelfranca committed Sep 26, 2023
1 parent 05ed261 commit 5dfded9
Showing 1 changed file with 21 additions and 0 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 5dfded9

Please sign in to comment.