summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/transaction/context_spec.rb
blob: 65d52b4d0994199dc02688650cc5e8f4e529c684 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::Transaction::Context do
  subject { described_class.new }

  let(:data) { subject.context }

  before do
    stub_const("#{described_class}::LOG_THROTTLE", 100)
  end

  describe '#set_start_time' do
    before do
      subject.set_start_time
    end

    it 'sets start_time' do
      expect(data).to have_key(:start_time)
    end
  end

  describe '#increment_savepoints' do
    before do
      2.times { subject.increment_savepoints }
    end

    it { expect(data[:savepoints]).to eq(2) }
  end

  describe '#increment_rollbacks' do
    before do
      3.times { subject.increment_rollbacks }
    end

    it { expect(data[:rollbacks]).to eq(3) }
  end

  describe '#increment_releases' do
    before do
      4.times { subject.increment_releases }
    end

    it { expect(data[:releases]).to eq(4) }
  end

  describe '#set_depth' do
    before do
      subject.set_depth(2)
    end

    it { expect(data[:depth]).to eq(2) }
  end

  describe '#track_sql' do
    before do
      subject.track_sql('SELECT 1')
      subject.track_sql('SELECT * FROM users')
    end

    it { expect(data[:queries]).to eq(['SELECT 1', 'SELECT * FROM users']) }
  end

  describe '#duration' do
    before do
      subject.set_start_time
    end

    it { expect(subject.duration).to be >= 0 }
  end

  context 'when depth is low' do
    it 'does not log data upon COMMIT' do
      expect(subject).not_to receive(:application_info)

      subject.commit
    end

    it 'does not log data upon ROLLBACK' do
      expect(subject).not_to receive(:application_info)

      subject.rollback
    end

    it '#should_log? returns false' do
      expect(subject.should_log?).to be false
    end
  end

  shared_examples 'logs transaction data' do
    it 'logs once upon COMMIT' do
      expect(subject).to receive(:application_info).and_call_original

      2.times { subject.commit }
    end

    it 'logs once upon ROLLBACK' do
      expect(subject).to receive(:application_info).once

      2.times { subject.rollback }
    end

    it 'logs again when log throttle duration passes' do
      expect(subject).to receive(:application_info).twice.and_call_original

      2.times { subject.commit }

      data[:last_log_timestamp] -= (described_class::LOG_THROTTLE_DURATION + 1)

      subject.commit
    end

    it '#should_log? returns true' do
      expect(subject.should_log?).to be true
    end
  end

  context 'when depth exceeds threshold' do
    before do
      subject.set_depth(described_class::LOG_DEPTH_THRESHOLD + 1)
    end

    it_behaves_like 'logs transaction data'
  end

  context 'when savepoints count exceeds threshold' do
    before do
      data[:savepoints] = described_class::LOG_SAVEPOINTS_THRESHOLD + 1
    end

    it_behaves_like 'logs transaction data'
  end

  context 'when duration exceeds threshold' do
    before do
      subject.set_start_time

      data[:start_time] -= (described_class::LOG_DURATION_S_THRESHOLD + 1)
    end

    it_behaves_like 'logs transaction data'
  end
end