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

Raise Redis::CommandError error when setex called with a negative timeout #174

Merged
merged 1 commit into from Oct 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/mock_redis/string_methods.rb
Expand Up @@ -313,9 +313,13 @@ def bitcount(key, start = 0, stop = -1)
end

def setex(key, seconds, value)
set(key, value)
expire(key, seconds)
'OK'
if seconds <= 0
raise Redis::CommandError, 'ERR invalid expire time in setex'
else
set(key, value)
expire(key, seconds)
'OK'
end
end

def setnx(key, value)
Expand Down
16 changes: 16 additions & 0 deletions spec/commands/setex_spec.rb
Expand Up @@ -19,4 +19,20 @@
@redises.real.ttl(@key).should > 0
@redises.mock.ttl(@key).should > 0
end

context 'when expiration time is zero' do
it 'raises Redis::CommandError' do
expect do
@redises.setex(@key, 0, 'value')
end.to raise_error(Redis::CommandError, 'ERR invalid expire time in setex')
end
end

context 'when expiration time is negative' do
it 'raises Redis::CommandError' do
expect do
@redises.setex(@key, -2, 'value')
end.to raise_error(Redis::CommandError, 'ERR invalid expire time in setex')
end
end
end