2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-06 07:48:46 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
describe Gitlab::RequestContext, :request_store do
|
|
|
|
subject { described_class.instance }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2020-04-27 05:09:51 -04:00
|
|
|
before do
|
|
|
|
allow(subject).to receive(:enabled?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
it { is_expected.to have_attributes(client_ip: nil, start_thread_cpu_time: nil, request_start_time: nil) }
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
describe '#request_deadline' do
|
|
|
|
let(:request_start_time) { 1575982156.206008 }
|
2019-03-12 14:22:29 -04:00
|
|
|
|
2020-01-20 10:09:18 -05:00
|
|
|
before do
|
2020-01-13 10:07:53 -05:00
|
|
|
allow(subject).to receive(:request_start_time).and_return(request_start_time)
|
2020-01-20 10:09:18 -05:00
|
|
|
end
|
2019-03-12 14:22:29 -04:00
|
|
|
|
2020-01-20 10:09:18 -05:00
|
|
|
it "sets the time to #{Settings.gitlab.max_request_duration_seconds} seconds in the future" do
|
|
|
|
expect(subject.request_deadline).to eq(request_start_time + Settings.gitlab.max_request_duration_seconds)
|
2020-01-13 10:07:53 -05:00
|
|
|
expect(subject.request_deadline).to be_a(Float)
|
|
|
|
end
|
2019-03-12 14:22:29 -04:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
it 'returns nil if there is no start time' do
|
|
|
|
allow(subject).to receive(:request_start_time).and_return(nil)
|
2019-03-12 14:22:29 -04:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
expect(subject.request_deadline).to be_nil
|
2019-03-12 14:22:29 -04:00
|
|
|
end
|
2020-01-13 10:07:53 -05:00
|
|
|
end
|
2019-03-12 14:22:29 -04:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
describe '#ensure_request_deadline_not_exceeded!' do
|
|
|
|
it 'does not raise an error when there was no deadline' do
|
|
|
|
expect(subject).to receive(:request_deadline).and_return(nil)
|
|
|
|
expect { subject.ensure_deadline_not_exceeded! }.not_to raise_error
|
|
|
|
end
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
it 'does not raise an error if the deadline is in the future' do
|
|
|
|
allow(subject).to receive(:request_deadline).and_return(Gitlab::Metrics::System.real_time + 10)
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
expect { subject.ensure_deadline_not_exceeded! }.not_to raise_error
|
|
|
|
end
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
it 'raises an error when the deadline is in the past' do
|
|
|
|
allow(subject).to receive(:request_deadline).and_return(Gitlab::Metrics::System.real_time - 10)
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
expect { subject.ensure_deadline_not_exceeded! }.to raise_error(described_class::RequestDeadlineExceeded)
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|