Skip to content

Return counter value after incrementing/decrementing #72

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
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
4 changes: 2 additions & 2 deletions lib/kredis/types/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ def increment(by: 1)
multi do
set 0, ex: expires_in, nx: true
incrby by
end
end[-1]
end

def decrement(by: 1)
multi do
set 0, ex: expires_in, nx: true
decrby by
end
end[-1]
end

def value
Expand Down
8 changes: 8 additions & 0 deletions test/types/counter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,35 @@ class CounterTest < ActiveSupport::TestCase

@counter.increment
assert_equal 2, @counter.value

assert_equal 3, @counter.increment
end

test "increment by 2" do
assert_equal 0, @counter.value

@counter.increment by: 2
assert_equal 2, @counter.value

assert_equal 4, @counter.increment(by: 2)
end

test "decrement" do
assert_equal 0, @counter.value

@counter.decrement
assert_equal (-1), @counter.value

assert_equal (-2), @counter.decrement
end

test "decrement by 2" do
assert_equal 0, @counter.value

@counter.decrement by: 2
assert_equal (-2), @counter.value

assert_equal (-4), @counter.decrement(by: 2)
end

test "expiring counter" do
Expand Down