Skip to content

Commit

Permalink
Fix missing cursor when no data is found in the zscan method
Browse files Browse the repository at this point in the history
  • Loading branch information
thukim committed May 26, 2024
1 parent c28f7ac commit 9561907
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/redis/connection/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ def punsubscribe(*patterns)

def zscan(key, start_cursor, *args)
data_type_check(key, ZSet)
return [] unless data[key]
return '0', [] unless data[key] # return cursor 0, indicating end of iteration

match = "*"
count = 10
Expand Down
51 changes: 31 additions & 20 deletions spec/sorted_sets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,30 +497,41 @@ module FakeRedis
end

describe "#zscan" do
before { 50.times { |x| @client.zadd("key", x, "key #{x}") } }

it 'with no arguments should return 10 numbers in ascending order' do
result = @client.zscan("key", 0)[1]
expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] })
expect(result.count).to eq(10)
context 'without data' do
it 'return cursor 0 with an empty array' do
result = @client.zscan("notexistkey", 0)
expect(result.count).to eq 2
expect(result[0]).to eq '0'
expect(result[1]).to eq []
end
end

it 'with a count should return that number of members' do
expect(@client.zscan("key", 0, count: 2)).to eq(["2", [["key 0", 0.0], ["key 1", 1.0]]])
end
context 'with data' do
before { 50.times { |x| @client.zadd("key", x, "key #{x}") } }

it 'with a count greater than the number of members, returns all the members in asc order' do
result = @client.zscan("key", 0, count: 1000)[1]
expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] })
expect(result.size).to eq(50)
end
it 'with no arguments should return 10 numbers in ascending order' do
result = @client.zscan("key", 0)[1]
expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] })
expect(result.count).to eq(10)
end

it 'with match, should return key-values where the key matches' do
@client.zadd("key", 1.0, "blah")
@client.zadd("key", 2.0, "bluh")
result = @client.zscan("key", 0, count: 100, match: "key*")[1]
expect(result).to_not include(["blah", 1.0])
expect(result).to_not include(["bluh", 2.0])
it 'with a count should return that number of members' do
expect(@client.zscan("key", 0, count: 2)).to eq(["2", [["key 0", 0.0], ["key 1", 1.0]]])
end

it 'with a count greater than the number of members, returns all the members in asc order' do
result = @client.zscan("key", 0, count: 1000)[1]
expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] })
expect(result.size).to eq(50)
end

it 'with match, should return key-values where the key matches' do
@client.zadd("key", 1.0, "blah")
@client.zadd("key", 2.0, "bluh")
result = @client.zscan("key", 0, count: 100, match: "key*")[1]
expect(result).to_not include(["blah", 1.0])
expect(result).to_not include(["bluh", 2.0])
end
end
end

Expand Down

0 comments on commit 9561907

Please sign in to comment.