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

Fix XADD maxlen 0 #192

Merged
merged 3 commits into from Jun 25, 2020
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
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