Skip to content

Commit

Permalink
simplify redis_key
Browse files Browse the repository at this point in the history
  • Loading branch information
somic committed Mar 3, 2021
1 parent e963fda commit f677527
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
8 changes: 1 addition & 7 deletions lib/master_lock/redis_lock.rb
Expand Up @@ -95,13 +95,7 @@ def eval_script(script, script_hash, keys:, argv:)
end

def redis_key
# Key hash tags are a way to ensure multiple keys are allocated in the same hash slot.
# This allows our redis operations to work with clusters
if config.cluster
"{#{config.key_prefix}}:#{key}"
else
"#{config.key_prefix}:#{key}"
end
"#{config.key_prefix}:#{key}"
end

def redis
Expand Down
14 changes: 3 additions & 11 deletions spec/master_lock/redis_lock_spec.rb
Expand Up @@ -60,11 +60,9 @@
it "returns true when lock can be acquired" do
expect(lock1.acquire(timeout: 0)).to eq(true)
expect(redis.get('masterlock:key')).to_not be_nil
expect(redis.get('{masterlock}:key')).to be_nil
# Cluster test
expect(lock3.acquire(timeout: 0)).to eq(true)
expect(cluster.get('masterlock:key')).to be_nil
expect(cluster.get('{masterlock}:key')).to_not be_nil
expect(cluster.get('masterlock:key')).to_not be_nil
end

it "returns false when lock can not be acquired" do
Expand Down Expand Up @@ -138,29 +136,23 @@
it "returns true when lock is held by owner" do
lock1.acquire(timeout: 0)
expect(redis.get('masterlock:key')).to_not be_nil
expect(redis.get('{masterlock}:key')).to be_nil
expect(lock1.release).to eq(true)
expect(redis.get('masterlock:key')).to be_nil
expect(redis.get('{masterlock}:key')).to be_nil
# Cluster test
lock3.acquire(timeout: 0)
expect(cluster.get('masterlock:key')).to be_nil
expect(cluster.get('{masterlock}:key')).to_not be_nil
expect(cluster.get('masterlock:key')).to_not be_nil
expect(lock3.release).to eq(true)
expect(cluster.get('masterlock:key')).to be_nil
expect(cluster.get('{masterlock}:key')).to be_nil
end

it "returns false when lock is held by another owner" do
lock2.acquire(timeout: 0)
expect(lock1.release).to eq(false)
expect(redis.get('masterlock:key')).to_not be_nil
expect(redis.get('{masterlock}:key')).to be_nil
# Cluster test
lock4.acquire(timeout: 0)
expect(lock3.release).to eq(false)
expect(cluster.get('masterlock:key')).to be_nil
expect(cluster.get('{masterlock}:key')).to_not be_nil
expect(cluster.get('masterlock:key')).to_not be_nil
end

it "returns false when lock is not held" do
Expand Down

0 comments on commit f677527

Please sign in to comment.