diff options
Diffstat (limited to 'spec/initializers/database_config_spec.rb')
-rw-r--r-- | spec/initializers/database_config_spec.rb | 57 |
1 files changed, 21 insertions, 36 deletions
diff --git a/spec/initializers/database_config_spec.rb b/spec/initializers/database_config_spec.rb index f1b353d4012..5ddfbd64c23 100644 --- a/spec/initializers/database_config_spec.rb +++ b/spec/initializers/database_config_spec.rb @@ -21,37 +21,31 @@ RSpec.describe 'Database config initializer' do let(:max_threads) { 8 } - context "no existing pool size is set" do - before do - stub_database_config(pool_size: nil) - end + it 'retains the correct database name for the connection' do + previous_db_name = Gitlab::Database.main.scope.connection.pool.db_config.name - it "sets it based on the max number of worker threads" do - expect { subject }.to change { Gitlab::Database.config['pool'] }.from(nil).to(18) + subject - expect(ActiveRecord::Base.connection_db_config.pool).to eq(18) - end + expect(Gitlab::Database.main.scope.connection.pool.db_config.name).to eq(previous_db_name) end - context "the existing pool size is smaller than the max number of worker threads" do - before do - stub_database_config(pool_size: 1) - end + context 'when no custom headroom is specified' do + it 'sets the pool size based on the number of worker threads' do + old = ActiveRecord::Base.connection_db_config.pool - it "sets it based on the max number of worker threads" do - expect { subject }.to change { Gitlab::Database.config['pool'] }.from(1).to(18) + expect(old).not_to eq(18) - expect(ActiveRecord::Base.connection_db_config.pool).to eq(18) + expect { subject } + .to change { ActiveRecord::Base.connection_db_config.pool } + .from(old) + .to(18) end - end - context "and the existing pool size is larger than the max number of worker threads" do - before do - stub_database_config(pool_size: 100) - end + it 'overwrites custom pool settings' do + config = Gitlab::Database.main.config.merge(pool: 42) - it "sets it based on the max number of worker threads" do - expect { subject }.to change { Gitlab::Database.config['pool'] }.from(100).to(18) + allow(Gitlab::Database.main).to receive(:config).and_return(config) + subject expect(ActiveRecord::Base.connection_db_config.pool).to eq(18) end @@ -61,25 +55,16 @@ RSpec.describe 'Database config initializer' do let(:headroom) { 15 } before do - stub_database_config(pool_size: 1) stub_env("DB_POOL_HEADROOM", headroom) end it "adds headroom on top of the calculated size" do - expect { subject }.to change { Gitlab::Database.config['pool'] } - .from(1) - .to(max_threads + headroom) + old = ActiveRecord::Base.connection_db_config.pool - expect(ActiveRecord::Base.connection_db_config.pool).to eq(max_threads + headroom) + expect { subject } + .to change { ActiveRecord::Base.connection_db_config.pool } + .from(old) + .to(23) end end - - def stub_database_config(pool_size:) - original_config = Gitlab::Database.config - - config = original_config.dup - config['pool'] = pool_size - - allow(Gitlab::Database).to receive(:config).and_return(config) - end end |