sanic: "Event Loop is Closed" error while Testing
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:
tests/integration/test_v1.py exception calling callback for <Future at 0x10e027588 state=finished returned UpdateResult>
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 "uvloop/loop.pyx", line 1023, in uvloop.loop.Loop.call_soon_threadsafe (uvloop/loop.c:23219)
File "uvloop/loop.pyx", line 500, in uvloop.loop.Loop._call_soon (uvloop/loop.c:13241)
File "uvloop/loop.pyx", line 504, in uvloop.loop.Loop._call_soon_handle (uvloop/loop.c:13300)
File "uvloop/loop.pyx", line 532, in uvloop.loop.Loop._check_closed (uvloop/loop.c:13860)
RuntimeError: 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:
@app.listener('before_server_start')
async def setup_mongo(app, loop):
await Db.initialize()
#for reference
async def initialize():
db = await Db.get_db()
await db.....create_index(...)
I get the same set of errors.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 5
- Comments: 22 (8 by maintainers)
@jibinmathew691993 I am looking into this to see what I can find out. I will update this thread shortly with findings.
Could someone provide an end to end example for implementing multiple requests, or create proper tutorials?