cunla / fakeredis-py

Implementation of Redis in python without having a Redis server running. Fully compatible with using redis-py.
https://fakeredis.moransoftware.ca/
BSD 3-Clause "New" or "Revised" License
298 stars 48 forks source link

Reading multiple streams does not give expected result #309

Closed mguijarr closed 5 months ago

mguijarr commented 6 months ago

Describe the bug

When reading multiple streams, if block is set fakeredis stops at the first stream whereas the real Redis gives output for all streams as expected.

To Reproduce

import redis
from fakeredis import FakeServer, FakeStrictRedis

def main():
    fake_server = FakeServer()
    fake_client = FakeStrictRedis(server=fake_server)
    real_client = redis.Redis(host="localhost", port=6379, db=1)

    k = "key"
    k2 = "key2"
    streams = {k: "0", k2: "0"}

    real_client.xadd(k, {"value":1234})
    real_client.xadd(k2, {"value": 5678})
    res = real_client.xread(streams, block=200)
    assert len(res) == 2
    print("Success: read 2 streams", res)

    fake_client.xadd(k, {"value":1234})
    fake_client.xadd(k2, {"value": 5678})
    res = fake_client.xread(streams, block=200)
    assert len(res) == 2, "Could not read 2 streams"

Expected behavior

Expected behaviour: the assertion should not raise, res for fake_client should be the same as res for real_client

It can be narrowed down in file fakeredis/commands_mixins/streams_mixin.py in method _xread:

...
for stream_name, start_id in stream_start_id_list:
            item = CommandItem(stream_name, self._db, item=self._db.get(stream_name), default=None)
            stream_results = self._xrange(item.value, start_id, max_inf, False, count)
            if len(stream_results) > 0:
                res.append([item.key, stream_results])
            if first_pass and (count is None): ###
                return res                                    ### why the early exit ? there might be other items in list
...

Note: when removing block in fake_client.xread in the example above, it seems to work ❓ 🤔 (except that in real life the code really needs block)

Desktop (please complete the following information):

Upvote & Fund

Fund with Polar

mguijarr commented 5 months ago

Thanks @cunla 👍🏻

mguijarr commented 5 months ago

I would like to know if there is going to be a new release with this bug fix soon? And thanks again! The simplified version looks nice 👍🏻

cunla commented 5 months ago

This or next weekend