Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize lock state of other semaphores with the same key. #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/redis/semaphore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def locked?(token = nil)
if token
@redis.hexists(grabbed_key, token)
else
@tokens.each do |token|
all_tokens.each do |token|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shadowing outer local variable - token.

return true if locked?(token)
end

Expand Down
12 changes: 12 additions & 0 deletions spec/semaphore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
expect(semaphore.locked?).to eq(false)
end

it "recognizes the lock state of another semaphore with the same key" do
semaphore.lock(1)
expect(semaphore.locked?).to eq(true)
expect(semaphore2.locked?).to eq(true)
semaphore.unlock
expect(semaphore.locked?).to eq(false)
expect(semaphore2.locked?).to eq(false)
end

it "should not lock twice as a mutex" do
expect(semaphore.lock(1)).not_to eq(false)
expect(semaphore.lock(1)).to eq(false)
Expand Down Expand Up @@ -147,6 +156,7 @@

describe "semaphore with expiration" do
let(:semaphore) { Redis::Semaphore.new(:my_semaphore, :redis => @redis, :expiration => 2) }
let(:semaphore2) { Redis::Semaphore.new(:my_semaphore, :redis => @redis, :expiration => 2) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [96/80]
Use the new Ruby 1.9 hash syntax.

let(:multisem) { Redis::Semaphore.new(:my_semaphore_2, :resources => 2, :redis => @redis, :expiration => 2) }

it_behaves_like "a semaphore"
Expand All @@ -170,6 +180,7 @@

describe "semaphore without staleness checking" do
let(:semaphore) { Redis::Semaphore.new(:my_semaphore, :redis => @redis) }
let(:semaphore2) { Redis::Semaphore.new(:my_semaphore, :redis => @redis) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.

let(:multisem) { Redis::Semaphore.new(:my_semaphore_2, :resources => 2, :redis => @redis) }

it_behaves_like "a semaphore"
Expand Down Expand Up @@ -208,6 +219,7 @@

describe "semaphore with staleness checking" do
let(:semaphore) { Redis::Semaphore.new(:my_semaphore, :redis => @redis, :stale_client_timeout => 5) }
let(:semaphore2) { Redis::Semaphore.new(:my_semaphore, :redis => @redis, :stale_client_timeout => 5) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [106/80]
Use the new Ruby 1.9 hash syntax.

let(:multisem) { Redis::Semaphore.new(:my_semaphore_2, :resources => 2, :redis => @redis, :stale_client_timeout => 5) }

it_behaves_like "a semaphore"
Expand Down