debian-mirror-gitlab/spec/lib/gitlab/sidekiq_middleware/client_metrics_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
3 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::SidekiqMiddleware::ClientMetrics do
2021-09-30 23:02:18 +05:30
let(:enqueued_jobs_metric) { double('enqueued jobs metric', increment: true) }
2021-04-29 21:17:54 +05:30
shared_examples "a metrics middleware" do
context "with mocked prometheus" do
2020-06-23 00:09:42 +05:30
before do
2021-09-30 23:02:18 +05:30
labels[:scheduling] = 'immediate'
2021-04-29 21:17:54 +05:30
allow(Gitlab::Metrics).to receive(:counter).with(described_class::ENQUEUED, anything).and_return(enqueued_jobs_metric)
2020-04-08 14:13:33 +05:30
end
2021-04-29 21:17:54 +05:30
describe '#call' do
it 'yields block' do
expect { |b| subject.call(worker_class, job, :test, double, &b) }.to yield_control.once
2020-03-13 15:44:24 +05:30
end
2021-04-29 21:17:54 +05:30
it 'increments enqueued jobs metric with correct labels when worker is a string of the class' do
expect(enqueued_jobs_metric).to receive(:increment).with(labels, 1)
2020-03-13 15:44:24 +05:30
2021-04-29 21:17:54 +05:30
subject.call(worker_class.to_s, job, :test, double) { nil }
2020-03-13 15:44:24 +05:30
end
2021-04-29 21:17:54 +05:30
it 'increments enqueued jobs metric with correct labels' do
expect(enqueued_jobs_metric).to receive(:increment).with(labels, 1)
2020-03-13 15:44:24 +05:30
2021-04-29 21:17:54 +05:30
subject.call(worker_class, job, :test, double) { nil }
2020-03-13 15:44:24 +05:30
end
end
end
end
2021-04-29 21:17:54 +05:30
it_behaves_like 'metrics middleware with worker attribution'
2021-09-30 23:02:18 +05:30
context 'when mounted' do
before do
stub_const('TestWorker', Class.new)
TestWorker.class_eval do
include Sidekiq::Worker
def perform(*args)
end
end
allow(Gitlab::Metrics).to receive(:counter).and_return(Gitlab::Metrics::NullMetric.instance)
allow(Gitlab::Metrics).to receive(:counter).with(described_class::ENQUEUED, anything).and_return(enqueued_jobs_metric)
end
context 'when scheduling jobs for immediate execution' do
it 'increments enqueued jobs metric with scheduling label set to immediate' do
expect(enqueued_jobs_metric).to receive(:increment).with(a_hash_including(scheduling: 'immediate'), 1)
Sidekiq::Testing.inline! { TestWorker.perform_async }
end
end
context 'when scheduling jobs for future execution' do
it 'increments enqueued jobs metric with scheduling label set to delayed' do
expect(enqueued_jobs_metric).to receive(:increment).with(a_hash_including(scheduling: 'delayed'), 1)
Sidekiq::Testing.inline! { TestWorker.perform_in(1.second) }
end
2021-11-18 22:05:49 +05:30
it 'sets the scheduled_at field' do
job = { 'at' => Time.current }
subject.call('TestWorker', job, 'queue', nil) do
expect(job[:scheduled_at]).to eq(job['at'])
end
end
end
context 'when the worker class cannot be found' do
it 'increments enqueued jobs metric with the worker labels set to NilClass' do
test_anonymous_worker = Class.new(TestWorker)
expect(enqueued_jobs_metric).to receive(:increment).with(a_hash_including(worker: 'NilClass'), 1)
# Sidekiq won't be able to create an instance of this class
expect do
Sidekiq::Testing.inline! { test_anonymous_worker.perform_async }
end.to raise_error(NameError)
end
2021-09-30 23:02:18 +05:30
end
end
2020-03-13 15:44:24 +05:30
end