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

Using watch within a pipline will use a different server_key if host/port are not provided #212

Closed OlegZv closed 1 year ago

OlegZv commented 1 year ago

Describe the bug Using pipe.watch(key) within a pipeline with a client that was created without host or port the client inside the pipeline will use a different server. More generally, if Redis is trying to establish another connection (socket) with a server, it will get a connection to a new empty FakeRedis each time.

To Reproduce

from fakeredis import FakeRedis
fr = FakeRedis() 
fr.set("foo", "barr")
fr.set("boo", "bar")
with fr.pipeline() as pipe:
    keys_before_watch = set(fr.keys())
    pipe.watch("foo")
    assert set(fr.keys()) == keys_before_watch # Fails

Expected behavior I expect FakeRedis to have the same data with and without a pipeline if I'm using the same instance of the FakeRedis.

Desktop (please complete the following information):

Additional context I wasn't sure if this is a bug really, because this can also be remediated by creating FakeRedis with some random values of host and port. The example above would work if you initialize fr with fr = FakeRedis(host='blah', port=0). But to me, it seems more like a workaround, so I opened a PR.

OlegZv commented 1 year ago

This is related to #140 and #142. I believe as of #140 this was still working. After #142 a new connection from the connection pool would be created to a new/fresh FakeServer within the same pipeline.