summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/subscribers/load_balancing_spec.rb
blob: 7f7efaffd9e8c53b572c1fae46c488b0271070ce (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::Subscribers::LoadBalancing, :request_store do
  let(:subscriber) { described_class.new }

  describe '#caught_up_replica_pick' do
    shared_examples 'having payload result value' do |result, counter_name|
      subject { subscriber.caught_up_replica_pick(event) }

      let(:payload) { { result: result } }

      let(:event) do
        double(
          :event,
          name: 'load_balancing.caught_up_replica_pick',
          payload: payload
        )
      end

      it 'stores per-request caught up replica search result' do
        subject

        expect(Gitlab::SafeRequestStore[counter_name]).to eq(1)
      end
    end

    it_behaves_like 'having payload result value', true, :caught_up_replica_pick_ok
    it_behaves_like 'having payload result value', false, :caught_up_replica_pick_fail
  end

  describe "#web_transaction_completed" do
    subject { subscriber.web_transaction_completed(event) }

    let(:event) do
      double(
        :event,
        name: 'load_balancing.web_transaction_completed',
        payload: {}
      )
    end

    let(:web_transaction) { double('Gitlab::Metrics::WebTransaction') }

    before do
      allow(::Gitlab::Metrics::WebTransaction).to receive(:current)
        .and_return(web_transaction)
    end

    context 'when no data in request store' do
      before do
        Gitlab::SafeRequestStore[:caught_up_replica_pick] = nil
      end

      it 'does not change the counters' do
        expect(web_transaction).not_to receive(:increment)
      end
    end

    context 'when request store was updated' do
      before do
        Gitlab::SafeRequestStore[:caught_up_replica_pick_ok] = 2
        Gitlab::SafeRequestStore[:caught_up_replica_pick_fail] = 1
      end

      it 'increments :caught_up_replica_pick count with proper label' do
        expect(web_transaction).to receive(:increment).with(:gitlab_transaction_caught_up_replica_pick_count_total, 2, { result: true })
        expect(web_transaction).to receive(:increment).with(:gitlab_transaction_caught_up_replica_pick_count_total, 1, { result: false })

        subject
      end
    end
  end

  describe '.load_balancing_payload' do
    subject { described_class.load_balancing_payload }

    context 'when no data in request store' do
      before do
        Gitlab::SafeRequestStore[:caught_up_replica_pick_ok] = nil
        Gitlab::SafeRequestStore[:caught_up_replica_pick_fail] = nil
      end

      it 'returns empty hash' do
        expect(subject).to eq({})
      end
    end

    context 'when request store was updated for a single counter' do
      before do
        Gitlab::SafeRequestStore[:caught_up_replica_pick_ok] = 2
      end

      it 'returns proper payload with only that counter' do
        expect(subject).to eq({ caught_up_replica_pick_ok: 2 })
      end
    end

    context 'when both counters were updated' do
      before do
        Gitlab::SafeRequestStore[:caught_up_replica_pick_ok] = 2
        Gitlab::SafeRequestStore[:caught_up_replica_pick_fail] = 1
      end

      it 'return proper payload' do
        expect(subject).to eq({ caught_up_replica_pick_ok: 2, caught_up_replica_pick_fail: 1 })
      end
    end
  end
end