Skip to content
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

As of sanic 21.3.0 pytest-sanic fails with "TypeError: serve() got an unexpected keyword argument 'main_start'" #50

Closed
tempelkim opened this issue Mar 31, 2021 · 2 comments · Fixed by #52

Comments

@tempelkim
Copy link

Running a stripped down version of the sanic_client example fails when used with current sanic-21.3.2:


loop = <uvloop.Loop running=False closed=False debug=False>, app = Sanic(name="test_sanic_app")
sanic_client = <function sanic_client.<locals>.create_client at 0x7fccc46691f0>

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

tests/test_pytest.py:20:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
uvloop/loop.pyx:1494: in uvloop.loop.Loop.run_until_complete
    ???
.venv/lib/python3.8/site-packages/pytest_sanic/plugin.py:203: in create_client
    await client.start_server()
.venv/lib/python3.8/site-packages/pytest_sanic/utils.py:224: in start_server
    await self._server.start_server()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pytest_sanic.utils.TestServer object at 0x7fccc465e850>

    async def start_server(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind((self.host, 0))
        self.port = self.socket.getsockname()[1]

        # server settings
        server_settings = self.app._helper(
            host=self.host, port=self.port,
            ssl=self.ssl, sock=self.socket,
            loop=self.loop, protocol=self.protocol,
            backlog=self.backlog, run_async=True)

        # clean up host/port.
        # host/port should not be used.
        server_settings['host'] = None
        server_settings['port'] = None

        # Let's get listeners
        self.before_server_start = server_settings.get('before_start', [])
        self.after_server_start = server_settings.get('after_start', [])
        self.before_server_stop = server_settings.get('before_stop', [])
        self.after_server_stop = server_settings.get('after_stop', [])

        # Trigger before_start events
        await trigger_events(self.before_server_start, self.loop)

        # Connections
        server_settings["connections"] = self.connections

        # start server
>       self.server = await serve(**server_settings)
E       TypeError: serve() got an unexpected keyword argument 'main_start'

Adding something like:

        main_start = server_settings.pop("main_start", None)
        main_stop = server_settings.pop("main_stop", None)
        if main_start or main_stop:
            warnings.warn(
                "Listener events for the main process are not available "
                "with start_server()"
            )

in utils.py should fix it.

Support for Sanic-21 additionally requires dropping python-3.6 as it is on >=3.7 - allowing uvloop-0.15.2 instead of 0.14.0.

I can create a PR if you want me to...

AluisioASG added a commit to AluisioASG/nixpkgs that referenced this issue Apr 30, 2021
pytest-sanic is incompatible with the current version of Sanic, see
sanic-org/sanic#2095 and yunstanford/pytest-sanic#50.  While it is
broken, we also need it to run Sanic's tests (for which case it works
just fine).
@ergoproxxxy
Copy link

If you cannot or do not want to edit source code of library, you can use the fixture below.

@pytest.fixture(scope="session")
def sanic_client_fixup():
    async def serve_mocked(**server_settings):
        server_settings.pop("main_start", None)
        server_settings.pop("main_stop", None)
        return await serve(**server_settings)

    with patch("pytest_sanic.utils.serve", MagicMock(side_effect=serve_mocked)):
        yield

@yunstanford
Copy link
Owner

merged the PR to unblock. i'm gonna revisit this over weekend and checkout if there are any other pieces needs to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants