Skip to content

Commit

Permalink
Fix XADD maxlen 0 (#192)
Browse files Browse the repository at this point in the history
* XADD maxlen 0 creates empty stream

* Rubocop

* Ignore emptry streams on cleanup
  • Loading branch information
jmthomas committed Jun 25, 2020
1 parent 919f0df commit 8ee7af4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/mock_redis/stream.rb
Expand Up @@ -37,7 +37,11 @@ def add(id, values)
def trim(count)
deleted = @members.size - count
if deleted > 0
@members = @members.to_a[-count..-1].to_set
@members = if count == 0
Set.new
else
@members.to_a[-count..-1].to_set
end
deleted
else
0
Expand Down
2 changes: 1 addition & 1 deletion lib/mock_redis/utility_methods.rb
Expand Up @@ -18,7 +18,7 @@ def primitive?(value)
end

def clean_up_empties_at(key)
if data[key]&.empty? && data[key] != ''
if data[key]&.empty? && data[key] != '' && !data[key].is_a?(Stream)
del(key)
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/commands/del_spec.rb
@@ -1,6 +1,14 @@
require 'spec_helper'

describe '#del(key [, key, ...])' do
before :all do
sleep 1 - (Time.now.to_f % 1)
end

before :each do
@redises._gsub(/\d{3}-\d/, '...-.')
end

it 'returns the number of keys deleted' do
@redises.set('mock-redis-test:1', 1)
@redises.set('mock-redis-test:2', 1)
Expand Down Expand Up @@ -32,4 +40,11 @@
it 'raises an error if an empty array is given' do
expect { @redises.del [] }.to raise_error Redis::CommandError
end

it 'removes a stream key' do
@redises.xadd('mock-redis-stream', { key: 'value' }, maxlen: 0)
expect(@redises.exists?('mock-redis-stream')).to eq true
@redises.del('mock-redis-stream')
expect(@redises.exists?('mock-redis-stream')).to eq false
end
end
7 changes: 7 additions & 0 deletions spec/commands/xadd_spec.rb
Expand Up @@ -112,4 +112,11 @@
]
)
end

it 'creates an empty stream with maxlen of 0' do
@redises.xadd(@key, { key: 'value' }, maxlen: 0)
expect(@redises.xlen(@key)).to eq 0
expect(@redises.xrange(@key, '-', '+')).to eq([])
expect(@redises.exists?(@key)).to eq true
end
end

0 comments on commit 8ee7af4

Please sign in to comment.