Skip to content

Commit

Permalink
fix:xread bug (#257)
Browse files Browse the repository at this point in the history
Fix #256
  • Loading branch information
cunla committed Dec 5, 2023
1 parent b629adb commit 9ba929e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
20 changes: 3 additions & 17 deletions fakeredis/commands_mixins/streams_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _xread(self, stream_start_id_list: List, count: int, first_pass: bool):
res = list()
for item, start_id in stream_start_id_list:
stream_results = self._xrange(item.value, start_id, max_inf, False, count)
if first_pass and (count is None or len(stream_results) < count):
if first_pass and (count is None):
return None
if len(stream_results) > 0:
res.append([item.key, stream_results])
Expand Down Expand Up @@ -137,22 +137,8 @@ def _parse_start_id(key: CommandItem, s: bytes) -> StreamRangeTest:

@command(name="XREAD", fixed=(bytes,), repeat=(bytes,))
def xread(self, *args):
(
count,
timeout,
), left_args = extract_args(
args,
(
"+count",
"+block",
),
error_on_unexpected=False,
)
if (
len(left_args) < 3
or not casematch(left_args[0], b"STREAMS")
or len(left_args) % 2 != 1
):
(count, timeout,), left_args = extract_args(args, ("+count", "+block",), error_on_unexpected=False, )
if (len(left_args) < 3 or not casematch(left_args[0], b"STREAMS") or len(left_args) % 2 != 1):
raise SimpleError(msgs.SYNTAX_ERROR_MSG)
left_args = left_args[1:]
num_streams = int(len(left_args) / 2)
Expand Down
7 changes: 7 additions & 0 deletions test/test_mixins/test_streams_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ def test_xread(r: redis.Redis):
assert r.xread(streams={stream: m2}) == []


def test_xread_count(r: redis.Redis):
r.xadd("test", {"x": 1})
result = r.xread(streams={"test": "0"}, count=100, block=10)
assert result[0][0] == b"test"
assert result[0][1][0][1] == {b'x': b'1'}


def test_xread_bad_commands(r: redis.Redis):
with pytest.raises(redis.ResponseError) as exc_info:
testtools.raw_command(r, 'xread', 'foo', '11-1')
Expand Down

0 comments on commit 9ba929e

Please sign in to comment.