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

describe Gitlab::Metrics::Subscribers::ActiveRecord do
  let(:transaction) { Gitlab::Metrics::Transaction.new }
  let(:subscriber)  { described_class.new }

  let(:event) do
    double(:event, duration: 0.2,
                   payload:  { sql: 'SELECT * FROM users WHERE id = 10' })
  end

  describe '#sql' do
    describe 'without a current transaction' do
      it 'simply returns' do
        expect_any_instance_of(Gitlab::Metrics::Transaction).
          to_not receive(:increment)

        subscriber.sql(event)
      end
    end

    describe 'with a current transaction' do
      it 'increments the :sql_duration value' do
        expect(subscriber).to receive(:current_transaction).
          at_least(:once).
          and_return(transaction)

        expect(transaction).to receive(:increment).
          with(:sql_duration, 0.2)

        expect(transaction).to receive(:increment).
          with(:sql_count, 1)

        subscriber.sql(event)
      end
    end
  end
end