Skip to content

RuntimeError: Event loop is closed #58

@HeathLee

Description

@HeathLee
import asyncio

import pytest
from sanic import Blueprint, Sanic, response


@pytest.fixture
def app():
    app = Sanic("test_sanic_app")

    bp = Blueprint("blueprint_route", url_prefix="/")

    @bp.route("/test_get")
    async def bp_root(request):
        return response.json({"GET": True})

    app.blueprint(bp)

    yield app


@pytest.fixture
def test_cli(loop, app, sanic_client):
    return loop.run_until_complete(sanic_client(app))


@pytest.fixture
async def sleep():
    return await asyncio.sleep(1)
async def test_get(test_cli, sleep):
    resp = await test_cli.get("/test_get")
    assert resp.status_code == 200
    resp_json = resp.json()
    assert resp_json == {"GET": True}

and then run pytest got this error

================================================================================================================== test session starts ===================================================================================================================
platform linux -- Python 3.9.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: xxxxxx
plugins: asyncio-0.15.1, anyio-3.3.0, sanic-1.8.1
collected 1 item                                                                                                                                                                                                                                         

tests/test_api.py FE                                                                                                                                                                                                                               [100%]

========================================================================================================================= ERRORS =========================================================================================================================
_____________________________________________________________________________________________________________ ERROR at teardown of test_get ______________________________________________________________________________________________________________

loop = <uvloop.Loop running=False closed=True debug=False>

    @pytest.fixture
    def sanic_client(loop):
        """
        Create a TestClient instance for test easy use.
    
        test_client(app, **kwargs)
        """
        clients = []
    
        async def create_client(app, **kwargs):
            client = TestClient(app, **kwargs)
            await client.start_server()
            clients.append(client)
            return client
    
        yield create_client
    
        # Clean up
        if clients:
            for client in clients:
>               loop.run_until_complete(client.close())

.venv/lib/python3.9/site-packages/pytest_sanic/plugin.py:212: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
uvloop/loop.pyx:1468: in uvloop.loop.Loop.run_until_complete
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   ???
E   RuntimeError: Event loop is closed

uvloop/loop.pyx:703: RuntimeError
----------------------------------------------------------------------------------------------------------------- Captured stdout setup ------------------------------------------------------------------------------------------------------------------
[2021-08-09 23:21:34 +0800] [5609] [INFO] Goin' Fast @ http://127.0.0.1:46037
------------------------------------------------------------------------------------------------------------------- Captured log setup -------------------------------------------------------------------------------------------------------------------
INFO     sanic.root:app.py:1236 Goin' Fast @ http://127.0.0.1:46037
======================================================================================================================== FAILURES ========================================================================================================================
________________________________________________________________________________________________________________________ test_get ________________________________________________________________________________________________________________________

pyfuncitem = <Function test_get>

    def pytest_pyfunc_call(pyfuncitem):
        """
        Run test coroutines in an event loop.
        """
        if _is_coroutine(pyfuncitem.function):
            loop = pyfuncitem.funcargs[LOOP_KEY]
            funcargs = pyfuncitem.funcargs
            testargs = {}
            for arg in pyfuncitem._fixtureinfo.argnames:
                testargs[arg] = funcargs[arg]
            loop.run_until_complete(
>               loop.create_task(
                    pyfuncitem.obj(**testargs)
                )
            )

.venv/lib/python3.9/site-packages/pytest_sanic/plugin.py:77: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
uvloop/loop.pyx:1422: in uvloop.loop.Loop.create_task
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   ???
E   RuntimeError: Event loop is closed

uvloop/loop.pyx:703: RuntimeError
----------------------------------------------------------------------------------------------------------------- Captured stdout setup ------------------------------------------------------------------------------------------------------------------
[2021-08-09 23:21:34 +0800] [5609] [INFO] Goin' Fast @ http://127.0.0.1:46037
------------------------------------------------------------------------------------------------------------------- Captured log setup -------------------------------------------------------------------------------------------------------------------
INFO     sanic.root:app.py:1236 Goin' Fast @ http://127.0.0.1:46037
==================================================================================================================== warnings summary ====================================================================================================================
tests/test_api.py::test_get
  .venv/lib/python3.9/site-packages/pytest_sanic/plugin.py:77: RuntimeWarning: coroutine 'test_get' was never awaited
    loop.create_task(

tests/test_api.py::test_get
  .venv/lib/python3.9/site-packages/pytest_sanic/plugin.py:212: RuntimeWarning: coroutine 'TestClient.close' was never awaited
    loop.run_until_complete(client.close())

-- Docs: https://docs.pytest.org/en/stable/warnings.html
================================================================================================================ short test summary info =================================================================================================================
FAILED tests/test_api.py::test_get - RuntimeError: Event loop is closed
ERROR tests/test_api.py::test_get - RuntimeError: Event loop is closed
========================================================================================================= 1 failed, 2 warnings, 1 error in 1.17s =========================================================================================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions