2019-11-20 16:06:38 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
require 'sidekiq/testing'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::SidekiqMiddleware do
|
2021-06-03 05:10:18 -04:00
|
|
|
let(:job_args) { [0.01] }
|
|
|
|
let(:disabled_sidekiq_middlewares) { [] }
|
|
|
|
let(:chain) { Sidekiq::Middleware::Chain.new }
|
|
|
|
let(:queue) { 'test' }
|
|
|
|
let(:enabled_sidekiq_middlewares) { all_sidekiq_middlewares - disabled_sidekiq_middlewares }
|
|
|
|
let(:worker_class) do
|
|
|
|
Class.new do
|
|
|
|
def self.name
|
|
|
|
'TestWorker'
|
|
|
|
end
|
2019-11-20 16:06:38 -05:00
|
|
|
|
2020-05-19 08:08:21 -04:00
|
|
|
include ApplicationWorker
|
2020-05-15 05:07:59 -04:00
|
|
|
|
2021-06-03 05:10:18 -04:00
|
|
|
def perform(*args)
|
2020-05-15 05:07:59 -04:00
|
|
|
Gitlab::SafeRequestStore['gitaly_call_actual'] = 1
|
2020-05-20 20:08:06 -04:00
|
|
|
Gitlab::SafeRequestStore[:gitaly_query_time] = 5
|
2020-05-15 05:07:59 -04:00
|
|
|
end
|
2019-11-20 16:06:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-03 05:10:18 -04:00
|
|
|
before do
|
|
|
|
stub_const('TestWorker', worker_class)
|
2019-11-20 16:06:38 -05:00
|
|
|
end
|
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
shared_examples "a middleware chain" do
|
2021-06-03 05:10:18 -04:00
|
|
|
before do
|
|
|
|
configurator.call(chain)
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2021-06-03 05:10:18 -04:00
|
|
|
it "passes through the right middlewares", :aggregate_failures do
|
|
|
|
enabled_sidekiq_middlewares.each do |middleware|
|
|
|
|
expect_next_instances_of(middleware, 1, true) do |middleware_instance|
|
|
|
|
expect(middleware_instance).to receive(:call).with(*middleware_expected_args).once.and_call_original
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2021-06-03 05:10:18 -04:00
|
|
|
|
|
|
|
expect { |b| chain.invoke(*worker_args, &b) }.to yield_control.once
|
2019-12-15 22:07:29 -05:00
|
|
|
end
|
2021-06-03 05:10:18 -04:00
|
|
|
end
|
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
shared_examples "a middleware chain for mailer" do
|
2021-06-03 05:10:18 -04:00
|
|
|
let(:worker_class) { ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper }
|
2019-12-15 22:07:29 -05:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
it_behaves_like "a middleware chain"
|
2021-06-03 05:10:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.server_configurator' do
|
|
|
|
let(:configurator) { described_class.server_configurator }
|
|
|
|
let(:worker_args) { [worker_class.new, { 'args' => job_args }, queue] }
|
|
|
|
let(:middleware_expected_args) { [a_kind_of(worker_class), hash_including({ 'args' => job_args }), queue] }
|
2019-11-20 16:06:38 -05:00
|
|
|
let(:all_sidekiq_middlewares) do
|
|
|
|
[
|
2021-06-03 05:10:18 -04:00
|
|
|
::Gitlab::SidekiqMiddleware::Monitor,
|
2021-09-20 20:09:28 -04:00
|
|
|
::Labkit::Middleware::Sidekiq::Server,
|
2021-06-03 05:10:18 -04:00
|
|
|
::Gitlab::SidekiqMiddleware::ServerMetrics,
|
|
|
|
::Gitlab::SidekiqMiddleware::ArgumentsLogger,
|
|
|
|
::Gitlab::SidekiqMiddleware::RequestStoreMiddleware,
|
|
|
|
::Gitlab::SidekiqMiddleware::ExtraDoneLogMetadata,
|
|
|
|
::Gitlab::SidekiqMiddleware::BatchLoader,
|
|
|
|
::Gitlab::SidekiqMiddleware::InstrumentationLogger,
|
|
|
|
::Gitlab::SidekiqMiddleware::AdminMode::Server,
|
|
|
|
::Gitlab::SidekiqVersioning::Middleware,
|
|
|
|
::Gitlab::SidekiqStatus::ServerMiddleware,
|
|
|
|
::Gitlab::SidekiqMiddleware::WorkerContext::Server,
|
2021-09-02 08:11:03 -04:00
|
|
|
::Gitlab::SidekiqMiddleware::DuplicateJobs::Server,
|
|
|
|
::Gitlab::Database::LoadBalancing::SidekiqServerMiddleware
|
2019-11-20 16:06:38 -05:00
|
|
|
]
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2021-06-03 05:10:18 -04:00
|
|
|
describe "server metrics" do
|
|
|
|
around do |example|
|
|
|
|
with_sidekiq_server_middleware do |chain|
|
|
|
|
described_class.server_configurator(
|
|
|
|
metrics: true,
|
2022-09-20 14:13:03 -04:00
|
|
|
arguments_logger: true
|
2021-06-03 05:10:18 -04:00
|
|
|
).call(chain)
|
2019-11-20 16:06:38 -05:00
|
|
|
|
2021-06-03 05:10:18 -04:00
|
|
|
Sidekiq::Testing.inline! { example.run }
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2021-06-03 05:10:18 -04:00
|
|
|
end
|
|
|
|
let(:gitaly_histogram) { double(:gitaly_histogram) }
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2021-06-03 05:10:18 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics).to receive(:histogram).and_call_original
|
|
|
|
|
|
|
|
allow(Gitlab::Metrics).to receive(:histogram)
|
|
|
|
.with(:sidekiq_jobs_gitaly_seconds, anything, anything, anything)
|
|
|
|
.and_return(gitaly_histogram)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "records correct Gitaly duration" do
|
|
|
|
expect(gitaly_histogram).to receive(:observe).with(anything, 5.0)
|
2019-11-20 16:06:38 -05:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
worker_class.perform_async(*job_args)
|
2019-11-20 16:06:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-03 05:10:18 -04:00
|
|
|
context "all optional middlewares on" do
|
2021-09-21 14:11:18 -04:00
|
|
|
it_behaves_like "a middleware chain"
|
|
|
|
it_behaves_like "a middleware chain for mailer"
|
2019-11-20 16:06:38 -05:00
|
|
|
end
|
|
|
|
|
2021-06-03 05:10:18 -04:00
|
|
|
context "all optional middlewares off" do
|
|
|
|
let(:configurator) do
|
|
|
|
described_class.server_configurator(
|
|
|
|
metrics: false,
|
2022-09-20 14:13:03 -04:00
|
|
|
arguments_logger: false
|
2021-06-03 05:10:18 -04:00
|
|
|
)
|
|
|
|
end
|
2020-03-16 20:09:12 -04:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
let(:disabled_sidekiq_middlewares) do
|
|
|
|
[
|
|
|
|
Gitlab::SidekiqMiddleware::ServerMetrics,
|
2022-09-20 14:13:03 -04:00
|
|
|
Gitlab::SidekiqMiddleware::ArgumentsLogger
|
2021-09-21 14:11:18 -04:00
|
|
|
]
|
2021-06-03 05:10:18 -04:00
|
|
|
end
|
2020-03-16 20:09:12 -04:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
it_behaves_like "a middleware chain"
|
|
|
|
it_behaves_like "a middleware chain for mailer"
|
2019-11-20 16:06:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.client_configurator' do
|
2021-06-03 05:10:18 -04:00
|
|
|
let(:configurator) { described_class.client_configurator }
|
2019-11-20 16:06:38 -05:00
|
|
|
let(:redis_pool) { Sidekiq.redis_pool }
|
2021-06-03 05:10:18 -04:00
|
|
|
let(:middleware_expected_args) { [worker_class, hash_including({ 'args' => job_args }), queue, redis_pool] }
|
|
|
|
let(:worker_args) { [worker_class, { 'args' => job_args }, queue, redis_pool] }
|
|
|
|
let(:all_sidekiq_middlewares) do
|
2020-01-31 04:08:53 -05:00
|
|
|
[
|
2021-06-03 05:10:18 -04:00
|
|
|
::Gitlab::SidekiqMiddleware::WorkerContext::Client,
|
|
|
|
::Labkit::Middleware::Sidekiq::Client,
|
2021-09-02 08:11:03 -04:00
|
|
|
::Gitlab::Database::LoadBalancing::SidekiqClientMiddleware,
|
2021-06-03 05:10:18 -04:00
|
|
|
::Gitlab::SidekiqMiddleware::DuplicateJobs::Client,
|
|
|
|
::Gitlab::SidekiqStatus::ClientMiddleware,
|
|
|
|
::Gitlab::SidekiqMiddleware::AdminMode::Client,
|
|
|
|
::Gitlab::SidekiqMiddleware::SizeLimiter::Client,
|
2021-09-02 08:11:03 -04:00
|
|
|
::Gitlab::SidekiqMiddleware::ClientMetrics
|
2020-01-31 04:08:53 -05:00
|
|
|
]
|
|
|
|
end
|
2019-11-20 16:06:38 -05:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
it_behaves_like "a middleware chain"
|
|
|
|
it_behaves_like "a middleware chain for mailer"
|
2019-11-20 16:06:38 -05:00
|
|
|
end
|
|
|
|
end
|