summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/connection_rack_middleware_spec.rb
blob: 12392b8dddadbbee529af676f4db679e7d8de7e4 (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
require 'spec_helper'

describe Gitlab::Metrics::ConnectionRackMiddleware do
  let(:app) { double('app') }
  subject { described_class.new(app) }

  around do |example|
    Timecop.freeze { example.run }
  end

  describe '#call' do
    let(:status) { 100 }
    let(:env) { { 'REQUEST_METHOD' => 'GET' } }
    let(:stack_result) { [status, {}, 'body'] }

    before do
      allow(app).to receive(:call).and_return(stack_result)
    end

    context '@app.call succeeds with 200' do
      before do
        allow(app).to receive(:call).and_return([200, nil, nil])
      end

      it 'increments requests count' do
        expect(described_class).to receive_message_chain(:rack_request_count, :increment).with(method: 'get')

        subject.call(env)
      end

      it 'measures execution time' do
        execution_time = 10
        allow(app).to receive(:call) do |*args|
          Timecop.freeze(execution_time.seconds)
        end

        expect(described_class).to receive_message_chain(:rack_execution_time, :observe).with({status: 200, method: 'get'}, execution_time)

        subject.call(env)
      end
    end

    context '@app.call throws exception' do
      let(:rack_response_count) { double('rack_response_count') }

      it 'increments exceptions count' do
        expect(described_class).to receive_message_chain(:rack_uncaught_errors_count, :increment)

        expect { subject.call(env) }.to raise_error(StandardError)
      end

      it 'increments requests count' do
        expect(described_class).to receive_message_chain(:rack_request_count, :increment).with(method: 'get')

        expect { subject.call(env) }.to raise_error(StandardError)
      end

      it "does't increment response count" do
        expect(described_class.rack_response_count).not_to receive(:increment)

        expect { subject.call(env) }.to raise_error(StandardError)
      end

      it 'measures execution time' do
        execution_time = 10
        allow(app).to receive(:call) do |*args|
          Timecop.freeze(execution_time.seconds)
          raise StandardError
        end

        expect(described_class).to receive_message_chain(:rack_execution_time, :observe).with({}, execution_time)

        expect { subject.call(env) }.to raise_error(StandardError)
      end
    end
  end
end