-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
"Event Loop is Closed" error while Testing #988
Comments
@jjackoway Also, |
@yunstanford fair enough, although I still encounter the same error using fixtures + pytest-sanic once I add a startup listener to my app as shown. |
@jjackoway weird, can you show me some pieces of your code and the error msg ? and which version of Sanic are you using ? I have some unit tests around |
@yunstanford I'm using sanic 0.6.0, pytest-sanic 0.1.4 conftest:
Test Cases (simplified to demonstrate the issue):
Errors: Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/_base.py", line 297, in _invoke_callbacks
callback(self)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/futures.py", line 419, in _call_set_state
dest_loop.call_soon_threadsafe(_set_state, destination, source)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 620, in call_soon_threadsafe
self._check_closed()
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 357, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed And
Which happens on the await create_index line initiated by the following block:
|
Sorry, your code is not organized very well, i can't get much info... which mongodb driver are you using ? How did you initialize your db driver ? you may wanna pass BTW, i used motor + sanic, which works fine. And why do you wanna fixture |
I'm using motor, and I'm not explicitly doing anything with the loop anywhere inside of any of my code. I don't really want to start doing that and complicate my server code. I pass ut as a fixture because I use some of the more complex assertions in other tests, and it's convenient to have it be a fixture. |
@jjackoway Ideally, you don't have to pass I'm pretty sure, |
Hello @yunstanford and @jjackoway . Got the same issue. Created a minimal app to reproduce it, please find it here. The problem is described in the README. Any idea on how to fix that? |
@bratushka the sanic test_client will be tear down for each single request, probably something wrong there. did you try |
@yunstanford I don't use |
Facing exact same issue, how do I run multiple requests within test case using pytest-sanic. |
Could someone provide an end to end example for implementing multiple requests, or create proper tutorials? |
@jibinmathew691993 Can you help me with a bit more details of what your use case is? A bit more detail on what's the expectation for example/tutorial would help. |
@jibinmathew691993 if you are using connection = None
async def get_connection():
"""Get existing connection if not closed or create one"""
global connection
if connection is None or connection.closed:
connection = await aiopg.connect(
database='user',
user='user',
password='password',
host='db',
)
return connection I've got a workaround for you. The if connection is None or connection.closed or connection._loop.is_closed: Reason: P.S.: I don't think |
I am running an
|
@jibinmathew691993 please show your |
@bratushka @harshanarayana
|
@jibinmathew691993 and the code of the |
this is the function call in
|
@jibinmathew691993 I am looking into this to see what I can find out. I will update this thread shortly with findings. |
I am closing this as it does not seem like a Sanic issue and should either be addressed on |
For anyone interested, this solved the issue in testing Faust agents for me. adding the following fixture - @pytest.mark.asyncio()
@pytest.fixture()
def event_loop():
"""necessary for testing Faust agents"""
yield app.loop |
I've had multiple issues with the event loop in my tests. I suspect they're all related, but am not totally sure.
The first is that if I try to make multiple requests to my app using app.test_client in a single test case, I get errors on awaits within my app that the event loop is closed:
I also get this error shortly afterwards:
RuntimeError: Task <Task pending coro=<Sanic.handle_request() running at /Users/.../server.py:36> cb=[run_until_complete.<locals>.<lambda>()]> got Future <Future pending cb=[_chain_future.<locals>._call_check_cancel() at /usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/futures.py:408]> attached to a different loop
I was able to work around this using pytest-sanic, setting up some fixtures, and restructuring my tests to all be async functions.
However, this process is kind of painful and not how I'd like to test my app, and it would be great if we could configure the test_client not to shut down its event loop after a single request.
Even with that workaround, however, I then encountered the same issue when I added a startup listener to my app:
I get the same set of errors.
The text was updated successfully, but these errors were encountered: