Commit f1c9c1e7 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Use helper to test sidekiq middleware in isolation

This uses the new helper method to test sidekiq middleware in isolation.
parent 07c450fa
......@@ -8,8 +8,6 @@ describe 'Admin mode for workers', :do_not_mock_admin_mode, :request_store, :cle
let(:user_to_delete) { create(:user) }
before do
add_sidekiq_middleware
sign_in(user)
end
......@@ -60,12 +58,6 @@ describe 'Admin mode for workers', :do_not_mock_admin_mode, :request_store, :cle
end
end
def add_sidekiq_middleware
Sidekiq::Testing.server_middleware do |chain|
chain.add Gitlab::SidekiqMiddleware::AdminMode::Server
end
end
def execute_jobs_signed_out(user)
gitlab_sign_out
......
......@@ -21,18 +21,9 @@ describe Gitlab::SidekiqMiddleware::DuplicateJobs::Server, :clean_gitlab_redis_q
end
around do |example|
Sidekiq::Testing.inline! { example.run }
end
before(:context) do
Sidekiq::Testing.server_middleware do |chain|
with_sidekiq_server_middleware do |chain|
chain.add described_class
end
end
after(:context) do
Sidekiq::Testing.server_middleware do |chain|
chain.remove described_class
Sidekiq::Testing.inline! { example.run }
end
end
......
......@@ -41,18 +41,9 @@ describe Gitlab::SidekiqMiddleware::WorkerContext::Server do
end
around do |example|
Sidekiq::Testing.inline! { example.run }
end
before(:context) do
Sidekiq::Testing.server_middleware do |chain|
with_sidekiq_server_middleware do |chain|
chain.add described_class
end
end
after(:context) do
Sidekiq::Testing.server_middleware do |chain|
chain.remove described_class
Sidekiq::Testing.inline! { example.run }
end
end
......
......@@ -28,11 +28,16 @@ describe Gitlab::SidekiqMiddleware do
# 2) yielding exactly once
describe '.server_configurator' do
around do |example|
original = Sidekiq::Testing.server_middleware.dup
example.run
Sidekiq::Testing.instance_variable_set :@server_chain, original
with_sidekiq_server_middleware do |chain|
described_class.server_configurator(
metrics: metrics,
arguments_logger: arguments_logger,
memory_killer: memory_killer,
request_store: request_store
).call(chain)
example.run
end
end
let(:middleware_expected_args) { [a_kind_of(worker_class), hash_including({ 'args' => job_args }), anything] }
......@@ -54,21 +59,17 @@ describe Gitlab::SidekiqMiddleware do
end
let(:enabled_sidekiq_middlewares) { all_sidekiq_middlewares - disabled_sidekiq_middlewares }
before do
Sidekiq::Testing.server_middleware.clear
Sidekiq::Testing.server_middleware(&described_class.server_configurator(
metrics: metrics,
arguments_logger: arguments_logger,
memory_killer: memory_killer,
request_store: request_store
))
enabled_sidekiq_middlewares.each do |middleware|
expect_any_instance_of(middleware).to receive(:call).with(*middleware_expected_args).once.and_call_original
end
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)
disabled_sidekiq_middlewares.each do |middleware|
expect_any_instance_of(middleware).not_to receive(:call)
end
worker_class.perform_async(*job_args)
end
end
......@@ -86,9 +87,7 @@ describe Gitlab::SidekiqMiddleware do
]
end
it "passes through server middlewares" do
worker_class.perform_async(*job_args)
end
it_behaves_like "a server middleware chain"
end
context "all optional middlewares on" do
......@@ -98,9 +97,7 @@ describe Gitlab::SidekiqMiddleware do
let(:request_store) { true }
let(:disabled_sidekiq_middlewares) { [] }
it "passes through server middlewares" do
worker_class.perform_async(*job_args)
end
it_behaves_like "a server middleware chain"
context "server metrics" do
let(:gitaly_histogram) { double(:gitaly_histogram) }
......
......@@ -24,20 +24,6 @@ describe 'Marginalia spec' do
end
end
def add_sidekiq_middleware
# Reference: https://github.com/mperham/sidekiq/wiki/Testing#testing-server-middlewaresidekiq
# Sidekiq test harness fakes worker without its server middlewares, so include instrumentation to 'Sidekiq::Testing' server middleware.
Sidekiq::Testing.server_middleware do |chain|
chain.add Marginalia::SidekiqInstrumentation::Middleware
end
end
def remove_sidekiq_middleware
Sidekiq::Testing.server_middleware do |chain|
chain.remove Marginalia::SidekiqInstrumentation::Middleware
end
end
def stub_feature(value)
allow(Gitlab::Marginalia).to receive(:cached_feature_enabled?).and_return(value)
end
......@@ -88,20 +74,16 @@ describe 'Marginalia spec' do
end
describe 'for Sidekiq worker jobs' do
before(:all) do
add_sidekiq_middleware
# Because of faking, 'Sidekiq.server?' does not work so implicitly set application name which is done in config/initializers/0_marginalia.rb
Marginalia.application_name = "sidekiq"
around do |example|
with_sidekiq_server_middleware do |chain|
chain.add Marginalia::SidekiqInstrumentation::Middleware
Marginalia.application_name = "sidekiq"
example.run
end
end
after(:all) do
MarginaliaTestJob.clear
remove_sidekiq_middleware
end
around do |example|
Sidekiq::Testing.fake! { example.run }
end
before do
......
......@@ -136,6 +136,7 @@ RSpec.configure do |config|
config.include ExpectRequestWithStatus, type: :request
config.include IdempotentWorkerHelper, type: :worker
config.include RailsHelpers
config.include SidekiqMiddleware
if ENV['CI'] || ENV['RETRIES']
# This includes the first try, i.e. tests will be run 4 times before failing.
......
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