debian-mirror-gitlab/spec/lib/gitlab/metrics/sidekiq_middleware_spec.rb

67 lines
2.4 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Metrics::SidekiqMiddleware do
let(:middleware) { described_class.new }
2016-08-24 12:49:21 +05:30
let(:message) { { 'args' => ['test'], 'enqueued_at' => Time.new(2016, 6, 23, 6, 59).to_f } }
describe '#call' do
it 'tracks the transaction' do
worker = double(:worker, class: double(:class, name: 'TestWorker'))
2020-06-23 00:09:42 +05:30
expect_next_instance_of(Gitlab::Metrics::BackgroundTransaction) do |transaction|
2020-10-24 23:57:45 +05:30
expect(transaction).to receive(:set).with(:gitlab_transaction_sidekiq_queue_duration_total, instance_of(Float))
expect(transaction).to receive(:increment).with(:gitlab_transaction_db_count_total, 1)
2020-06-23 00:09:42 +05:30
end
2020-06-23 00:09:42 +05:30
middleware.call(worker, message, :test) do
ActiveRecord::Base.connection.execute('SELECT pg_sleep(0.1);')
end
2020-07-28 23:09:34 +05:30
end
it 'prevents database counters from leaking to the next transaction' do
worker = double(:worker, class: double(:class, name: 'TestWorker'))
2016-09-13 17:45:13 +05:30
2020-07-28 23:09:34 +05:30
2.times do
Gitlab::WithRequestStore.with_request_store do
middleware.call(worker, message, :test) do
ActiveRecord::Base.connection.execute('SELECT pg_sleep(0.1);')
end
end
end
expect(message).to include(db_count: 1, db_write_count: 0, db_cached_count: 0)
2016-08-24 12:49:21 +05:30
end
2020-06-23 00:09:42 +05:30
it 'tracks the transaction (for messages without `enqueued_at`)', :aggregate_failures do
2016-08-24 12:49:21 +05:30
worker = double(:worker, class: double(:class, name: 'TestWorker'))
2018-03-17 18:26:18 +05:30
expect(Gitlab::Metrics::BackgroundTransaction).to receive(:new)
.with(worker.class)
2017-09-10 17:25:29 +05:30
.and_call_original
2016-08-24 12:49:21 +05:30
2017-09-10 17:25:29 +05:30
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:set)
2020-10-24 23:57:45 +05:30
.with(:gitlab_transaction_sidekiq_queue_duration_total, instance_of(Float))
2016-09-13 17:45:13 +05:30
2016-08-24 12:49:21 +05:30
middleware.call(worker, {}, :test) { nil }
end
2016-09-13 17:45:13 +05:30
2020-07-28 23:09:34 +05:30
it 'tracks any raised exceptions', :aggregate_failures, :request_store do
2016-09-13 17:45:13 +05:30
worker = double(:worker, class: double(:class, name: 'TestWorker'))
2017-09-10 17:25:29 +05:30
expect_any_instance_of(Gitlab::Metrics::Transaction)
.to receive(:add_event).with(:sidekiq_exception)
2016-09-13 17:45:13 +05:30
2020-07-28 23:09:34 +05:30
expect do
middleware.call(worker, message, :test) do
ActiveRecord::Base.connection.execute('SELECT pg_sleep(0.1);')
raise RuntimeError
end
end.to raise_error(RuntimeError)
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
expect(message).to include(db_count: 1, db_write_count: 0, db_cached_count: 0)
2016-09-13 17:45:13 +05:30
end
end
end