Skip to content

Commit

Permalink
Issue 249: when atomic decrbyfloat fails, increment back instead of d…
Browse files Browse the repository at this point in the history
…ecrementing again
  • Loading branch information
samoliuk committed Nov 20, 2019
1 parent f354a45 commit 39c3ee1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/redis/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def incrbyfloat(by=1.0, &block)
def decrbyfloat(by=1.0, &block)
allow_expiration do
val = redis.incrbyfloat(key, -by).to_f
block_given? ? rewindable_block(:incrbyfloat, -by, val, &block) : val
block_given? ? rewindable_block(:incrbyfloat, by, val, &block) : val
end
end

Expand Down
47 changes: 38 additions & 9 deletions spec/redis_objects_instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,44 @@
@counter = Redis::Counter.new("spec/block_counter")
@counter.should == 0
@counter.increment(1)
# The block is never executed.
@updated =
@counter.increment(1) do |updated|
if updated == 2
'yep'
else
raise("test failed")
end
end

# successfully increments
@updated = @counter.increment(1) { |updated| updated == 2 ? 'yep' : nil }
@updated.should == 'yep'
@counter.should == 2

# fails to increment
@updated = @counter.increment(1) { |updated| updated == 2 ? 'yep' : nil }
@updated.should == nil
@counter.should == 2

# successfully increments by float
@updated = @counter.incrbyfloat(1.5) { |updated| updated == 3.5 ? 'yep' : nil }
@updated.should == 'yep'
@counter.should == 3.5

# fails to increment by float
@updated = @counter.incrbyfloat(2.5) { |updated| updated == 5 ? 'yep' : nil }
@updated.should == nil
@counter.should == 3.5

# fails to decrement by float
@updated = @counter.decrbyfloat(0.5) { |updated| updated == 5 ? 'yep' : nil }
@updated.should == nil
@counter.should == 3.5

# successfully decrements by float
@updated = @counter.decrbyfloat(0.5) { |updated| updated == 3 ? 'yep' : nil }
@updated.should == 'yep'
@counter.should == 3

# fails to decrement
@updated = @counter.decrement(1) { |updated| updated == 3 ? 'yep' : nil }
@updated.should == nil
@counter.should == 3

# successfully decrements
@updated = @counter.decrement(1) { |updated| updated == 2 ? 'yep' : nil }
@updated.should == 'yep'
@counter.should == 2
end
Expand Down

0 comments on commit 39c3ee1

Please sign in to comment.