Welcome to mirror list, hosted at ThFree Co, Russian Federation.

database_sampler_spec.rb « samplers « metrics « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b94d19ff2272d0eb8c86cc6b3e1d46bd793361c2 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::Samplers::DatabaseSampler do
  subject { described_class.new }

  describe '#interval' do
    it 'samples every five seconds by default' do
      expect(subject.interval).to eq(5)
    end

    it 'samples at other intervals if requested' do
      expect(described_class.new(11).interval).to eq(11)
    end
  end

  describe '#sample' do
    before do
      described_class::METRIC_DESCRIPTIONS.each_key do |metric|
        allow(subject.metrics[metric]).to receive(:set)
      end
    end

    context 'for ActiveRecord::Base' do
      let(:labels) do
        {
          class: 'ActiveRecord::Base',
          host: Gitlab::Database.config['host'],
          port: Gitlab::Database.config['port']
        }
      end

      context 'when the database is connected' do
        it 'samples connection pool statistics' do
          expect(subject.metrics[:size]).to receive(:set).with(labels, a_value >= 1)
          expect(subject.metrics[:connections]).to receive(:set).with(labels, a_value >= 1)
          expect(subject.metrics[:busy]).to receive(:set).with(labels, a_value >= 1)
          expect(subject.metrics[:dead]).to receive(:set).with(labels, a_value >= 0)
          expect(subject.metrics[:waiting]).to receive(:set).with(labels, a_value >= 0)

          subject.sample
        end
      end

      context 'when the database is not connected' do
        before do
          allow(ActiveRecord::Base).to receive(:connected?).and_return(false)
        end

        it 'records no samples' do
          expect(subject.metrics[:size]).not_to receive(:set).with(labels, anything)

          subject.sample
        end
      end
    end
  end
end