2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe 'Database config initializer' do
|
2019-12-26 22:10:19 +05:30
|
|
|
subject do
|
|
|
|
load Rails.root.join('config/initializers/database_config.rb')
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(ActiveRecord::Base).to receive(:establish_connection)
|
2020-10-24 23:57:45 +05:30
|
|
|
allow(Gitlab::Runtime).to receive(:max_threads).and_return(max_threads)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:max_threads) { 8 }
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
context "no existing pool size is set" do
|
2019-12-26 22:10:19 +05:30
|
|
|
before do
|
2020-10-24 23:57:45 +05:30
|
|
|
stub_database_config(pool_size: nil)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it "sets it based on the max number of worker threads" do
|
|
|
|
expect { subject }.to change { Gitlab::Database.config['pool'] }.from(nil).to(18)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
context "the existing pool size is smaller than the max number of worker threads" do
|
|
|
|
before do
|
|
|
|
stub_database_config(pool_size: 1)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it "sets it based on the max number of worker threads" do
|
|
|
|
expect { subject }.to change { Gitlab::Database.config['pool'] }.from(1).to(18)
|
|
|
|
end
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
context "and the existing pool size is larger than the max number of worker threads" do
|
|
|
|
before do
|
|
|
|
stub_database_config(pool_size: 100)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it "sets it based on the max number of worker threads" do
|
|
|
|
expect { subject }.to change { Gitlab::Database.config['pool'] }.from(100).to(18)
|
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
context "when specifying headroom through an ENV variable" do
|
|
|
|
let(:headroom) { 15 }
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
before do
|
|
|
|
stub_database_config(pool_size: 1)
|
|
|
|
stub_env("DB_POOL_HEADROOM", headroom)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it "adds headroom on top of the calculated size" do
|
|
|
|
expect { subject }.to change { Gitlab::Database.config['pool'] }
|
|
|
|
.from(1)
|
|
|
|
.to(max_threads + headroom)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_database_config(pool_size:)
|
|
|
|
config = {
|
|
|
|
'adapter' => 'postgresql',
|
|
|
|
'host' => 'db.host.com',
|
|
|
|
'pool' => pool_size
|
|
|
|
}.compact
|
|
|
|
|
|
|
|
allow(Gitlab::Database).to receive(:config).and_return(config)
|
|
|
|
end
|
|
|
|
end
|