Open M1ha-Shvn opened 1 year ago
Both stack traces complain about queues or tasks being attached to different event loops. Two things come to my mind:
--setup-show
.As to why this occurs only from Python 3.10 onwards, I don't know.
Thanks for comment. I'll have a look
@M1ha-Shvn Any updates on your issue?
I guess, my problem is a result of architecture: Redis storage connection object realises Singleton pattern. It creates single object and reuses Redis instance. For testing purposes it replaces Redis with FakeRedis. When tests are run each test is run in separate loop, but singleton object is still the same (as python thread is the same) which leads to loop changing problems when calling Redis commands.
I've fixed the problem for myself by 2 things:
A hacky solution regenerating FakeRedis object on loop change:
current_loop = asyncio.get_event_loop()
if self._loop is not None and self._loop is not current_loop:
self._redis_clients.clear()
# Script should be created in same loop, where it is executed
self._generate_worker_id_script = None
log('asyncio_loop_changed', 'asyncio_loop_changed', level=logging.WARNING)
self._loop = current_loop
conftest.py
. I'm not exactly sure why, but without this some clean up fixtures inside tests didn't work properly and left my FakeRedis database in unclear state
@pytest.fixture(scope="session", autouse=True)
def event_loop() -> Generator["AbstractEventLoop", Any, None]:
policy = get_event_loop_policy()
loop = policy.new_event_loop()
yield loop
loop.close()
Maybe there's a smarter way for pytest-asyncio to detect loop changes. I'll leave the issue open for the time being.
Faced the same problem when running tests locally
It is interesting that the error is reproduced only when run tests module like pytest path/to/module/with/tests
. If run purest for all tests like pytest .
, there will be no error
Hi. I'm using fakeredis to run tests via pytest-asyncio in FastAPI application.
Library versions:
Everything works well in python 3.9 and earlier. But in 3.10 and 3.11 I start getting exceptions:
If I disable FakeRedis and use redis-py in tests without isolation, I start getting this error:
Firstly, I thought, it was a problem in Fakeredis and created an issue there. But now I suppose, that it is something connected with pytest-asyncio as:
Can you suggest anything and where to dig more? Thanks