diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-15 12:07:58 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-15 12:07:58 +0000 |
commit | f26f31d2fd8e3c91677e2629293b9e0562c1437a (patch) | |
tree | 5a689004863bee4374c16920663221ea81afe664 /spec/lib/initializer_connections_spec.rb | |
parent | 50e177d19bdeeb0fcc7b129b9c30841454df240b (diff) | |
download | gitlab-ce-f26f31d2fd8e3c91677e2629293b9e0562c1437a.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/initializer_connections_spec.rb')
-rw-r--r-- | spec/lib/initializer_connections_spec.rb | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/spec/lib/initializer_connections_spec.rb b/spec/lib/initializer_connections_spec.rb index 4ca283c4f22..e69aa0aa821 100644 --- a/spec/lib/initializer_connections_spec.rb +++ b/spec/lib/initializer_connections_spec.rb @@ -3,15 +3,20 @@ require 'spec_helper' RSpec.describe InitializerConnections do - describe '.with_disabled_database_connections', :reestablished_active_record_base do + describe '.raise_if_new_database_connection', :reestablished_active_record_base do + before do + ActiveRecord::Base.connection_handler.clear_active_connections! + ActiveRecord::Base.connection_handler.flush_idle_connections! + end + def block_with_database_call - described_class.with_disabled_database_connections do + described_class.raise_if_new_database_connection do Project.first end end def block_with_error - described_class.with_disabled_database_connections do + described_class.raise_if_new_database_connection do raise "oops, an error" end end @@ -20,6 +25,12 @@ RSpec.describe InitializerConnections do expect { block_with_database_call }.to raise_error(/Database connection should not be called during initializer/) end + it 'prevents any database connection re-use within the block' do + Project.connection # establish a connection first, it will be used inside the block + + expect { block_with_database_call }.to raise_error(/Database connection should not be called during initializer/) + end + it 'does not prevent database connection if SKIP_RAISE_ON_INITIALIZE_CONNECTIONS is set' do stub_env('SKIP_RAISE_ON_INITIALIZE_CONNECTIONS', '1') @@ -33,31 +44,34 @@ RSpec.describe InitializerConnections do end it 'restores original connection handler' do - # rubocop:disable Database/MultipleDatabases original_handler = ActiveRecord::Base.connection_handler expect { block_with_database_call }.to raise_error(/Database connection should not be called during initializer/) expect(ActiveRecord::Base.connection_handler).to eq(original_handler) - # rubocop:enabled Database/MultipleDatabases end it 'restores original connection handler even there is an error' do - # rubocop:disable Database/MultipleDatabases original_handler = ActiveRecord::Base.connection_handler expect { block_with_error }.to raise_error(/an error/) expect(ActiveRecord::Base.connection_handler).to eq(original_handler) - # rubocop:enabled Database/MultipleDatabases end - it 'raises if any new connection_pools are established in the block' do + it 'does not raise if connection_pool is retrieved in the block' do + # connection_pool, connection_db_config doesn't connect to database, so it's OK + expect do + described_class.raise_if_new_database_connection do + ApplicationRecord.connection_pool + end + end.not_to raise_error + expect do - described_class.with_disabled_database_connections do - ApplicationRecord.connects_to database: { writing: :main, reading: :main } + described_class.raise_if_new_database_connection do + Ci::ApplicationRecord.connection_pool end - end.to raise_error(/Unxpected connection_pools/) + end.not_to raise_error end end end |