Skip to content

Add guard against releasing slots that haven't been reserved #71

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

Merged
merged 2 commits into from
Feb 19, 2022
Merged
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
17 changes: 13 additions & 4 deletions lib/kredis/types/slots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,36 @@ def reserve
release
end
else
if incr <= available
if available?
incr
true
else
release
false
end
end
end
end

def release
decr
if taken > 0
decr
true
else
false
end
end

def available?
failsafe returning: false do
get.to_i < available
taken < available
end
end

def reset
del
end

def taken
get.to_i
end
end
27 changes: 27 additions & 0 deletions test/types/slots_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ class SlotsTest < ActiveSupport::TestCase
assert @slots.available?
end

test "release when slots are reserved" do
assert_not @slots.release

3.times do
assert @slots.reserve
end

3.times do
assert @slots.release
end

assert_not @slots.release

assert_equal 0, @slots.taken
end

test "reserve with block" do
assert @slots.reserve
assert @slots.reserve
Expand Down Expand Up @@ -70,6 +86,17 @@ class SlotsTest < ActiveSupport::TestCase
assert_not slot.available?
end

test "release single slot when reserved" do
slot = Kredis.slot "myslot"

assert_not slot.release

assert slot.reserve
assert slot.release

assert_not slot.release
end

test "failing open" do
stub_redis_down(@slots) do
assert_not @slots.available?
Expand Down