2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Middleware::RailsQueueDuration do
|
2016-06-16 23:09:34 +05:30
|
|
|
let(:app) { double(:app) }
|
|
|
|
let(:middleware) { described_class.new(app) }
|
|
|
|
let(:env) { {} }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:transaction) { Gitlab::Metrics::WebTransaction.new(env) }
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
2020-04-22 19:07:51 +05:30
|
|
|
allow(app).to receive(:call).with(env).and_return('yay')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
it 'calls the app when metrics are disabled' do
|
|
|
|
expect(Gitlab::Metrics).to receive(:current_transaction).and_return(nil)
|
|
|
|
expect(middleware.call(env)).to eq('yay')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metrics are enabled' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics).to receive(:current_transaction).and_return(transaction)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
it 'calls the app when metrics are enabled but no timing header is found' do
|
|
|
|
expect(middleware.call(env)).to eq('yay')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets proxy_flight_time and calls the app when the header is present' do
|
2016-09-13 17:45:13 +05:30
|
|
|
env['HTTP_GITLAB_WORKHORSE_PROXY_START'] = '123'
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(transaction).to receive(:set).with(:gitlab_transaction_rails_queue_duration_total, an_instance_of(Float))
|
2016-06-16 23:09:34 +05:30
|
|
|
expect(middleware.call(env)).to eq('yay')
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it 'observes rails queue duration metrics and calls the app when the header is present' do
|
|
|
|
env['HTTP_GITLAB_WORKHORSE_PROXY_START'] = '2000000000'
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(transaction).to receive(:observe).with(:gitlab_rails_queue_duration_seconds, 1)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
travel_to(Time.at(3)) do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(middleware.call(env)).to eq('yay')
|
|
|
|
end
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|