Skip to content

Commit

Permalink
Raise Redis::CommandError error when setex called with a negative tim…
Browse files Browse the repository at this point in the history
…eout (#174)
  • Loading branch information
bolshakov authored and sds committed Oct 12, 2019
1 parent ee99190 commit 62cfc3c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit 62cfc3c

Please sign in to comment.