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
281 stars 47 forks source link

`FakeRedis.smembers()` reads data added to other instances (in version 2.21.2) #298

Closed Zac-HD closed 6 months ago

Zac-HD commented 6 months ago

Describe the bug New instances of FakeRedis() read data added to older instances. This is new in version 2.21.2, but a quick skim of https://github.com/cunla/fakeredis-py/compare/v2.21.1...v2.21.2 didn't show an obvious culprit.

To Reproduce

from fakeredis import FakeRedis

for val in (b"1", b"2"):
    db = FakeRedis()  # <-- should be fresh each time!
    db.sadd(b"key", val)
    x = db.smembers(b"key")
    assert x == {val}, f"{x=} {val=}"
Traceback (most recent call last):
  File "repro.py", line 7, in <module>
    assert x == {val}, f"{x=} {val=}"
AssertionError: x={b'2', b'1'} val=b'2'

(found by the https://github.com/HypothesisWorks/hypothesis/ test suite, minimized by hand)

cunla commented 6 months ago

Hi, this is not a bug. It aims to emulate redis behavior. When not supplying connection parameters - it will connect to "localhost:6389" server, which keeps a state.

This is the relevant change.

If you want a fresh instance, provide different connection parameters.

Zac-HD commented 6 months ago

Oh, I see! That makes sense, but it'd be great to explain this in the changelog - I suspect I'm not going to be the only person to run into this.

Thanks for maintaining such a useful library 😊

cunla commented 6 months ago

Yes, you are right, someone already opened another similar issue. Do you mind updating the changelog in a way that will be clear?

Zac-HD commented 6 months ago

https://github.com/cunla/fakeredis-py/commit/c0e620af93839f2761c8c0be2ecc6c531617ce0b looks good to me!