Skip to content

Commit

Permalink
Fix(unlock): Delete primed keys on last entry (#726)
Browse files Browse the repository at this point in the history
* Fix(unlock): Delete primed keys on last entry

* Chore(test): Add test coverage
  • Loading branch information
mhenrixon authored Jul 28, 2022
1 parent 3de1f62 commit 97dd169
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib/sidekiq_unique_jobs/lua/unlock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ if locked_count and locked_count < 1 then
redis.call(del_cmd, locked)
end

if redis.call("LLEN", primed) == 0 then
log_debug(del_cmd, primed)
redis.call(del_cmd, primed)
end

if limit and limit <= 1 and locked_count and locked_count <= 1 then
log_debug("ZREM", digests, digest)
redis.call("ZREM", digests, digest)
Expand Down
18 changes: 14 additions & 4 deletions spec/sidekiq_unique_jobs/lua/unlock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,36 @@
end

shared_examples "unlock with ttl" do
it { expect { unlock }.to change { zcard(key.changelog) }.by(1) }
it { expect { unlock }.to change { ttl(key.digest) }.to(-2) }
it { expect { unlock }.to change { unique_keys }.to(%w[uniquejobs:digest:QUEUED]) }
it { expect { unlock }.to change { llen(key.queued) }.by(1) }

it { expect { unlock }.to change { llen(key.primed) }.by(0) }

it { expect { unlock }.to change { ttl(key.locked) }.to(-2) }
it { expect { unlock }.to change { hget(key.locked, job_id_one) }.to(nil) }

it { expect { unlock }.to change { ttl(key.digest) }.to(-2) }
it { expect { unlock }.to change { zcard(key.digests) }.by(-1) }
it { expect { unlock }.to change { digests.entries }.from(key.digest => kind_of(Float)).to({}) }

it { expect { unlock }.to change { zcard(key.changelog) }.by(1) }
end

shared_examples "unlock without ttl" do
it { expect { unlock }.to change { zcard(key.changelog) }.by(1) }
it { expect { unlock }.to change { ttl(key.digest) }.from(-1).to(-2) }
it { expect { unlock }.to change { unique_keys }.to(%w[uniquejobs:digest:QUEUED]) }
it { expect { unlock }.to change { lrange(key.queued, -1, 0) }.to(["1"]) }
it { expect { unlock }.to change { llen(key.queued) }.by(1) }

it { expect { unlock }.to change { llen(key.primed) }.by(0) }

it { expect { unlock }.to change { ttl(key.locked) }.to(-2) }
it { expect { unlock }.to change { hget(key.locked, job_id_one) }.to(nil) }

it { expect { unlock }.to change { ttl(key.digest) }.from(-1).to(-2) }
it { expect { unlock }.to change { zcard(key.digests) }.by(-1) }
it { expect { unlock }.to change { digests.entries }.from(key.digest => kind_of(Float)).to({}) }

it { expect { unlock }.to change { zcard(key.changelog) }.by(1) }
end

context "with lock: :until_expired", :with_a_lock do
Expand Down

0 comments on commit 97dd169

Please sign in to comment.