blob: 331c3c1d8b00660072310dbfa075327413870581 (
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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::QueryLimiting::Transaction do
after do
Thread.current[described_class::THREAD_KEY] = nil
end
describe '.current' do
it 'returns nil when there is no transaction' do
expect(described_class.current).to be_nil
end
it 'returns the transaction when present' do
Thread.current[described_class::THREAD_KEY] = described_class.new
expect(described_class.current).to be_an_instance_of(described_class)
end
end
describe '.run' do
it 'runs a transaction and returns it and its return value' do
trans, ret = described_class.run do
10
end
expect(trans).to be_an_instance_of(described_class)
expect(ret).to eq(10)
end
it 'removes the transaction from the current thread upon completion' do
described_class.run do
10
end
expect(Thread.current[described_class::THREAD_KEY]).to be_nil
end
end
describe '#act_upon_results' do
context 'when the query threshold is not exceeded' do
it 'does nothing' do
trans = described_class.new
expect(trans).not_to receive(:raise)
trans.act_upon_results
end
end
context 'when the query threshold is exceeded' do
let(:transaction) do
trans = described_class.new
trans.count = described_class::THRESHOLD + 1
trans
end
it 'raises an error when this is enabled' do
expect { transaction.act_upon_results }
.to raise_error(described_class::ThresholdExceededError)
end
end
end
describe '#increment' do
it 'increments the number of executed queries' do
transaction = described_class.new
expect(transaction.count).to be_zero
transaction.increment
expect(transaction.count).to eq(1)
end
end
describe '#raise_error?' do
it 'returns true in a test environment' do
transaction = described_class.new
expect(transaction.raise_error?).to eq(true)
end
it 'returns false in a production environment' do
transaction = described_class.new
stub_rails_env('production')
expect(transaction.raise_error?).to eq(false)
end
end
describe '#threshold_exceeded?' do
it 'returns false when the threshold is not exceeded' do
transaction = described_class.new
expect(transaction.threshold_exceeded?).to eq(false)
end
it 'returns true when the threshold is exceeded' do
transaction = described_class.new
transaction.count = described_class::THRESHOLD + 1
expect(transaction.threshold_exceeded?).to eq(true)
end
end
describe '#error_message' do
it 'returns the error message to display when the threshold is exceeded' do
transaction = described_class.new
transaction.count = max = described_class::THRESHOLD
expect(transaction.error_message).to eq(
"Too many SQL queries were executed: a maximum of #{max} " \
"is allowed but #{max} SQL queries were executed"
)
end
it 'includes the action name in the error message when present' do
transaction = described_class.new
transaction.count = max = described_class::THRESHOLD
transaction.action = 'UsersController#show'
expect(transaction.error_message).to eq(
"Too many SQL queries were executed in UsersController#show: " \
"a maximum of #{max} is allowed but #{max} SQL queries were executed"
)
end
end
end
|