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

FakeAsyncRedis stores state when clear instance expected. #293

Closed irusland closed 8 months ago

irusland commented 8 months ago

Describe the bug When creating FakeAsyncRedis each time we get same instance of redis.

To Reproduce

import asyncio

import fakeredis

async def main():
    redis_client_1 = fakeredis.FakeAsyncRedis()
    await redis_client_1.set('k', 'v')

    redis_client_2 = fakeredis.FakeAsyncRedis()
    v2 = await redis_client_2.get('k')

    assert v2 is None, f'Value of new redis is {v2}'

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

produces AssertionError: Value of new redis is b'v'

Expected behavior Code runs successfully same as

import fakeredis

def main():
    redis_client_1 = fakeredis.FakeRedis()
    redis_client_1.set('k', 'v')

    redis_client_2 = fakeredis.FakeRedis()
    v2 = redis_client_2.get('k')

    assert v2 is None, f'Value of new redis is {v2}'

if __name__ == '__main__':
    main()

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context Not sure if it's an intended behaviour. But anyways I expect same behaviour as FakeRedis

Upvote & Fund

Fund with Polar

cunla commented 8 months ago

It is the intended behavior.

You can supply different connection parameters to get different fake servers.

irusland commented 8 months ago

Thanks for quick response!

Could you please provide me an example? Or point out the necessary code in documentation?

irusland commented 8 months ago

Solved it with using different each time port=...

import asyncio

import fakeredis

async def main():
    redis_client_1 = fakeredis.FakeAsyncRedis(port=1)
    await redis_client_1.set('k', 'v')

    redis_client_2 = fakeredis.FakeAsyncRedis(port=2)
    v2 = await redis_client_2.get('k')

    assert v2 is None, f'Value of new redis is {v2}'

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
cunla commented 8 months ago

Yeah, that works. It behaves the same as redis.Redis - by default the connection parameters are localhost:6389.