Commit c9dcab30 authored by Quang-Minh Nguyen's avatar Quang-Minh Nguyen

Refactor background transaction tests

Issue https://gitlab.com/gitlab-org/gitlab/-/issues/323163
parent e99ee95c
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Metrics::BackgroundTransaction do
let(:transaction) { described_class.new }
let(:prometheus_metric) { instance_double(Prometheus::Client::Metric, base_labels: {}) }
before do
allow(described_class).to receive(:prometheus_metric).and_return(prometheus_metric)
end
describe '#run' do
it 'yields the supplied block' do
expect { |b| transaction.run(&b) }.to yield_control
end
it 'stores the transaction in the current thread' do
transaction.run do
expect(Thread.current[described_class::BACKGROUND_THREAD_KEY]).to eq(transaction)
end
end
it 'removes the transaction from the current thread upon completion' do
transaction.run { }
expect(Thread.current[described_class::BACKGROUND_THREAD_KEY]).to be_nil
end
end
describe '#labels' do
it 'provides labels with endpoint_id and feature_category' do
Labkit::Context.with_context(feature_category: 'projects', caller_id: 'TestWorker') do
expect(transaction.labels).to eq({ endpoint_id: 'TestWorker', feature_category: 'projects' })
end
end
end
RSpec.shared_examples 'metric with labels' do |metric_method|
it 'measures with correct labels and value' do
value = 1
expect(prometheus_metric).to receive(metric_method).with({ endpoint_id: 'TestWorker', feature_category: 'projects' }, value)
Labkit::Context.with_context(feature_category: 'projects', caller_id: 'TestWorker') do
transaction.send(metric_method, :test_metric, value)
end
end
end
describe '#increment' do
let(:prometheus_metric) { instance_double(Prometheus::Client::Counter, :increment, base_labels: {}) }
it_behaves_like 'metric with labels', :increment
end
describe '#set' do
let(:prometheus_metric) { instance_double(Prometheus::Client::Gauge, :set, base_labels: {}) }
it_behaves_like 'metric with labels', :set
end
describe '#observe' do
let(:prometheus_metric) { instance_double(Prometheus::Client::Histogram, :observe, base_labels: {}) }
it_behaves_like 'metric with labels', :observe
end
end
...@@ -113,6 +113,14 @@ RSpec.describe Gitlab::SidekiqMiddleware::ServerMetrics do ...@@ -113,6 +113,14 @@ RSpec.describe Gitlab::SidekiqMiddleware::ServerMetrics do
expect { |b| subject.call(worker, job, :test, &b) }.to yield_control.once expect { |b| subject.call(worker, job, :test, &b) }.to yield_control.once
end end
it 'calls BackgroundTransaction' do
expect_next_instance_of(Gitlab::Metrics::BackgroundTransaction) do |instance|
expect(instance).to receive(:run)
end
subject.call(worker, job, :test) {}
end
it 'sets queue specific metrics' do it 'sets queue specific metrics' do
expect(running_jobs_metric).to receive(:increment).with(labels, -1) expect(running_jobs_metric).to receive(:increment).with(labels, -1)
expect(running_jobs_metric).to receive(:increment).with(labels, 1) expect(running_jobs_metric).to receive(:increment).with(labels, 1)
......
...@@ -99,26 +99,30 @@ RSpec.shared_examples 'record ActiveRecord metrics' do |db_role| ...@@ -99,26 +99,30 @@ RSpec.shared_examples 'record ActiveRecord metrics' do |db_role|
end end
context 'when web transaction is available' do context 'when web transaction is available' do
let(:transaction) { double('Gitlab::Metrics::WebTransaction', increment: nil, observe: nil) } let(:transaction) { double('Gitlab::Metrics::WebTransaction') }
before do before do
allow(::Gitlab::Metrics::WebTransaction).to receive(:current) allow(::Gitlab::Metrics::WebTransaction).to receive(:current)
.and_return(transaction) .and_return(transaction)
allow(::Gitlab::Metrics::BackgroundTransaction).to receive(:current) allow(::Gitlab::Metrics::BackgroundTransaction).to receive(:current)
.and_return(nil) .and_return(nil)
allow(transaction).to receive(:increment)
allow(transaction).to receive(:observe)
end end
it_behaves_like 'record ActiveRecord metrics in a metrics transaction', db_role it_behaves_like 'record ActiveRecord metrics in a metrics transaction', db_role
end end
context 'when background transaction is available' do context 'when background transaction is available' do
let(:transaction) { double('Gitlab::Metrics::BackgroundTransaction', increment: nil, observe: nil) } let(:transaction) { double('Gitlab::Metrics::BackgroundTransaction') }
before do before do
allow(::Gitlab::Metrics::WebTransaction).to receive(:current) allow(::Gitlab::Metrics::WebTransaction).to receive(:current)
.and_return(nil) .and_return(nil)
allow(::Gitlab::Metrics::BackgroundTransaction).to receive(:current) allow(::Gitlab::Metrics::BackgroundTransaction).to receive(:current)
.and_return(transaction) .and_return(transaction)
allow(transaction).to receive(:increment)
allow(transaction).to receive(:observe)
end end
it_behaves_like 'record ActiveRecord metrics in a metrics transaction', db_role it_behaves_like 'record ActiveRecord metrics in a metrics transaction', db_role
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment