2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::QueryLimiting::Transaction do
|
2018-03-17 18:26:18 +05:30
|
|
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
trans.count = described_class.threshold + 1
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
expect { transaction.increment }.to change { transaction.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not increment the number of executed queries when query limiting is disabled' do
|
|
|
|
transaction = described_class.new
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
allow(transaction).to receive(:enabled?).and_return(false)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
expect { transaction.increment }.not_to change { transaction.count }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
it 'does not increment the number of executed queries when the query is known to be ignorable' do
|
|
|
|
transaction = described_class.new
|
|
|
|
|
|
|
|
expect do
|
|
|
|
transaction.increment(described_class::GEO_NODES_LOAD)
|
|
|
|
transaction.increment(described_class::LICENSES_LOAD)
|
|
|
|
transaction.increment('SELECT a.attname, a.other_column FROM pg_attribute a')
|
|
|
|
transaction.increment('SELECT x.foo, a.attname FROM some_table x JOIN pg_attribute a')
|
|
|
|
transaction.increment(<<-SQL)
|
|
|
|
SELECT a.attname, a.other_column
|
|
|
|
FROM pg_attribute a
|
|
|
|
SQL
|
2023-01-13 00:05:48 +05:30
|
|
|
transaction.increment(
|
|
|
|
"SELECT a.attnum, a.attname\nFROM pg_attribute a\nWHERE a.attrelid = 10605202\nAND a.attnum IN (3)\n"
|
|
|
|
)
|
2022-07-16 23:28:13 +05:30
|
|
|
end.not_to change(transaction, :count)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
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
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
stub_rails_env('production')
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
transaction.count = described_class.threshold + 1
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
transaction.count = max = described_class.threshold
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'includes a list of executed queries' do
|
|
|
|
transaction = described_class.new
|
2022-11-25 23:54:43 +05:30
|
|
|
transaction.count = max = described_class.threshold
|
2021-04-17 20:07:23 +05:30
|
|
|
%w[foo bar baz].each { |sql| transaction.executed_sql(sql) }
|
|
|
|
|
|
|
|
message = transaction.error_message
|
|
|
|
|
|
|
|
expect(message).to start_with(
|
|
|
|
"Too many SQL queries were executed: a maximum of #{max} " \
|
|
|
|
"is allowed but #{max} SQL queries were executed"
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(message).to include("0: foo", "1: bar", "2: baz")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'indicates if the log is truncated' do
|
|
|
|
transaction = described_class.new
|
2022-11-25 23:54:43 +05:30
|
|
|
transaction.count = described_class.threshold * 2
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
message = transaction.error_message
|
|
|
|
|
|
|
|
expect(message).to end_with('...')
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'includes the action name in the error message when present' do
|
|
|
|
transaction = described_class.new
|
2022-11-25 23:54:43 +05:30
|
|
|
transaction.count = max = described_class.threshold
|
2018-03-17 18:26:18 +05:30
|
|
|
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
|