Skip to content

Commit

Permalink
write basic specs ; clean up setup and file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Azdaroth committed Oct 24, 2024
1 parent 20e5c7f commit 35264bc
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,5 @@ Style/StringLiterals:
Style/Documentation:
Enabled: false


RSpec/AnyInstance:
Enabled: false
2 changes: 2 additions & 0 deletions lib/sidekiq-poison-pill-remedy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "sidekiq_poison_pill_remedy"
require "active_support"
require "active_support/inflector"
16 changes: 0 additions & 16 deletions lib/sidekiq_worker_loader.rb

This file was deleted.

1 change: 1 addition & 0 deletions sidekiq-poison-pill-remedy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 43 additions & 29 deletions spec/sidekiq_poison_pill_remedy_spec.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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
8 changes: 3 additions & 5 deletions spec/support/my_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 35264bc

Please sign in to comment.