Skip to content

Commit

Permalink
Adds some coverage for the Sidekiq::Job scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
mhenrixon committed Sep 21, 2021
1 parent aa981e7 commit b4e2b0f
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
20 changes: 20 additions & 0 deletions spec/jobs/another_unique_job_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

if Sidekiq.const_defined?("JobRecord")
RSpec.describe AnotherUniqueJobJob do
it_behaves_like "sidekiq with options" do
let(:options) do
{
"queue" => :working2,
"retry" => 1,
"backtrace" => 10,
"lock" => :until_executed,
}
end
end

it_behaves_like "a performing worker", splat_arguments: false do
let(:args) { %w[one two] }
end
end
end
97 changes: 97 additions & 0 deletions spec/sidekiq_unique_jobs/lock/until_and_while_executing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,101 @@
end
end
end

if Sidekiq.const_defined?("JobRecord")
let(:process_one) { described_class.new(item_one, callback) }
let(:runtime_one) { process_one.send(:runtime_lock) }

let(:process_two) { described_class.new(item_two, callback) }
let(:runtime_two) { process_two.send(:runtime_lock) }

let(:jid_one) { "jid one" }
let(:jid_two) { "jid two" }
let(:lock_timeout) { nil }
let(:sleepy_time) { 0 }
let(:worker_class) { AnotherUniqueJobJob }
let(:unique) { :until_and_while_executing }
let(:queue) { :another_queue }
let(:args) { [sleepy_time] }
let(:callback) { -> {} }
let(:item_one) do
{ "jid" => jid_one,
"class" => worker_class.to_s,
"queue" => queue,
"lock" => unique,
"args" => args,
"lock_timeout" => lock_timeout }
end
let(:item_two) do
item_one.merge("jid" => jid_two)
end

before do
allow(runtime_one).to receive(:reflect).and_call_original
allow(runtime_two).to receive(:reflect).and_call_original
allow(process_one).to receive(:runtime_lock).and_return(runtime_one)
allow(process_two).to receive(:runtime_lock).and_return(runtime_two)
end

it_behaves_like "a lock implementation"

it "does not manipulate the original item" do
lock = described_class.new(item_one, callback)
expect { lock.send(:runtime_lock) }.not_to change { item_one["lock_digest"] }
end

it "has not locked runtime_one" do
process_one.lock
expect(runtime_one).not_to be_locked
end

context "when process_one executes the job" do
it "releases the lock for process_one" do
process_one.execute do
expect(process_one).not_to be_locked
end
end

it "is locked by runtime_one" do
process_one.execute do
expect(runtime_one).to be_locked
end
end

it "allows process_two to lock" do
process_one.execute do
expect(process_two.lock).to eq(jid_two)
end
end

it "prevents process_two from executing" do
process_one.lock
expect { process_two.execute { raise "Hell" } }.not_to raise_error
end

it "process two cannot execute the job" do
process_one.execute do
process_two.lock
unset = true
process_two.execute { unset = false }
expect(unset).to eq(true)
end
end

context "when worker raises error in runtime lock" do
before do
allow(runtime_one.locksmith).to receive(:execute).and_raise(RuntimeError, "Hell")
end

it "always unlocks" do
process_one.lock

expect { process_one.execute {} }
.to raise_error(RuntimeError, "Hell")

expect(process_one).to be_locked
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

RSpec.describe SidekiqUniqueJobs::RSpec::Matchers::HaveValidSidekiqOptions do
describe "#matches?" do
if Sidekiq.const_defined?("JobRecord")
context "when sidekiq options are valid" do
it { expect(AnotherUniqueJob).to have_valid_sidekiq_options }
end

context "when sidekiq options lacks `:lock`" do
it "raises SidekiqUniqueJobs::NotUniqueWorker" do
AnotherUniqueJob.use_options({}) do
AnotherUniqueJob.sidekiq_options_hash.delete("lock")
expect { expect(AnotherUniqueJob).to have_valid_sidekiq_options }
.to raise_error(SidekiqUniqueJobs::NotUniqueWorker)
end
end
end

context "when sidekiq options are invalid" do
it "raises SidekiqUniqueJobs::NotUniqueWorker" do
AnotherUniqueJob.use_options(on_client_conflict: :reject, on_server_conflict: :replace) do
expect(AnotherUniqueJob).not_to have_valid_sidekiq_options
end
end
end
end

context "when sidekiq options are valid" do
it { expect(AnotherUniqueJob).to have_valid_sidekiq_options }
end
Expand Down
18 changes: 18 additions & 0 deletions spec/support/jobs/another_unique_job_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# :nocov:
if Sidekiq.const_defined?("JobRecord")
require 'sidekiq/job'

class AnotherUniqueJobJob
include Sidekiq::Job
sidekiq_options backtrace: 10,
lock: :until_executed,
queue: :working2,
retry: 1

def perform(args)
args
end
end
end

0 comments on commit b4e2b0f

Please sign in to comment.