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

context_spec.rb « transaction « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 33a47150060f29add7aabc44e584c2507ccbd821 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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 '#track_backtrace' do
    before do
      subject.track_backtrace(caller)
    end

    it { expect(data[:backtraces]).to be_a(Array) }
    it { expect(data[:backtraces]).to all(be_a(Array)) }
    it { expect(data[:backtraces].length).to eq(1) }
    it { expect(data[:backtraces][0][0]).to be_a(String) }

    it 'appends the backtrace' do
      subject.track_backtrace(caller)

      expect(data[:backtraces].length).to eq(2)
      expect(subject.backtraces).to be_a(Array)
      expect(subject.backtraces).to all(be_a(Array))
      expect(subject.backtraces[1][0]).to be_a(String)
    end
  end

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

    it { expect(subject.duration).to be >= 0 }
  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 savepoints count exceeds threshold' do
    before do
      data[:savepoints] = 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

  context 'when there are too many external HTTP requests' do
    before do
      allow(::Gitlab::Metrics::Subscribers::ExternalHttp)
        .to receive(:request_count)
        .and_return(100)
    end

    it_behaves_like 'logs transaction data'
  end

  context 'when there are too many too long external HTTP requests' do
    before do
      allow(::Gitlab::Metrics::Subscribers::ExternalHttp)
        .to receive(:duration)
        .and_return(5.5)
    end

    it_behaves_like 'logs transaction data'
  end
end