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
|
2020-05-15 05:07:59 -04:00
|
|
|
before do
|
|
|
|
stub_const('TestWorker', Class.new)
|
2019-11-20 16:06:38 -05:00
|
|
|
|
2020-05-15 05:07:59 -04:00
|
|
|
TestWorker.class_eval do
|
|
|
|
include Sidekiq::Worker
|
2020-05-19 08:08:21 -04:00
|
|
|
include ApplicationWorker
|
2020-05-15 05:07:59 -04:00
|
|
|
|
|
|
|
def perform(_arg)
|
|
|
|
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
|
|
|
|
|
|
|
|
around do |example|
|
|
|
|
Sidekiq::Testing.inline! { example.run }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:worker_class) { TestWorker }
|
|
|
|
let(:job_args) { [0.01] }
|
|
|
|
|
|
|
|
# The test sets up a new server middleware stack, ensuring that the
|
|
|
|
# appropriate middlewares, as passed into server_configurator,
|
|
|
|
# are invoked.
|
|
|
|
# Additionally the test ensure that each middleware is
|
|
|
|
# 1) not failing
|
|
|
|
# 2) yielding exactly once
|
|
|
|
describe '.server_configurator' do
|
2019-12-15 22:07:29 -05:00
|
|
|
around do |example|
|
2020-04-21 11:21:10 -04:00
|
|
|
with_sidekiq_server_middleware do |chain|
|
|
|
|
described_class.server_configurator(
|
|
|
|
metrics: metrics,
|
|
|
|
arguments_logger: arguments_logger,
|
2020-04-24 08:10:16 -04:00
|
|
|
memory_killer: memory_killer
|
2020-04-21 11:21:10 -04:00
|
|
|
).call(chain)
|
|
|
|
|
|
|
|
example.run
|
|
|
|
end
|
2019-12-15 22:07:29 -05:00
|
|
|
end
|
|
|
|
|
2019-11-20 16:06:38 -05:00
|
|
|
let(:middleware_expected_args) { [a_kind_of(worker_class), hash_including({ 'args' => job_args }), anything] }
|
|
|
|
let(:all_sidekiq_middlewares) do
|
|
|
|
[
|
|
|
|
Gitlab::SidekiqMiddleware::Monitor,
|
|
|
|
Gitlab::SidekiqMiddleware::BatchLoader,
|
2020-01-03 10:08:33 -05:00
|
|
|
Labkit::Middleware::Sidekiq::Server,
|
2019-11-20 16:06:38 -05:00
|
|
|
Gitlab::SidekiqMiddleware::InstrumentationLogger,
|
2020-08-04 11:09:27 -04:00
|
|
|
Gitlab::SidekiqVersioning::Middleware,
|
2019-11-20 16:06:38 -05:00
|
|
|
Gitlab::SidekiqStatus::ServerMiddleware,
|
2020-01-16 13:08:46 -05:00
|
|
|
Gitlab::SidekiqMiddleware::ServerMetrics,
|
2019-11-20 16:06:38 -05:00
|
|
|
Gitlab::SidekiqMiddleware::ArgumentsLogger,
|
|
|
|
Gitlab::SidekiqMiddleware::MemoryKiller,
|
2020-01-24 07:09:01 -05:00
|
|
|
Gitlab::SidekiqMiddleware::RequestStoreMiddleware,
|
2020-05-19 08:08:21 -04:00
|
|
|
Gitlab::SidekiqMiddleware::ExtraDoneLogMetadata,
|
2020-02-05 13:09:06 -05:00
|
|
|
Gitlab::SidekiqMiddleware::WorkerContext::Server,
|
2020-02-27 13:09:21 -05:00
|
|
|
Gitlab::SidekiqMiddleware::AdminMode::Server,
|
|
|
|
Gitlab::SidekiqMiddleware::DuplicateJobs::Server
|
2019-11-20 16:06:38 -05:00
|
|
|
]
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2019-11-20 16:06:38 -05:00
|
|
|
let(:enabled_sidekiq_middlewares) { all_sidekiq_middlewares - disabled_sidekiq_middlewares }
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
shared_examples "a server middleware chain" do
|
|
|
|
it "passes through the right server middlewares" do
|
|
|
|
enabled_sidekiq_middlewares.each do |middleware|
|
|
|
|
expect_any_instance_of(middleware).to receive(:call).with(*middleware_expected_args).once.and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
disabled_sidekiq_middlewares.each do |middleware|
|
|
|
|
expect_any_instance_of(middleware).not_to receive(:call)
|
|
|
|
end
|
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
|
|
|
|
|
2020-08-04 11:09:27 -04:00
|
|
|
shared_examples "a server middleware chain for mailer" do
|
|
|
|
let(:worker_class) { ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper }
|
|
|
|
let(:job_args) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"job_class" => "ActionMailer::MailDeliveryJob",
|
|
|
|
"job_id" => "a180b47c-3fd6-41b8-81e9-34da61c3400e",
|
|
|
|
"provider_job_id" => nil,
|
|
|
|
"queue_name" => "mailers",
|
|
|
|
"priority" => nil,
|
|
|
|
"arguments" => [
|
|
|
|
"Notify",
|
|
|
|
"test_email",
|
|
|
|
"deliver_now",
|
|
|
|
{
|
|
|
|
"args" => [
|
|
|
|
"test@example.com",
|
|
|
|
"subject",
|
|
|
|
"body"
|
|
|
|
],
|
2021-01-07 13:10:38 -05:00
|
|
|
ActiveJob::Arguments.const_get('RUBY2_KEYWORDS_KEY', false) => ["args"]
|
2020-08-04 11:09:27 -04:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"executions" => 0,
|
|
|
|
"exception_executions" => {},
|
|
|
|
"locale" => "en",
|
|
|
|
"timezone" => "UTC",
|
|
|
|
"enqueued_at" => "2020-07-27T07:43:31Z"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like "a server middleware chain"
|
|
|
|
end
|
|
|
|
|
2019-11-20 16:06:38 -05:00
|
|
|
context "all optional middlewares off" do
|
|
|
|
let(:metrics) { false }
|
|
|
|
let(:arguments_logger) { false }
|
|
|
|
let(:memory_killer) { false }
|
|
|
|
let(:disabled_sidekiq_middlewares) do
|
|
|
|
[
|
2020-01-16 13:08:46 -05:00
|
|
|
Gitlab::SidekiqMiddleware::ServerMetrics,
|
2019-11-20 16:06:38 -05:00
|
|
|
Gitlab::SidekiqMiddleware::ArgumentsLogger,
|
2020-04-24 08:10:16 -04:00
|
|
|
Gitlab::SidekiqMiddleware::MemoryKiller
|
2019-11-20 16:06:38 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
it_behaves_like "a server middleware chain"
|
2020-08-04 11:09:27 -04:00
|
|
|
it_behaves_like "a server middleware chain for mailer"
|
2019-11-20 16:06:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "all optional middlewares on" do
|
|
|
|
let(:metrics) { true }
|
|
|
|
let(:arguments_logger) { true }
|
|
|
|
let(:memory_killer) { true }
|
|
|
|
let(:disabled_sidekiq_middlewares) { [] }
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
it_behaves_like "a server middleware chain"
|
2020-08-04 11:09:27 -04:00
|
|
|
it_behaves_like "a server middleware chain for mailer"
|
2020-03-16 20:09:12 -04:00
|
|
|
|
|
|
|
context "server metrics" do
|
|
|
|
let(:gitaly_histogram) { double(:gitaly_histogram) }
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
worker_class.perform_async(*job_args)
|
|
|
|
end
|
|
|
|
end
|
2019-11-20 16:06:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# The test sets up a new client middleware stack. The test ensures
|
|
|
|
# that each middleware is:
|
|
|
|
# 1) not failing
|
|
|
|
# 2) yielding exactly once
|
|
|
|
describe '.client_configurator' do
|
|
|
|
let(:chain) { Sidekiq::Middleware::Chain.new }
|
|
|
|
let(:job) { { 'args' => job_args } }
|
|
|
|
let(:queue) { 'default' }
|
|
|
|
let(:redis_pool) { Sidekiq.redis_pool }
|
|
|
|
let(:middleware_expected_args) { [worker_class_arg, job, queue, redis_pool] }
|
2020-01-31 04:08:53 -05:00
|
|
|
let(:expected_middlewares) do
|
|
|
|
[
|
2020-04-06 08:10:44 -04:00
|
|
|
::Gitlab::SidekiqMiddleware::WorkerContext::Client,
|
|
|
|
::Labkit::Middleware::Sidekiq::Client,
|
|
|
|
::Gitlab::SidekiqMiddleware::DuplicateJobs::Client,
|
|
|
|
::Gitlab::SidekiqStatus::ClientMiddleware,
|
|
|
|
::Gitlab::SidekiqMiddleware::AdminMode::Client,
|
|
|
|
::Gitlab::SidekiqMiddleware::ClientMetrics
|
2020-01-31 04:08:53 -05:00
|
|
|
]
|
|
|
|
end
|
2019-11-20 16:06:38 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
described_class.client_configurator.call(chain)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "a client middleware chain" do
|
|
|
|
# Its possible that a middleware could accidentally omit a yield call
|
|
|
|
# this will prevent the full middleware chain from being executed.
|
|
|
|
# This test ensures that this does not happen
|
|
|
|
it "invokes the chain" do
|
2020-01-31 04:08:53 -05:00
|
|
|
expected_middlewares do |middleware|
|
|
|
|
expect_any_instance_of(middleware).to receive(:call).with(*middleware_expected_args).once.ordered.and_call_original
|
|
|
|
end
|
2019-11-20 16:06:38 -05:00
|
|
|
|
|
|
|
expect { |b| chain.invoke(worker_class_arg, job, queue, redis_pool, &b) }.to yield_control.once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sidekiq documentation states that the worker class could be a string
|
|
|
|
# or a class reference. We should test for both
|
|
|
|
context "handles string worker_class values" do
|
|
|
|
let(:worker_class_arg) { worker_class.to_s }
|
|
|
|
|
|
|
|
it_behaves_like "a client middleware chain"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "handles string worker_class values" do
|
|
|
|
let(:worker_class_arg) { worker_class }
|
|
|
|
|
|
|
|
it_behaves_like "a client middleware chain"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|