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

metrics_spec.rb « cache « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d8103837708ab84ee3cc2ce090bce631a596c34e (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Cache::Metrics do
  subject(:metrics) do
    described_class.new(
      caller_id: caller_id,
      cache_identifier: cache_identifier,
      feature_category: feature_category,
      backing_resource: backing_resource
    )
  end

  let(:caller_id) { 'caller-id' }
  let(:cache_identifier) { 'ApplicationController#show' }
  let(:feature_category) { :source_code_management }
  let(:backing_resource) { :unknown }

  let(:counter_mock) { instance_double(Prometheus::Client::Counter) }

  before do
    allow(Gitlab::Metrics).to receive(:counter)
      .with(
        :redis_hit_miss_operations_total,
        'Hit/miss Redis cache counter'
      ).and_return(counter_mock)
  end

  describe '#initialize' do
    context 'when backing resource is not supported' do
      let(:backing_resource) { 'foo' }

      it { expect { metrics }.to raise_error(RuntimeError) }

      context 'when on production' do
        before do
          allow(Gitlab).to receive(:dev_or_test_env?).and_return(false)
        end

        it 'does not raise an exception' do
          expect { metrics }.not_to raise_error
        end
      end
    end
  end

  describe '#increment_cache_hit' do
    subject { metrics.increment_cache_hit }

    it 'increments number of hits' do
      expect(counter_mock)
        .to receive(:increment)
        .with(
          {
            caller_id: caller_id,
            cache_identifier: cache_identifier,
            feature_category: feature_category,
            backing_resource: backing_resource,
            cache_hit: true
          }
        ).once

      subject
    end
  end

  describe '#increment_cache_miss' do
    subject { metrics.increment_cache_miss }

    it 'increments number of misses' do
      expect(counter_mock)
        .to receive(:increment)
        .with(
          {
            caller_id: caller_id,
            cache_identifier: cache_identifier,
            feature_category: feature_category,
            backing_resource: backing_resource,
            cache_hit: false
          }
        ).once

      subject
    end
  end

  describe '#observe_cache_generation' do
    subject do
      metrics.observe_cache_generation { action }
    end

    let(:action) { 'action' }
    let(:histogram_mock) { instance_double(Prometheus::Client::Histogram) }

    before do
      allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(100.0, 500.0)
    end

    it 'updates histogram metric' do
      expect(Gitlab::Metrics).to receive(:histogram).with(
        :redis_cache_generation_duration_seconds,
        'Duration of Redis cache generation',
        {
          caller_id: caller_id,
          cache_identifier: cache_identifier,
          feature_category: feature_category,
          backing_resource: backing_resource
        },
        [0, 1, 5]
      ).and_return(histogram_mock)

      expect(histogram_mock).to receive(:observe).with({}, 400.0)

      is_expected.to eq(action)
    end
  end
end