debian-mirror-gitlab/spec/lib/gitlab/sidekiq_middleware/duplicate_jobs/server_spec.rb

76 lines
2.4 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::SidekiqMiddleware::DuplicateJobs::Server, :clean_gitlab_redis_queues do
2021-01-29 00:20:46 +05:30
shared_context 'server duplicate job' do |strategy|
let(:worker_class) do
Class.new do
def self.name
'TestDeduplicationWorker'
end
include ApplicationWorker
deduplicate strategy
def perform(*args)
self.class.work
end
def self.work
end
2020-04-08 14:13:33 +05:30
end
2021-01-29 00:20:46 +05:30
end
2020-04-08 14:13:33 +05:30
2021-01-29 00:20:46 +05:30
before do
stub_const('TestDeduplicationWorker', worker_class)
end
2020-04-08 14:13:33 +05:30
2021-01-29 00:20:46 +05:30
around do |example|
with_sidekiq_server_middleware do |chain|
chain.add described_class
Sidekiq::Testing.inline! { example.run }
2020-04-08 14:13:33 +05:30
end
end
end
2021-01-29 00:20:46 +05:30
context 'with until_executing strategy' do
include_context 'server duplicate job', :until_executing
2020-04-08 14:13:33 +05:30
2021-01-29 00:20:46 +05:30
describe '#call' do
it 'removes the stored job from redis before execution' do
bare_job = { 'class' => 'TestDeduplicationWorker', 'args' => ['hello'] }
job_definition = Gitlab::SidekiqMiddleware::DuplicateJobs::DuplicateJob.new(bare_job.dup, 'test_deduplication')
expect(Gitlab::SidekiqMiddleware::DuplicateJobs::DuplicateJob)
.to receive(:new).with(a_hash_including(bare_job), 'test_deduplication')
.and_return(job_definition).twice # once in client middleware
expect(job_definition).to receive(:delete!).ordered.and_call_original
expect(TestDeduplicationWorker).to receive(:work).ordered.and_call_original
TestDeduplicationWorker.perform_async('hello')
end
2020-04-08 14:13:33 +05:30
end
end
2021-01-29 00:20:46 +05:30
context 'with until_executed strategy' do
include_context 'server duplicate job', :until_executed
it 'removes the stored job from redis after execution' do
2020-04-08 14:13:33 +05:30
bare_job = { 'class' => 'TestDeduplicationWorker', 'args' => ['hello'] }
job_definition = Gitlab::SidekiqMiddleware::DuplicateJobs::DuplicateJob.new(bare_job.dup, 'test_deduplication')
expect(Gitlab::SidekiqMiddleware::DuplicateJobs::DuplicateJob)
.to receive(:new).with(a_hash_including(bare_job), 'test_deduplication')
.and_return(job_definition).twice # once in client middleware
2021-01-29 00:20:46 +05:30
expect(TestDeduplicationWorker).to receive(:work).ordered.and_call_original
expect(job_definition).to receive(:delete!).ordered.and_call_original
2020-04-08 14:13:33 +05:30
TestDeduplicationWorker.perform_async('hello')
end
end
end