2019-10-30 08:06:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe 'Database config initializer' do
|
2019-10-30 08:06:34 -04:00
|
|
|
subject do
|
|
|
|
load Rails.root.join('config/initializers/database_config.rb')
|
|
|
|
end
|
|
|
|
|
2021-06-15 11:10:04 -04:00
|
|
|
around do |example|
|
|
|
|
original_config = ActiveRecord::Base.connection_db_config
|
|
|
|
|
|
|
|
example.run
|
|
|
|
|
|
|
|
ActiveRecord::Base.establish_connection(original_config)
|
|
|
|
end
|
|
|
|
|
2019-10-30 08:06:34 -04:00
|
|
|
before do
|
2020-07-29 23:09:32 -04:00
|
|
|
allow(Gitlab::Runtime).to receive(:max_threads).and_return(max_threads)
|
2019-10-30 08:06:34 -04:00
|
|
|
end
|
|
|
|
|
2020-07-29 23:09:32 -04:00
|
|
|
let(:max_threads) { 8 }
|
2019-10-30 08:06:34 -04:00
|
|
|
|
2021-07-23 11:09:21 -04:00
|
|
|
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
|
2020-06-11 14:08:28 -04:00
|
|
|
|
2021-07-23 11:09:21 -04:00
|
|
|
expect(old).not_to eq(18)
|
2021-06-15 11:10:04 -04:00
|
|
|
|
2021-07-23 11:09:21 -04:00
|
|
|
expect { subject }
|
|
|
|
.to change { ActiveRecord::Base.connection_db_config.pool }
|
|
|
|
.from(old)
|
|
|
|
.to(18)
|
2020-06-11 14:08:28 -04:00
|
|
|
end
|
2019-10-30 08:06:34 -04:00
|
|
|
|
2021-07-23 11:09:21 -04:00
|
|
|
it 'overwrites custom pool settings' do
|
2021-07-29 11:09:48 -04:00
|
|
|
config = Gitlab::Database.main.config.merge(pool: 42)
|
2020-07-22 17:09:50 -04:00
|
|
|
|
2021-07-23 11:09:21 -04:00
|
|
|
allow(Gitlab::Database.main).to receive(:config).and_return(config)
|
|
|
|
subject
|
2021-06-15 11:10:04 -04:00
|
|
|
|
|
|
|
expect(ActiveRecord::Base.connection_db_config.pool).to eq(18)
|
2020-07-22 17:09:50 -04:00
|
|
|
end
|
2020-07-29 23:09:32 -04:00
|
|
|
end
|
2020-07-22 17:09:50 -04:00
|
|
|
|
2020-07-29 23:09:32 -04:00
|
|
|
context "when specifying headroom through an ENV variable" do
|
|
|
|
let(:headroom) { 15 }
|
2020-07-22 17:09:50 -04:00
|
|
|
|
2020-07-29 23:09:32 -04:00
|
|
|
before do
|
|
|
|
stub_env("DB_POOL_HEADROOM", headroom)
|
2020-07-22 17:09:50 -04:00
|
|
|
end
|
|
|
|
|
2020-07-29 23:09:32 -04:00
|
|
|
it "adds headroom on top of the calculated size" do
|
2021-07-23 11:09:21 -04:00
|
|
|
old = ActiveRecord::Base.connection_db_config.pool
|
2021-06-15 11:10:04 -04:00
|
|
|
|
2021-07-23 11:09:21 -04:00
|
|
|
expect { subject }
|
|
|
|
.to change { ActiveRecord::Base.connection_db_config.pool }
|
|
|
|
.from(old)
|
|
|
|
.to(23)
|
2020-07-22 17:09:50 -04:00
|
|
|
end
|
|
|
|
end
|
2019-10-30 08:06:34 -04:00
|
|
|
end
|