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:

  1. create a FakeAsyncRedis
  2. override the dependency in the endpoint
  3. 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.
<picture> <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/cunla/fakeredis-py/issues/292/pledge.svg?darkmode=1"> Fund with Polar </picture>

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (11 by maintainers)

Most upvoted comments

@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:

from fakeredis.aioredis import FakeRedis
from httpx import AsyncClient
from .main import app  # this is the fastapi app object
from .main import RedisBackend # this is a class with a bunch of methods with the Redis code for the actions I perform

@pytest_asyncio.fixture(scope="function")
async def redis_conn() -> AsyncIterator[Redis[str]]:
    # Actually I have some code here to return fakeredis or real redis according to a pytest 
    # command-line option, but that's not really relevant. It's the reason I bother here with
    # the async context, though. There's no cleanup needed with FakeRedis alone.
    async with FakeRedis(decode_responses=True, version=(6,)) as redis_conn:
        await redis_conn.flushdb()
        yield redis_conn

@pytest_asyncio.fixture(scope="function")
async def client(redis_conn: Redis[str]) -> AsyncIterator[AsyncClient]:
    with mock.patch("main.create_backend", return_value=RedisBackend(redis_conn)):
        app.dependency_overrides[_some_other_injected_dependency] = _some_other_override
        async with AsyncClient(app=app, base_url="http://testserver") as client:
            yield client

In main.py, create_backend is not the function used in fastapi.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 in Depends.

Then I have a bunch of async tests that use the client fixture to make async http calls on the app object, and test its API that way. Separately I have async tests for the RedisBackend object, that use the redis_conn fixture directly. What I could never get working, due to the event loop error, was synchronous tests using fastapi.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’t await in a sync function, but it’s not clear to me whether your tests are actually sync or async.