Skip to content

Importing app has loop already running #1

@wrnrlr

Description

@wrnrlr

This problem could very well be caused by me being confused about the correct usage of testing an existing Sanic application, but when I try using my app as a fixture like so

from myapp import server

@pytest.yield_fixture
def app():
    yield server.app

@pytest.fixture
def test_cli(loop, app, test_client):
    return loop.run_until_complete(test_client(app, protocol=WebSocketProtocol))

I get this error:

test setup failed
loop = <_UnixSelectorEventLoop running=False closed=False debug=False>
app = <sanic.app.Sanic object at 0x10a350ac8>
test_client = <function test_client.<locals>.create_client at 0x10a8330d0>

    @pytest.fixture
    def test_cli(loop, app, test_client):
>       return loop.run_until_complete(test_client(app, protocol=WebSocketProtocol))

tests/test_registration.py:18: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py:466: in run_until_complete
    return future.result()
/usr/local/lib/python3.6/site-packages/pytest_sanic/plugin.py:129: in create_client
    await client.start_server()
/usr/local/lib/python3.6/site-packages/pytest_sanic/utils.py:185: in start_server
    await self._server.start_server(loop=self._loop)
/usr/local/lib/python3.6/site-packages/pytest_sanic/utils.py:88: in start_server
    trigger_events(self.after_server_start, self.loop)
/usr/local/lib/python3.6/site-packages/sanic/server.py:354: in trigger_events
    loop.run_until_complete(result)
/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py:454: in run_until_complete
    self.run_forever()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <_UnixSelectorEventLoop running=False closed=False debug=False>

    def run_forever(self):
        """Run until stop() is called."""
        self._check_closed()
        if self.is_running():
>           raise RuntimeError('This event loop is already running')
E           RuntimeError: This event loop is already running

Why is it behaving this way, If i simply define a test app in de closure of the app function everything works as documented, when i import it from somewhere else I get this error. BTW the server file gaurds the app.run(...) statement with an if __name__ == "__main__":. Any hints are more then welcome, slightly pulling on my own hair here.

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