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

Add xread block and count support #194

Merged
merged 1 commit into from Jul 2, 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
7 changes: 5 additions & 2 deletions lib/mock_redis/stream.rb
Expand Up @@ -60,9 +60,12 @@ def range(start, finish, reversed, *opts_in)
items
end

def read(id)
def read(id, *opts_in)
opts = options opts_in, %w[count block]
stream_id = MockRedis::Stream::Id.new(id)
members.select { |m| (stream_id < m[0]) }.map { |m| [m[0].to_s, m[1]] }
items = members.select { |m| (stream_id < m[0]) }.map { |m| [m[0].to_s, m[1]] }
return items.first(opts['count'].to_i) if opts.key?('count')
items
end

def each
Expand Down
8 changes: 5 additions & 3 deletions lib/mock_redis/stream_methods.rb
Expand Up @@ -66,14 +66,16 @@ def xrevrange(key, last = '+', first = '-', count: nil)
end
end

# TODO: Implement count and block parameters
def xread(keys, ids)
def xread(keys, ids, count: nil, block: nil)
args = []
args += ['COUNT', count] if count
args += ['BLOCK', block.to_i] if block
result = {}
keys = keys.is_a?(Array) ? keys : [keys]
ids = ids.is_a?(Array) ? ids : [ids]
keys.each_with_index do |key, index|
with_stream_at(key) do |stream|
data = stream.read(ids[index])
data = stream.read(ids[index], *args)
result[key] = data unless data.empty?
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/commands/xread_spec.rb
Expand Up @@ -47,4 +47,20 @@
expect(@redises.xread([@key, @key1], %w[1234567891234-2 1234567891234-1]))
.to eq({ @key1 => [['1234567891234-2', { 'key1' => 'value2' }]] })
end

it 'supports the block parameter' do
Copy link
Author

@ghost ghost Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sds I accepted the block parameter but don't do anything with it. Do you implement blocking anywhere else in the mock_redis? I had considered just doing a sleep block/1000.0 but didn't see sleep anywhere else in the code base. Of course only doing the sleep if the result was empty.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will rely on users of this library to report any issues with what you've implemented, and provide a PR fixing the logic to add the support if it is needed. Thank you!

@redises.xadd(@key, { key: 'value' }, id: '1234567891234-0')
expect(@redises.xread(@key, '0-0', block: 1000))
.to eq({ @key => [['1234567891234-0', { 'key' => 'value' }]] })
end

it 'limits results with count' do
@redises.xadd(@key, { key: 'value' }, id: '1234567891234-0')
@redises.xadd(@key, { key: 'value' }, id: '1234567891234-1')
@redises.xadd(@key, { key: 'value' }, id: '1234567891234-2')
expect(@redises.xread(@key, '0-0', count: 1))
.to eq({ @key => [['1234567891234-0', { 'key' => 'value' }]] })
expect(@redises.xread(@key, '1234567891234-0', count: 1))
.to eq({ @key => [['1234567891234-1', { 'key' => 'value' }]] })
end
end