diff --git a/.rubocop.yml b/.rubocop.yml index 6aee70e..469aab1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -111,4 +111,5 @@ Style/StringLiterals: Style/Documentation: Enabled: false - +RSpec/AnyInstance: + Enabled: false diff --git a/lib/sidekiq-poison-pill-remedy.rb b/lib/sidekiq-poison-pill-remedy.rb index bd456f4..9625472 100644 --- a/lib/sidekiq-poison-pill-remedy.rb +++ b/lib/sidekiq-poison-pill-remedy.rb @@ -1,3 +1,5 @@ # frozen_string_literal: true require "sidekiq_poison_pill_remedy" +require "active_support" +require "active_support/inflector" diff --git a/lib/sidekiq_worker_loader.rb b/lib/sidekiq_worker_loader.rb deleted file mode 100644 index 7fcf196..0000000 --- a/lib/sidekiq_worker_loader.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -require "sidekiq" -require "sidekiq/api" -require "sidekiq/testing" -require "rspec-sidekiq" -require "sidekiq-poison-pill-remedy" -require_relative "jobs/my_job" - -Sidekiq.configure_server do |config| - config.redis = { url: ENV['REDIS_URL'] || 'redis://localhost:6379/0' } -end - -Sidekiq.configure_client do |config| - config.redis = { url: ENV['REDIS_URL'] || 'redis://localhost:6379/0' } -end diff --git a/sidekiq-poison-pill-remedy.gemspec b/sidekiq-poison-pill-remedy.gemspec index 5896b3e..5f23c1c 100644 --- a/sidekiq-poison-pill-remedy.gemspec +++ b/sidekiq-poison-pill-remedy.gemspec @@ -33,6 +33,7 @@ Gem::Specification.new do |spec| # Uncomment to register a new dependency of your gem # spec.add_dependency "example-gem", "~> 1.0" + spec.add_dependency "activesupport", ">= 6.1" spec.add_dependency "sidekiq" # For more information and examples about making a new gem, check out our diff --git a/spec/sidekiq_poison_pill_remedy_spec.rb b/spec/sidekiq_poison_pill_remedy_spec.rb index 526af06..91f9029 100644 --- a/spec/sidekiq_poison_pill_remedy_spec.rb +++ b/spec/sidekiq_poison_pill_remedy_spec.rb @@ -1,35 +1,49 @@ # frozen_string_literal: true -require "sidekiq" -require "sidekiq/api" -require "sidekiq/testing" -require "rspec-sidekiq" -require "sidekiq-poison-pill-remedy" -require "support/my_job" +require "spec_helper" RSpec.describe SidekiqPoisonPillRemedy do - before do - Sidekiq::Testing.fake! - end - - it "moves job to poison_pill queue and logs message" do - puts "Starting test..." - - job_id = MyJob.perform_async(nil) - - - expect(Sidekiq::Queue.new.size).to eq(1) - expect(Sidekiq::Queue.new("poison_pill").size).to eq(0) - puts "2" - - job = Sidekiq::Queue.new.find_job(job_id) - - SidekiqPoisonPillRemedy.remedy.call(nil, job) - - puts "Jobs in Queue: #{Sidekiq::Queue.new.size}" - puts "3" - - expect(Sidekiq::Queue.new.size).to eq(0) - expect(Sidekiq::Queue.new("poison_pill").size).to eq(1) + describe ".remedy" do + subject(:call) { described_class.remedy.call(nil, job) } + + let(:default_queue) { "default" } + let(:poison_pill_queue) { "poison_pill" } + let(:enqueue_job) { MyJob.set(queue: job_queue).perform_async("fail") } + let(:job) { Sidekiq::Queue.new(default_queue).find_job(enqueue_job) } + + before do + Sidekiq::Testing.disable! + Sidekiq::Queue.new(poison_pill_queue).clear + Sidekiq::Queue.new(default_queue).clear + + enqueue_job + + # there is no easy way to move the job to DeadSet, the process is rather complex + # we would ideally execute a single method call have a proper setup but in that case + # we need to use stub + allow_any_instance_of(Sidekiq::DeadSet).to receive(:find_job).with(enqueue_job).and_return(job) + end + + context "when the job is a poison pill in non-poison pill queue" do + let(:job_queue) { default_queue } + + it "moves job to poison_pill queue and sends error notification" do + expect do + call + end.to change { Sidekiq::Queue.new(default_queue).count }.from(1).to(0) + .and change { Sidekiq::Queue.new(poison_pill_queue).count }.from(0).to(1) + end + end + + context "when the job is a poison pill in poison pill queue" do + let(:job_queue) { poison_pill_queue } + + it "keep the jobs in posion pill queue and sends error notification" do + expect do + call + end.to avoid_changing { Sidekiq::Queue.new(default_queue).count }.from(0) + .and avoid_changing { Sidekiq::Queue.new(poison_pill_queue).count }.from(1) + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 70faa9a..a830f93 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,7 +5,9 @@ require "rspec-sidekiq" require "sentry-ruby" require "sidekiq" +require "sidekiq/api" require "sidekiq-poison-pill-remedy" +require "support/my_job" Dir[File.join(__dir__, "support", "**", "*.rb")].each { |f| require f } @@ -24,4 +26,14 @@ config.before(:all) do ENV["REDIS_URL"] ||= "redis://localhost:6379/1" end + + Sidekiq.configure_server do |sidekiq_config| + sidekiq_config.redis = { url: ENV["REDIS_URL"] || "redis://localhost:6379/0" } + end + + Sidekiq.configure_client do |sidekiq_config| + sidekiq_config.redis = { url: ENV["REDIS_URL"] || "redis://localhost:6379/0" } + end + + RSpec::Matchers.define_negated_matcher :avoid_changing, :change end diff --git a/spec/support/my_job.rb b/spec/support/my_job.rb index c0d57c7..b4661e3 100644 --- a/spec/support/my_job.rb +++ b/spec/support/my_job.rb @@ -5,10 +5,8 @@ class MyJob sidekiq_options retry: 3 def perform(arg) - if arg.nil? - raise StandardError, "Job was called with nil argument" - else - puts "Job executed successfully with argument: #{arg}" - end + raise StandardError, "Job was called with nil argument" if arg.nil? + + puts "Job executed successfully with argument: #{arg}" end end