summaryrefslogtreecommitdiff
path: root/spec/initializers/database_config_spec.rb
blob: 5ddfbd64c2373afb97fc33d167bfcf4d24c53821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Database config initializer' do
  subject do
    load Rails.root.join('config/initializers/database_config.rb')
  end

  around do |example|
    original_config = ActiveRecord::Base.connection_db_config

    example.run

    ActiveRecord::Base.establish_connection(original_config)
  end

  before do
    allow(Gitlab::Runtime).to receive(:max_threads).and_return(max_threads)
  end

  let(:max_threads) { 8 }

  it 'retains the correct database name for the connection' do
    previous_db_name = Gitlab::Database.main.scope.connection.pool.db_config.name

    subject

    expect(Gitlab::Database.main.scope.connection.pool.db_config.name).to eq(previous_db_name)
  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

      expect(old).not_to eq(18)

      expect { subject }
        .to change { ActiveRecord::Base.connection_db_config.pool }
        .from(old)
        .to(18)
    end

    it 'overwrites custom pool settings' do
      config = Gitlab::Database.main.config.merge(pool: 42)

      allow(Gitlab::Database.main).to receive(:config).and_return(config)
      subject

      expect(ActiveRecord::Base.connection_db_config.pool).to eq(18)
    end
  end

  context "when specifying headroom through an ENV variable" do
    let(:headroom) { 15 }

    before do
      stub_env("DB_POOL_HEADROOM", headroom)
    end

    it "adds headroom on top of the calculated size" do
      old = ActiveRecord::Base.connection_db_config.pool

      expect { subject }
        .to change { ActiveRecord::Base.connection_db_config.pool }
        .from(old)
        .to(23)
    end
  end
end