diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-05 12:21:54 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-05 12:21:54 +0300 |
commit | a92d6b36c2d2892e8c070efb169f0c06815900ee (patch) | |
tree | e7119c41b0e0d0eb0cff50b19fbb2bb34315f5a4 /spec/lib/gitlab/sidekiq_middleware | |
parent | 39a48637e52c4afc58341edfb10167947d961b33 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/sidekiq_middleware')
-rw-r--r-- | spec/lib/gitlab/sidekiq_middleware/metrics_spec.rb | 92 |
1 files changed, 33 insertions, 59 deletions
diff --git a/spec/lib/gitlab/sidekiq_middleware/metrics_spec.rb b/spec/lib/gitlab/sidekiq_middleware/metrics_spec.rb index c9399f591da..0d8cff3a295 100644 --- a/spec/lib/gitlab/sidekiq_middleware/metrics_spec.rb +++ b/spec/lib/gitlab/sidekiq_middleware/metrics_spec.rb @@ -1,11 +1,8 @@ # frozen_string_literal: true require 'fast_spec_helper' -require 'rspec-parameterized' describe Gitlab::SidekiqMiddleware::Metrics do - using RSpec::Parameterized::TableSyntax - let(:middleware) { described_class.new } let(:concurrency_metric) { double('concurrency metric') } @@ -48,7 +45,7 @@ describe Gitlab::SidekiqMiddleware::Metrics do let(:job) { {} } let(:job_status) { :done } let(:labels) { { queue: :test } } - let(:labels_with_job_status) { labels.merge(job_status: job_status) } + let(:labels_with_job_status) { { queue: :test, job_status: job_status } } let(:thread_cputime_before) { 1 } let(:thread_cputime_after) { 2 } @@ -60,75 +57,52 @@ describe Gitlab::SidekiqMiddleware::Metrics do let(:queue_duration_for_job) { 0.01 } - where(:worker_has_attributes, :worker_is_latency_sensitive, :worker_has_external_dependencies, :worker_feature_category, :worker_resource_boundary, :labels) do - false | false | false | nil | nil | { queue: :test } - true | false | false | nil | nil | { queue: :test } - true | true | false | nil | nil | { queue: :test, latency_sensitive: true } - true | false | true | nil | nil | { queue: :test, external_deps: true } - true | false | false | :authentication | nil | { queue: :test, feat_cat: :authentication } - true | false | false | nil | :cpu | { queue: :test, boundary: :cpu } - true | false | false | nil | :memory | { queue: :test, boundary: :memory } - true | false | false | nil | :unknown | { queue: :test } - true | true | true | :authentication | :cpu | { queue: :test, latency_sensitive: true, external_deps: true, feat_cat: :authentication, boundary: :cpu } - end + before do + allow(middleware).to receive(:get_thread_cputime).and_return(thread_cputime_before, thread_cputime_after) + allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(monotonic_time_before, monotonic_time_after) + allow(Gitlab::InstrumentationHelper).to receive(:queue_duration_for_job).with(job).and_return(queue_duration_for_job) - with_them do - before do - allow(middleware).to receive(:get_thread_cputime).and_return(thread_cputime_before, thread_cputime_after) - allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(monotonic_time_before, monotonic_time_after) - allow(Gitlab::InstrumentationHelper).to receive(:queue_duration_for_job).with(job).and_return(queue_duration_for_job) - - # Attributes - allow(worker).to receive(:include?).with(WorkerAttributes).and_return(worker_has_attributes) - allow(worker).to receive(:latency_sensitive_worker?).and_return(worker_is_latency_sensitive) - allow(worker).to receive(:worker_has_external_dependencies?).and_return(worker_has_external_dependencies) - allow(worker).to receive(:get_worker_resource_boundary).and_return(worker_resource_boundary) - allow(worker).to receive(:get_feature_category).and_return(worker_feature_category) - - expect(running_jobs_metric).to receive(:increment).with(labels, 1) - expect(running_jobs_metric).to receive(:increment).with(labels, -1) - - expect(queue_duration_seconds).to receive(:observe).with(labels, queue_duration_for_job) if queue_duration_for_job - expect(user_execution_seconds_metric).to receive(:observe).with(labels_with_job_status, thread_cputime_duration) - expect(completion_seconds_metric).to receive(:observe).with(labels_with_job_status, monotonic_time_duration) - end + expect(running_jobs_metric).to receive(:increment).with(labels, 1) + expect(running_jobs_metric).to receive(:increment).with(labels, -1) - it 'yields block' do - expect { |b| middleware.call(worker, job, :test, &b) }.to yield_control.once - end + expect(queue_duration_seconds).to receive(:observe).with(labels, queue_duration_for_job) if queue_duration_for_job + expect(user_execution_seconds_metric).to receive(:observe).with(labels_with_job_status, thread_cputime_duration) + expect(completion_seconds_metric).to receive(:observe).with(labels_with_job_status, monotonic_time_duration) + end - it 'sets queue specific metrics' do - middleware.call(worker, job, :test) { nil } - end + it 'yields block' do + expect { |b| middleware.call(worker, job, :test, &b) }.to yield_control.once + end - context 'when job_duration is not available' do - let(:queue_duration_for_job) { nil } + it 'sets queue specific metrics' do + middleware.call(worker, job, :test) { nil } + end - it 'does not set the queue_duration_seconds histogram' do - expect(queue_duration_seconds).not_to receive(:observe) + context 'when job_duration is not available' do + let(:queue_duration_for_job) { nil } - middleware.call(worker, job, :test) { nil } - end + it 'does not set the queue_duration_seconds histogram' do + middleware.call(worker, job, :test) { nil } end + end - context 'when job is retried' do - let(:job) { { 'retry_count' => 1 } } + context 'when job is retried' do + let(:job) { { 'retry_count' => 1 } } - it 'sets sidekiq_jobs_retried_total metric' do - expect(retried_total_metric).to receive(:increment) + it 'sets sidekiq_jobs_retried_total metric' do + expect(retried_total_metric).to receive(:increment) - middleware.call(worker, job, :test) { nil } - end + middleware.call(worker, job, :test) { nil } end + end - context 'when error is raised' do - let(:job_status) { :fail } + context 'when error is raised' do + let(:job_status) { :fail } - it 'sets sidekiq_jobs_failed_total and reraises' do - expect(failed_total_metric).to receive(:increment).with(labels, 1) + it 'sets sidekiq_jobs_failed_total and reraises' do + expect(failed_total_metric).to receive(:increment).with(labels, 1) - expect { middleware.call(worker, job, :test) { raise StandardError, "Failed" } }.to raise_error(StandardError, "Failed") - end + expect { middleware.call(worker, job, :test) { raise StandardError, "Failed" } }.to raise_error(StandardError, "Failed") end end end |