-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to fix "attached to a different loop"? #38
Comments
Hi, the first thing you can try is this:
i.e. make your app fixture depend on the event loop fixture. This should make your client instance get the loop actually used in the test (even if you don't actually use the argument, the event loop will get installed as the default loop for the duration of the test). This will also make your client fixture function-scoped, but it's a good starting point. |
Hi Tinche, thanks for the incredibly fast response. You suggestions seems to work. However "This will also make your client fixture function-scoped" is something I'd rather like to avoid as it doesn't exactly increase the speed of the tests. What's the rationale for pytest-asyncio to create a new loop for each test? Not to share resources between tests? Maybe your suggestion is the only viable solution then. Any idea, why my attempt to inject the loop into the client does not work? Cannot make any sense of the error message. |
The AsyncElasticsearch instance probably grabs the loop and uses it somewhere before you can change it in the test. Yeah, ideally tests should run in total isolation. The reason why your client fixture must be function scoped is that the event loop fixture is function scoped. You can override the event loop fixture to be session scoped though. I'm not sure this is tested but it should be possible :) Make a conftest.py file in your tests directory, and put this in it:
|
Ok, thanks for the valuable input. |
Hi there, So following from this issue, I'm trying to see if we could use this library to run async tests dependent on i/o in parallel. I've been trying the following example but couldn't get it to work asynchronously (total test takes 20 seconds instead of the desired 10) import asyncio
import pytest
@pytest.yield_fixture(scope='session')
def event_loop(request):
"""Create an instance of the default event loop for each test case."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio
async def test_example1(event_loop):
"""No marker!"""
await asyncio.sleep(10, loop=event_loop)
async def test_example2(event_loop):
"""No marker!"""
await asyncio.sleep(10, loop=event_loop) If anyone could give me input, I'd greatly appreciate it! |
Hello! |
@david-shiko What exactly do you mean? |
I got an error:
|
Why do you need the What happens if you try this:
|
When error occurs server will always return 503 status code. Line that causing an error: |
Please open up a different issue for this, with a small repro case and I will look at it. |
@Tinche #207 (comment). Thanks. |
@Tinche what are the implications of the suggested workaround?
In our case we create an sqlalchemy async engine at the module level, and if we don't use this workaround tests will fail with different errors, including the dreaded |
Same case as @sk- described. But
Kudos to @Tinche . His post has aged well. |
How to fix it with 0.23 version? In my case the latest test always failed because of "Event loop is closed" |
@n-elloco Pytest-asyncio v0.23 supports running tests in different event loops. There's a loop for each level of the test suite (session, package, module, class, function). However, pytest-asyncio wrongly assumes that the scope of an async fixture is the same as the scope of the event loop in which the fixture runs. This essentially makes it impossible to have a session-scoped fixture that runs in the same loop as a function-scoped test. See #706. If v0.23 causes trouble for you, I suggest sticking to pytest-asyncio v0.21, until the issue is resolved. |
So far, the only way for me to solve this issue was to run all tests and fixtures in function scope. This is mostly fine but the execution speed would definitely benefit from having some expensive fixtures in the session scope. Looking forward to future releases that will hopefully solve the problem and allow to run async tests and have async fixtures in the session scope. |
@konstantinmiller Pytest-asyncio v0.24.0 was released just today. It allows specifying different scopes for the asyncio loop and the fixture caching. Your description sounds like it would be addressed by that feature. |
I have a very simple app called "myapp". It uses the AsyncElasticsearch client:
My question is not about AsyncElasticsearch, it just happens to be an async thing I want to work with, could be sth else like a Mongo driver or whatever.
I want to test
do_async_stuff()
and wrote the following conftest.py... and test_stuff.py
When I execute the test I get an exception with the message "attached to a different loop". Digging into that matter I found that pytest-asyncio creates a new event_loop for each test case (right?). The Elasticsearch client however, takes the default loop on instantiation and sticks with it. So I tried to convince it to use the pytest-asyncio event_loop like so:
This however gives me another exception:
How am I supposed to test this scenario?
The text was updated successfully, but these errors were encountered: