Skip to content

Commit

Permalink
fix(unlock): ensure callback and unlock (#771)
Browse files Browse the repository at this point in the history
* chore(deps): update gems (solargraph is awesome)

* fix(unlock): ensure unlock and callback runs

* chore(lint): lint'em real good
# Conflicts:
#	.github/workflows/rspec.yml
#	myapp/.tool-versions
  • Loading branch information
mhenrixon committed Jul 14, 2023
1 parent b3a1fa3 commit bd566bf
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
bundler: 2.3.19
bundler: 2.4.12
bundler-cache: true
- run: bin/bundle --jobs=$(nproc) --retry=$(nproc)
- run: bin/rubocop -P
Expand All @@ -29,7 +29,7 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
bundler: 2.3.19
bundler: 2.4.12
bundler-cache: true
- run: bin/bundle --jobs=$(nproc) --retry=$(nproc)
- run: bin/reek .
8 changes: 4 additions & 4 deletions .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ jobs:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
bundler: 2.3.19
ruby-version: 3.2
bundler: 2.4.12
bundler-cache: true

- name: Install Code Climate reporter
Expand Down Expand Up @@ -59,14 +59,14 @@ jobs:
strategy:
fail-fast: true
matrix:
ruby: [2.5, 2.6, 2.7, '3.0', 3.1]
ruby: [2.7, '3.0', 3.1, 3.2]

steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler: 2.3.19
bundler: 2.4.12
bundler-cache: true
- run: bin/appraisal install --jobs=$(nproc) --retry=$(nproc)
- run: bin/appraisal rspec --require spec_helper --tag ~perf
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ end

if respond_to?(:install_if)
install_if -> { RUBY_PLATFORM.include?("darwin") } do
gem "fasterer"
gem "fuubar"
gem "github_changelog_generator"
gem "pry"
gem "redcarpet", "~> 3.4"
gem "rspec-nc"
gem "ruby-prof", ">= 0.17.0", require: false
gem "stackprof", ">= 0.2.9", require: false
gem "test-prof"
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/sidekiq_5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
source "https://rubygems.org"

gem "appraisal"
gem "faraday-retry"
gem "gem-release"
gem "github-markup"
gem "rack-test"
gem "rake", "13.0.3"
gem "redis-namespace"
gem "reek", ">= 5.3"
gem "rspec"
gem "rspec-benchmark"
Expand Down
1 change: 1 addition & 0 deletions lib/sidekiq_unique_jobs/lock/until_executed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def lock(&block)
def execute
executed = locksmith.execute do
yield
ensure
unlock_and_callback
end

Expand Down
3 changes: 1 addition & 2 deletions lib/sidekiq_unique_jobs/lock/while_executing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def execute(&block)
with_logging_context do
executed = locksmith.execute do
yield
callback_safely if locksmith.unlock
ensure
locksmith.unlock
unlock_and_callback
end

unless executed
Expand Down
4 changes: 4 additions & 0 deletions myapp/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ruby 3.2.2
nodejs 19.1.0
yarn 1.22.19
direnv 2.32.2
10 changes: 8 additions & 2 deletions spec/sidekiq_unique_jobs/lock/while_executing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,33 @@
context "when no callback is defined" do
let(:job_class) { WhileExecutingRescheduleJob }
let(:callback_one) { -> { true } }
let(:callback_two) { nil }
let(:callback_two) { -> { true } }

let(:strategy_one) { process_one.send(:server_strategy) }
let(:strategy_two) { process_two.send(:server_strategy) }

before do
allow(strategy_one).to receive(:call).and_call_original
allow(strategy_two).to receive(:call).and_call_original
allow(process_one).to receive(:reflect).and_call_original
allow(process_two).to receive(:reflect).and_call_original
end

it "reflects execution_failed" do
process_one.execute do
process_two.execute { puts "BOGUS!" }
# NOTE: Below looks weird but tests that
# the result from process_two (which is nil) isn't considered.
jid_one
end

expect(process_one).not_to have_received(:reflect).with(:execution_failed, item_one)
expect(callback_one).to have_received(:call).once
expect(strategy_one).not_to have_received(:call)
expect(strategy_two).to have_received(:call).once

expect(process_two).to have_received(:reflect).with(:execution_failed, item_two)
expect(callback_two).not_to have_received(:call)
expect(strategy_two).to have_received(:call).once
end
end

Expand Down
58 changes: 0 additions & 58 deletions spec/support/shared_examples/a_lockable_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,61 +35,3 @@
end
end
end

RSpec.shared_examples "an executing lock implementation" do
context "when job can't be locked" do
before do
allow(process_one.locksmith).to receive(:execute).and_return(nil)
end

it "does not execute" do
unset = true
process_one.execute { unset = false }
expect(unset).to be(true)
end
end

context "when process_one executes the job" do
before { process_one.lock }

it "keeps being locked while executing" do
process_one.execute do
expect(process_one).to be_locked
end
end

it "keeps being locked when an error is raised" do
allow(process_one.locksmith).to receive(:execute).and_raise(RuntimeError, "Hell")

expect { process_one.execute { "hey ho" } }.to raise_error("Hell")

expect(process_one).to be_locked
end

it "prevents process_two from locking" do
process_one.execute do
expect(process_two.lock).to be_nil
expect(process_two).not_to be_locked
end
end

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

it "reflects execution_failed on failure" do
allow(process_two).to receive(:reflect).and_call_original
process_two.execute { puts "Failed to execute" }

expect(process_two).to have_received(:reflect).with(:execution_failed, item_two)
end

it "yields without arguments" do
process_one.lock
process_one.execute {}
blk = -> {}

expect { process_one.execute(&blk) }.not_to raise_error
end
end
end
59 changes: 59 additions & 0 deletions spec/support/shared_examples/an_executing_lock_implementation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

RSpec.shared_examples "an executing lock implementation" do
context "when job can't be locked" do
before do
allow(process_one.locksmith).to receive(:execute).and_return(nil)
end

it "does not execute" do
unset = true
process_one.execute { unset = false }
expect(unset).to be(true)
end
end

context "when process_one executes the job" do
before { process_one.lock }

it "keeps being locked while executing" do
process_one.execute do
expect(process_one).to be_locked
end
end

it "keeps being locked when an error is raised" do
allow(process_one.locksmith).to receive(:execute).and_raise(RuntimeError, "Hell")

expect { process_one.execute { "hey ho" } }.to raise_error("Hell")

expect(process_one).to be_locked
end

it "prevents process_two from locking" do
process_one.execute do
expect(process_two.lock).to be_nil
expect(process_two).not_to be_locked
end
end

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

it "reflects execution_failed on failure" do
allow(process_two).to receive(:reflect).and_call_original
process_two.execute { puts "Failed to execute" }

expect(process_two).to have_received(:reflect).with(:execution_failed, item_two)
end

it "yields without arguments" do
process_one.lock
process_one.execute {}
blk = -> {}

expect { process_one.execute(&blk) }.not_to raise_error
end
end
end

0 comments on commit bd566bf

Please sign in to comment.