fakeredis-py: FastAPI integration Async Redis session dependency override, error during test RuntimeError: is bound to a different event loop
Describe the bug I am currently developing a FastAPI app that gets a Redis Session from a Dependency Injection, and uses it to write some data on Redis. I need to test the endpoint, therefore I created a pytext fixture which in turn:
- create a FakeAsyncRedis
- override the dependency in the endpoint
- return the same FakeAsyncRedis object
in the test basically I call the endpoint, and I read the key I expect to get set, but I get the following error
test_redis - RuntimeError: <Queue at 0x111538a10 maxsize=0 tasks=1> is bound to a different event loop
To Reproduce My fixtures are:
@pytest.fixture(scope="session")
def event_loop():
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
yield loop
loop.close()
@pytest.fixture
async def redis(event_loop: asyncio.AbstractEventLoop):
redis_session = fakeredis.FakeAsyncRedis(decode_responses=True, encoding="utf-8", )
async def _get_async_redis_session() -> AsyncIterator[fakeredis.FakeAsyncRedis]:
yield redis_session
for app in all_fast_apps:
app.dependency_overrides[get_async_redis_session] = _get_async_redis_session
return redis_session
This is my dependency used to fetch a Redis Connection:
from redis import asyncio as aioredis
async def get_async_redis_session() -> AsyncIterator[Redis]:
async with aioredis.from_url(redisUrl, **redis_args) as session:
yield session
await session.close()
and this is the endpoint definition:
async def write_on_redis(
request: Request,
redis: Redis = Depends(get_async_redis_session)
) :
await redis.set(name=key, value=value, ex=expire)
Finally the test where I get the error:
def test_write(redis): # redis is the fixture we defined before
url = "myapiendpoint"
response = client.get(url)
value = await redis.get(key) <-- this throws the error
Expected behavior Get the key set before in the endpoint
Desktop (please complete the following information):
- OS: [e.g. iOS]
- fakeredis==2.21.1
- redis==4.6.0
Upvote & Fund
- We’re using Polar.sh so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (11 by maintainers)
@cunla I have created a standalone FastAPI app that uses Redis, and written a test for it to demonstrate the dependency override. Then I’ve copied that code into your docs and the PR is [edit: 311, which I have closed]
This does not need the eventloop fixture like in @christiansicari 's original question. I believe this to be due to improvements in pytest-asyncio since this question was asked, since my project used to need the same thing and no longer does.
I should probably warn that this code is a bit scrappy. It doesn’t bother undoing the modification to
app.dependency_overrides, which happens to work with my code because every test that uses the app at all, uses it via this fixture and wants the same dependency override. It wouldn’t work if I had different tests that used a different fixture and didn’t want the override of that other dependency.@christiansicari there’s a bit of redaction here since it’s proprietary code, and I haven’t included all the imports, but it’s a bit like this:
In
main.py,create_backendis not the function used infastapi.Depends(I don’t think patching that would work, since it’s bound by value and not by name before the test code gets a chance to patch it). Rather, it’s a function called by name in the function used inDepends.Then I have a bunch of async tests that use the
clientfixture to make async http calls on the app object, and test its API that way. Separately I have async tests for theRedisBackendobject, that use theredis_connfixture directly. What I could never get working, due to the event loop error, was synchronous tests usingfastapi.testclient.TestClient.It might be that the code here is obvious, and my key insight was to give up on trying to write synchronous tests. The code you posted has
def test_write ...await redis.get. I would expect that to never possibly work, just because you can’tawaitin a sync function, but it’s not clear to me whether your tests are actually sync or async.